Chapter 28. Session management 963
A cookie consists of information that is embedded as part of the headers in the HTML stream
passed between the server and the browser. The browser holds the cookie and returns it to
the server whenever the user makes a subsequent request. By default, WebSphere defines
its cookies so they are destroyed if the browser is closed.
The web application developer uses the HTTP request object’s standard interface to obtain
the session, as shown in Example 28-1.
Example 28-1 Get the HTTP session object
//Suppose HttpServletRequest request has been initiated.
HttpSession session = request.getSession(true);
String sessionID = session.getId();
WebSphere places the user’s session identifier in the outbound cookie when the servlet
completes its execution, and the HTML response stream returns to the user.
URL rewriting
A typical usage of URL rewriting is configuring session tracking for Wireless Application
Protocol (WAP) devices. Because most WAP devices do not support cookies, you can
configure these devices to use URL rewriting to track sessions. URL rewriting requires the
developer to perform the following actions:
Use special APIs to encode the URLs.
Set up the site page flow to avoid losing the encoded information.
Program session servlets to encode URLs
URL rewriting works by storing the session identifier in the page that is returned to the user.
WebSphere Application Server encodes the session identifier as a parameter on URLs that
are encoded programmatically by the web application developer. Example 28-2 shows a web
page link with URL encoding.
Example 28-2 Web page link with URL encoding
<a href="/store/catalog;$jsessionid=DA32242SSGE2">
When the user clicks this link to move to the /store/catalog page, the session identifier is
passed in the request as a parameter.
If the servlet returns HTML directly to the requester, without using
JavaServer Pages (JSP),
the servlet calls the API, as shown in Example 28-3, to encode the returning content.
Example 28-3 URL encoding from a servlet
//Suppose HttpServletResponse response has been initiated.
out.println("<a href=\");
out.println(response.encodeURL ("/store/catalog"));
out.println("\>catalog</a>");
The pages using redirection, servlet, or JSP must encode the session ID as part of the
redirection, as shown in Example 28-4.
Example 28-4 URL encoding with redirection
//Suppose HttpServletResponse response has been initiated.
response.sendRedirect(response.encodeRedirectURL("http://myhost/store/catalog"));