|
|
|
|||
|
Session TrackingSession tracking is a mechanism that servlets use to maintain state about a series of requests from the same user (that is, requests originating from the same browser) across some period of time. Sessions are shared among the servlets accessed by a client. This is convenient for applications made up of multiple servlets. For example, Duke's Bookstore uses session tracking to keep track of the books being ordered by a user. All the servlets in the example have access to the user's session. To use session tracking,
Obtaining a SessionThe To properly maintain the session, you must call The Duke's Bookstore example uses
session tracking to keep track of the books in the user's shopping cart.
Here is an example of the
public class CatalogServlet extends HttpServlet {
public void doGet (HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException
{
// Get the user's session and shopping cart
HttpSession session = request.getSession(true);
...
out = response.getWriter();
...
}
}
Storing and Getting Data from a SessionThe
The Duke's Bookstore example uses session tracking to keep track
of the books in the user's shopping cart. Here is an example of the
public class CatalogServlet extends HttpServlet {
public void doGet (HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException
{
// Get the user's session and shopping cart
HttpSession session = request.getSession(true);
ShoppingCart cart =
(ShoppingCart)session.getValue(session.getId());
// If the user has no cart, create a new one
if (cart == null) {
cart = new ShoppingCart();
session.putValue(session.getId(), cart);
}
...
}
}
Because an object can be associated with a session, the Duke's
Bookstore example keeps track of the books that a user has ordered within
an object. The object is type
public void doGet (HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException
{
HttpSession session = request.getSession(true);
ShoppingCart cart = (ShoppingCart)session.getValue(session.getId());
...
// Check for pending adds to the shopping cart
String bookId = request.getParameter("Buy");
//If the user wants to add a book, add it and print the result
String bookToAdd = request.getParameter("Buy");
if (bookToAdd != null) {
BookDetails book = database.getBookDetails(bookToAdd);
cart.add(bookToAdd, book);
out.println("<p><h3>" + ...);
}
}
Finally, note that a session can be designated as new. A new
session causes the You must deal with situations involving new sessions. In the Duke's
Bookstore example above, if the user has no shopping cart (the only data
associated with a session), the servlet creates a new one. Alternatively,
if you need information from the user to start a session (such as a
user-name), you might want to redirect the user to an "starting page" where
you collect the necessary information.
Invalidating the SessionA user's session can be invalidated manually or, depending on where the
servlet is running, automatically. (For example, the Java Web Server
automatically invalidates a session when there have been no page requests in
some period of time, 30 minutes by default.) To invalidate a session means to
remove the To manually invalidate a session, use the session's
public class ReceiptServlet extends HttpServlet {
public void doPost(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
...
scart = (ShoppingCart)session.getValue(session.getId());
...
// Clear out shopping cart by invalidating the session
session.invalidate();
// set content type header before accessing the Writer
response.setContentType("text/html");
out = response.getWriter();
...
}
}
Handling All BrowsersBy default, session tracking uses cookies to associate a session
identifier with a user. To also support users that access a servlet with a
browser that does not support cookies, or that is set up to reject cookies,
you must use URL rewriting instead. (While some web servers support URL
rewriting, the When you use URL rewriting you call methods that, when necessary, include the session ID in a link. You must call these methods for every link in the servlet response. The method that associates a session ID with a URL is
The The Duke's Bookstore example uses URL rewriting for all of the
links that it returns to its users. For example, the
public class CatalogServlet extends HttpServlet {
public void doGet (HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException
{
// Get the user's session and shopping cart, the Writer, etc.
...
// then write the data of the response
out.println("<html>" + ...);
...
// Get the catalog and send it, nicely formatted
BookDetails[] books = database.getBooksSortedByTitle();
...
for(int i=0; i < numBooks; i++) {
...
//Print out info on each book in its own two rows
out.println("<tr>" + ...
"<a href=\"" +
response.encodeUrl("/servlet/bookdetails?bookId=" +
bookId) +
"\"> <strong>" + books[i].getTitle() +
" </strong></a></td>" + ...
"<a href=\"" +
response.encodeUrl("/servlet/catalog?Buy=" + bookId)
+ "\"> Add to Cart </a></td></tr>" +
}
}
}
If the user clicks on a link with a rewritten URL, the servlet
recognizes and extracts the session ID. Then the On the other hand, if the user's browser does not support cookies and the user clicks on an un-rewritten URL, the user's session is lost. The servlet contacted through that link creates a new session, but the new session does not have the data associated with the previous session. Once a servlet loses the session data, the data is lost for all servlets that share the session. You should consistently use URL rewriting if your servlet is to support clients that do not support or accept cookies. | |||
|