Java Reference
In-Depth Information
L ISTING 5.3
Continued
}
//Get Servlet information
public String getServletInfo() {
return “URLRewritingServlet Information”;
}
}
This servlet services a GET request and redirects the client to a new URL. This new URL has
the string sid=5748 appended to it. This string represents a session ID. When the servlet that
services the redirection receives the request, it will be able to determine the current user based
on the appended value. At that point, the servlet can perform a database lookup on the user and
her actions based on this ID.
Two methods are involved in this redirection. The first is
HttpServletResponse.encodeRedirectURL() , which takes a String that represents a redirec-
tion URL and encodes it for use in the second method. The second method used is the
HttpServletRequest.sendRedirect() method. It takes the String returned from the
encodeRedirectString() and sends it back to the client for redirection.
The advantage of URL rewriting over hidden form fields is the capability to include session
tracking information without the use of forms. Even with this advantage, it is still a very ardu-
ous coding process.
Session Tracking with the Servlet API
The Servlet API has its own built-in support for session tracking. The HttpSession object pro-
vides this functionality. In this section, I focus on four of the HttpSession 's session tracking
methods.
The first method is the setAttribute() method. The setAttribute() method binds a
name/value pair to store in the current session. If the name already exists in the session, it is
replaced. The method signature for setAttribute() is listed as follows:
public void setAttribute(String name , Object value )
5
The next method is the getAttribute() method, which is used to get an object that is stored
in the session. The getAttribute() method takes a string representing the name that the
desired object is bound to. Its signature is listed as follows:
public Object getAttribute(String name )
 
Search WWH ::




Custom Search