❮ Previous Next ❯

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.



Let's see demo example for better clarity:

index.html

<html>
<body>
<form action=../../../Users/Manish Sinha/Desktop/java8s/abc method="post">
Name:<input type="text" name="uname" />
Password:<input type="text" name="pwd" >
<input type="submit" value="login">
</form>
</body>
</html>

MyServlet1.java

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class MyServlet1 extends HttpServlet
{
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
response.setContentType("text/html");
String name = request.getParameter("uname");
String pass = request.getParameter("pwd");
if(pass.equals("admin123"))
{
Cookie ck = new Cookie("username",name);
response.addCookie(ck);
response.sendRedirect("MyServlet2");
}
}
}

MyServlet2.java

mport java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class MyServlet2 extends HttpServlet
{
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
response.setContentType("text/html);
PrintWriter pw = response.getWriter();
Cookie[] c = request.getCookies();
pw.println("Welcome "+c[0].getValue());
}
}

web.xml

web.xml

<web-app...>
<servlet>
<servlet-name>abc</servlet-name>
<servlet-class>com.javarace.MyServlet1</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>abc</servlet-name>
<url-pattern>/MyServlet1</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>MyServlet2</servlet-name>
<servlet-class>com.javarace.MyServlet2</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>MyServlet2</servlet-name>
<url-pattern>/MyServlet2</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
</web-app>
❮ Previous Next ❯

Follow Us





  Tutorials

Popular Links

Contact Us

address Plot No-N6/530,2ND Floor
Nayapalli,Jayadev Vihar
      Bhubaneswar-751015
      Email: info@silantechnology.com
      Phone: 0674-2361252
address
39877, sundale dr, apt#101, Fremont, California 94538, USA
       Phone: +1(262)388-7619

facebook twitter google linked in

© 2018 Silan Technology. All Rights Reserved