Java Hosting » Servlet Hosting » JSP tutorial » Servlet session tracking

Servlet session tracking

HTTP is a stateless protocol: it doesn't provides a way for a server to recognize which client is using what part of the application. The shopping cart software is classic example, a client can selected the items from multiply actions in his virtual box. Other examples include sites that use online communication portals, or database managing.

Great number of web-oriented application requires that application has to keep track of the clients performing actions, plain http doesn't provide that, and thus can't be supported without an additional API. To support the software that needs keep track of the state, Java Servlet technology provides an API for managing sessions and allows several mechanisms for implementing sessions.

How to access the session

Sessions are provided by an HttpSession object. One can access a session by calling the getSession method of a request object. You will get the current session associated with this request returned by this method, or, if the request does not have a session, it will create one.

How to associate Attributes with a Session

Object-valued attributes can be associated with a session by name. These attributes would be accessible by any Web component that belongs to the same Web context and is handling a request that is part of the same session. This is the example of the shopping cart session handling:

public class ShoppingCartServlet extends HttpServlet { public void GetIt (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // Find the user's session associated with the cart HttpSession session = request.getSession(); ShoppingCart scart = (ShoppingCart)session. getAttribute("scart"); ... // Get the total amount in the shopping cart double amount = scart.getTotal();

Session Tracking

For professional java hosting visit website.

Session tracking is a great thing. Every user can be associated with a javax.servlet.http.HttpSession object that servlets can use to store or retrieve information about that user. Any set of arbitry can be saved by the Java objects in a session object. For example, a user s session object provides a convenient location for a servlet to store the user s shopping cart contents.

Web container can use several method to store the session objects, one of the method is by passing the session identifier, you can associate the data set in memory with the id you give to the client.

If you intend to use the object identifiers you must make sure, that you check whether the client uses cookies. If te client's cookies are disabled,you must past session id by rewriting the page url. This way the session url is saved on the page url. If cookies are enabled, there is no need to rewrite the page url, because the id is saved into the cookie information.

Servlet's ability to associate a request with a user is allowed by session tracking. A session can be extended across requests and connections of the stateless HTTP. You can maintain sessions in two ways: Using Cookies. A cookie is a string passed from the webserver to a client's browser. It contains the session id. If the browser wants to keep the cookie (cookies are enabled), it will send the same string in the page headers to the browser.

Using rewrited URLs. This is less suitable solution. All links on the page must be encoded dynamicly, so they contain the session id. Also, you cannot have static pages, and all the pages must be created dynamically from the servlet, so they contain the session id, thus pass it to the server and keep session alive.

Contact sales!