Books Online Courses
Free Tutorials  Go to Your University  Placement Preparation 
Latest:- Important tips for Campus Placements
0 like 0 dislike
308 views
in RTU B.Tech (CSE-V Sem) Advance Java Lab by Goeduhub's Expert (5.8k points)

Session Tracking in Servlets using Cookies

1 Answer

0 like 0 dislike
by Goeduhub's Expert (5.8k points)
selected by
 
Best answer

Sesson Tracking in Servlets

  • Session simply means a particular interval of time.
  • Session Tracking is a way to maintain state (data) of an user. It is also known as session management in servlet.
  • Http protocol is a stateless so we need to maintain state using session tracking techniques. Each time user requests to the server, server treats the request as the new request. So we need to maintain the state of an user to recognize to particular user.

Cookies

  • Small files that store information on client's computer.
  • Servlet can check previous cookies for information.

How Cookies Works 

By default, each request is considered as a new request. In cookies technique, we add cookie with response from the servlet. So cookie is stored in the cache of the browser. After that if request is sent by the user, cookie is added with request by default. Thus, we recognize the user as the old user.

Age of Cookies

  • Cookies have a lifespan
  • Can set maximum age 
  • Cookies can expire and are deleted

Types of Cookies 

  • Non-persistent cookie: It is valid for single session only. It is removed each time when user closes the browser.
  • Persistent cookie: It is valid for multiple session. It is not removed each time when user closes the browser. It is removed only if user logout or signout.
Pros of Cookies Cons of Cookies
Simplest technique of maintaining the state It will not work if cookie is disabled from the browser
Cookies are maintained at client side Only textual information can be set in Cookie object

Methods of Cookie Class

Methods Description
Cookie() constructs a cookie
Cookie(String name, String value) constructs a cookie with a specified name and value
public void setMaxAge(int expiry) Sets the maximum age of the cookie in seconds
public String getName() Returns the name of the cookie. The name cannot be changed after creation. 
public String getValue() Returns the value of the cookie
public void setName(String name) changes the name of the cookie
public void setValue(String value) changes the value of the cookie
Public void addCookie(Cookie ck) method of HttpServletResponse interface is used to add cookie in response object
public Cookie[] getCookies() method of HttpServletRequest interface is used to return all the cookies from the browser

Example Code

FirstServlet.java

import java.io.*;

import javax.servlet.*;

import javax.servlet.http.*;

public class FirstServlet extends HttpServlet {

  public void doPost(HttpServletRequest request, HttpServletResponse response){

try{

response.setContentType("text/html");

PrintWriter out = response.getWriter();

String n=request.getParameter("userName");

out.print("Welcome "+n);

Cookie ck=new Cookie("uname",n);//creating cookie object

        response.addCookie(ck);//adding cookie in the response

//creating submit button

out.print("<form action='servlet2' method='post'>");

out.print("<input type='submit' value='go'>");

out.print("</form>");

out.close();

        }catch(Exception e){System.out.println(e);}

  }

}

SecondServlet.java

import java.io.*;

import javax.servlet.*;

import javax.servlet.http.*;

public class SecondServlet extends HttpServlet {

public void doPost(HttpServletRequest request, HttpServletResponse response){

try{

response.setContentType("text/html");

PrintWriter out = response.getWriter();

Cookie ck[]=request.getCookies();

out.print("Hello "+ck[0].getValue());

out.close();

         }catch(Exception e){System.out.println(e);}

}

}

index.html

<form action="servlet1" method="post">

Name:<input type="text" name="userName"/><br/>

<input type="submit" value="go"/>

</form>

web.xml

<?xml version="1.0"?>

<web-app>

<servlet>

    <description>This is the description of my J2EE component</description>

    <display-name>This is the display name of my J2EE component</display-name>

    <servlet-name>Servlet1</servlet-name>

    <servlet-class>FirstServlet</servlet-class>

  </servlet>

  <servlet>

    <description>This is the description of my J2EE component</description>

    <display-name>This is the display name of my J2EE component</display-name>

    <servlet-name>SecondServlet</servlet-name>

    <servlet-class>SecondServlet</servlet-class>

  </servlet>

  <servlet-mapping>

    <servlet-name>Servlet1</servlet-name>

    <url-pattern>/servlet1</url-pattern>

  </servlet-mapping>

  <servlet-mapping>

    <servlet-name>SecondServlet</servlet-name>

    <url-pattern>/servlet2</url-pattern>

  </servlet-mapping>

  <welcome-file-list>

    <welcome-file>index.html</welcome-file>

  </welcome-file-list>

</web-app>

Output

Output 1

Output 2

Output 2


For more RTU V Sem Advance Java Lab Experiments Click here


3.3k questions

7.1k answers

395 comments

4.6k users

Related questions

 Goeduhub:

About Us | Contact Us || Terms & Conditions | Privacy Policy || Youtube Channel || Telegram Channel © goeduhub.com Social::   |  | 
...