2:07 pm - Thursday March 21, 2019

Types of Session in Servlet

Corporate Training for Experienced Candidate
Struts | Hibernate | Spring | Java / J2EE
SOAP | RestFull | Design Pattern | more...
Ph: +91 72000 69003
137b, 2nd st, shanthi nagar, Chrompet, Chennai -600044

Types of session:


The session in servlet is mainly classified into two types

  1. Server side session

Server side session is also known as HttpSession

  1. Client side session

There are three types of client side session

  1. Cookies
  2. URL rewriting
  3. Hidden Form Fields


Cookies are information that a Web server sends to a browser and that the browser returns back to web server when visiting the same Web site later.

Cookie can be saved in a browser as a key value pair

To add a cookie in browser we need to create an object for cookie class and use a HttpResponse object to send the data to browser.

To create cookie object:

            Cookie ck=new Cookie(“key”,”value”);        

To add cookie to browser

        Response.addCookie(ck); // addCookie method takes cookie objects as argument

To get or retrieve the all the cookie present in the browser we need to use a HttpRequest object

To get cookie:

Cookie ck[]=request.getCookies();

To display cookie present in cookie array:

for(int i=0;i<ck.length;i++){

String str=ck[i].getName();

String value=ck[i].getValue();

out.println("<b><br>Name of the Cookie :"+str);

out.println("<b><br> Value of the cookie :"+value);



URL Rewriting:

URL rewriting is the process of sending information to another servlet or same servlet in the form of url

URL rewriting cannot be used for secure data, it will not works under post request.

URL rewriting can be used to maintain a page hits

In URL rewriting we append a identifier to the URL of next servlet or next resource. We can send parameter name/values pairs using the following format:


From the servlet we can use getParameter() method to obtain a parameter value.

String value1=request.getParameter(“name1”);

String value2=request.getParameter(“name2”);

Hidden Form fields: 

            Hidden form is a form of session tracking which saves the information in client browser itself.

We uses hidden box in html to hide the session information from client.

We cannot use this type of session for secure data because  when a client views the source code of the html page the session information can be view by all.

<input type =”hidden” name =”session” value=”sessionid”/>/

In servlet to get session information

    String value = request.getParameter(“session”);


One way to support anonymous session tracking is to use hidden form fields. As the name implies, these are fields added to an HTML form that are not displayed in the client’s browser. They are sent back to the server when the form that contains them is submitted.

In case of hidden form field an invisible text field is used for maintaining state of an user.




Filed in: Z-A Servlet codes

No comments yet.

Leave a Reply