Session Objects in Servlets

Background and Introduction to Session Objects

In the previous tutorial “object scopes and session object” we have learned that HTTP protocol is a stateless protocol it means that our servlet could not store the state of the user from which requests are coming. To store the required state or data we have to use different object and that object is HTTPSession, we have also seen a small example implementing the session object. As one Session object is created for one user or machine. So these objects are available across various request, these objects best fit in the case of logins and shopping carts. We could access these session objects from request objects as mentioned earlier by defining a HttpSession object like this.

Source:  HttpSession session = request.getSession();

Http Session Limitations

This technique has its limitations. That is, we could not access the stored session states across various browsers.Try to test the source that we have elaborated earlier on different browsers and you will notice that the userName that we have saved in session object will not be accessible for the same request on another browser.

Understanding the limitations of Http Session from example

RightClick on the source file named “MyFirstServlet”: RunAs -> RunOnServer the output will have both the userNames as null

Now Pass the query string after the URL as “?userName=John” and press enter to reload the page, this will set both the userNames as John.

Now remove the query string “?userName=John” from the URL and reload it this will set userName to null which is stored in the request object while the userName stored in session object will remain unchanged that is John.

If we access the present URL again and again within the same browser, the userName stored in session object will remain unchanged but if we try to access the same URL from another browser the state will no longer be stored and the userName stored in session object will become null. This proves that the scope of the session object is only inside the browser or for a single user.

Introduction to Context object

So what if we need to store data across multiple users, or multiple servlets within an entire application.we have an option of the context object. You must implement context object in the Tomcat server; we just have to get the data from context object. We can get the context object from the request object as mentioned in the source below.

Source: request.getServletContext();

The type of the context object is ServletContext

So to initialize it as an object we have first to import it as

Source: import javax.servlet.ServletContext

And then we  will initialize it as follows

Source: ServletContext context = request .getServletContext();

And in last, we will set the attribute to store it along multiple URL requests as mentioned in source.

Source: context.setAttribute(“savedUserName”, userName);

Example of Context Object

Let us elaborate a detailed example of above explanation. Edit your “MyFirstServlet.java” as mentioned in below source that we have created earlier

Explanation of Context Object Example:

Try to run this example. RightClick on the source  “MyFirstServlet.java”: RunAs -> RunOnServer the output will have all the userNames as null. copy and paste the link in a browser .

 

Initial URL demonstration of Context Object Example
Initial URL demonstration of Context Object Example

Now again for this example Pass the query string after the URL as “?userName=John” and press enter to reload the page, this will set all the userNames as John.URL will be this “http://localhost:8080/SimpleServletProject/MyFirstServletURL?userName=John”

Now remove the query string “?userName=John” from the URL and refresh the page, this will set userName to null which is stored in the request object while the userName stored in the session object and the userName stored in context objects will remain unchanged that is John. New URL will become this “http://localhost:8080/SimpleServletProject/MyFirstServletURL”

Now to test whether context 0bject is working or not Copy the URL and paste it in another browser and load the page in that browser, this will set userName to null which is stored in the request object and the userName stored in session object will also become null while the userName stored in context objects will remain unchanged that is John.

Context Object Example
Context Object Example

Within one application there could be many session objects, but the context object will be one.

This way you can successfully store the required state across different browsers.

Download Complete Servlets Project Here

Summary

In this tutorial, we have accomplished following goals.

  • Limitations of the session object.
  • Introduction to the context object and its example.

In the next tutorial, we will see the lifecycle of the servlet. So stay tuned and stay blessed.