Cookies for Session Management

A Cookie is a small piece of information created at server side and stored at the cache of the browser(at client side). A cookie has a name, a single value, and optional attributes such as a comment, path and domain qualifiers, a maximum age, and a version number.

Here when the first request sends from client to the server, then at the server side cookie is created. Then the server sends the response along with cookie to the client. When the second request sends from that particular client then the request goes along with cookie. Then the server recognizes the user as old user by cookie.

In Servlet, there are two types of cookie, such as non-persistent cookie and persistent cookie. Non-persistent cookie is valid for single session only. It is removed each time when user closes the browser and persistent cookie is valid for multiple session. It is removed only if user logout or signout.

We can create cookie by creating Cookie object. Cookie is an in-built class present in Servlet API. Cookies are added to response object by invoking the addCookie() method, and we can get the cookie by invoking getCookies() method.

Cookie in Servlet
img
img
index.html
<html>
<head>
<meta charset="UTF-8">
<title>My First Form</title>
</head>
<body>
    <center><h2>My Form</h2>
        <form action="FirstServlet">
            Name<input type="text" name="name"><br>
            <input type="submit" value="ok">
        </form>
    </center>
</body>
</html>
FirstServlet.java
package com.priyanka;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
public class FirstServlet extends HttpServlet {
    protected void doGet(HttpServletRequest request, HttpServletResponse
    response) throws ServletException, IOException {
        response.setContentType("text/html");
        PrintWriter out=response.getWriter();
        String s1=request.getParameter("name");
        out.print("Hi"+" "+s1);
        //create a Cookie
        Cookie ck=new Cookie("cname",s1);
        response.addCookie(ck);
        out.print("<form action='SecondServlet'>");
        out.print("<input type='submit'value='go'>");
        out.print("</form>");
    }
}
SecondServlet.java
package com.priyanka;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;

public class SecondServlet extends HttpServlet {
    protected void doGet(HttpServletRequest request, HttpServletResponse
    response) throws ServletException, IOException {
        response.setContentType("text/html");
        PrintWriter out=response.getWriter();
        Cookie[] arr=request.getCookies();
        out.print("Welcome"+" "+arr[0].getValue());
    }
}
web.xml
<web-app>
    <servlet>
        <servlet-name>abc</servlet-name>
        <servlet-class>com.priyanka.FirstServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>abc</servlet-name>
        <url-pattern>/FirstServlet</url-pattern>
    </servlet-mapping>
    <servlet>
        <servlet-name>def</servlet-name>
        <servlet-class>com.priyanka.SecondServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>def</servlet-name>
        <url-pattern>/SecondServlet</url-pattern>
    </servlet-mapping>
    <welcome-file-list>
        <welcome-file>index.html</welcome-file>
    </welcome-file-list>
</web-app>

Output
img
img
img


About the Author



Silan Software is one of the India's leading provider of offline & online training for Java, Python, AI (Machine Learning, Deep Learning), Data Science, Software Development & many more emerging Technologies.

We provide Academic Training || Industrial Training || Corporate Training || Internship || Java || Python || AI using Python || Data Science etc






 PreviousNext