Enterprise J2ME Developing Mobile Java Applications [Electronic resources] نسخه متنی

اینجــــا یک کتابخانه دیجیتالی است

با بیش از 100000 منبع الکترونیکی رایگان به زبان فارسی ، عربی و انگلیسی

Enterprise J2ME Developing Mobile Java Applications [Electronic resources] - نسخه متنی

Michael Juntao Yuan

| نمايش فراداده ، افزودن یک نقد و بررسی
افزودن به کتابخانه شخصی
ارسال به دوستان
جستجو در متن کتاب
بیشتر
تنظیمات قلم

فونت

اندازه قلم

+ - پیش فرض

حالت نمایش

روز نیمروز شب
جستجو در لغت نامه
بیشتر
لیست موضوعات
توضیحات
افزودن یادداشت جدید



6.3 Session Tracking via HTTP Cookies


Cookies are pieces of NAME=VALUE formatted text embedded in HTTP headers. They are used to track client states. Since cookies reside in HTTP headers, they are transparent to applications and users. The server assigns new cookies to the client through the HTTP header set-cookie. The set-cookie header takes the following format:


set-cookie: NAME=VALUE; expires=DATE; path=PATH;
domain=DOMAIN_NAME; secure

The first NAME=VALUE is the cookie itself and is required. All the following attributes, such as expiration time, domain, and path, are optional. When the client makes subsequent requests, it sends the cookies back in the cookies header to identify itself.


cookie: NAME1=VALUE1; NAME1=VALUE2; ...

Note

The server can send out multiple cookies in one connection using multiple set-cookie headers. The client can send back multiple cookies in one header by delimiting them using semicolons.


6.3.1 Handle Cookies via Decorator Classes


Sun Microsystems' Smart Ticket blueprint v1.1 provides a class SessionConnector that utilizes the decorator pattern to add cookie support into the standard MIDP HTTP framework. The source code of this class is available from this book's Web site. The following snippet demonstrates how to use this class.


HttpConnection c =
(HttpConnection) SessionConnector.open(url);
// You can use "c" as a normal HttpConnection
// but it is session aware now.


6.3.2 Handle Cookies via HttpClient Handlers


To support cookie headers in the HttpClient framework, we need to write the handler class. The source code of handler class (CookieHandler) is shown in Listing 6.4. Method getCookie() parses the response header and stores cookies in a static data member cookies. Method addCookie() matches stored cookies with the current request URL to determine which cookies to send out. Please refer to this book's Web site for complete source code.

Listing 6.4. The CookieHandler class


public class CookieHandler implements Handler {
private static Vector cookies;
private static Vector domains;
public CookieHandler() {
cookies = new Vector ();
domains = new Vector ();
}
public void prepareHeaders(HttpConnection c) throws Exception {
String url = c.getURL ();
addCookie(c, url);
}
public boolean processHeaders (HttpConnection c) throws Exception {
getCookie(c);
return false;
}
// Remove all cookies.
public void removeCookies() throws Exception {
cookies = new Vector ();
domains = new Vector ();
return;
}
// Retrieve cookies from the connection header
// and save them with domain information
private void getCookie(HttpConnection c) throws Exception {
// Parse the incoming cookies and store them in
// cookies and domains vectors.
}
private void addCookie(HttpConnection c,
String url) throws Exception {
// Match the url domain with existing cookies
// in the cookies vector. If a match is found,
// set it into the connection header.
}
}

The use of CookieHandler is illustrated in Listing 6.5.

Listing 6.5. The CookieHandler usage


HttpClient client = new HttpClient ();
Handler h = new CookieHandler();
client.addHandler( h );
client.setUrl( url );
client.setRequestMethod( HttpConnection.GET );
byte [] result = client.query(null);


/ 204