How to Work with Cookies in JSP and Servlet - BunksAllowed

BunksAllowed is an effort to facilitate Self Learning process through the provision of quality tutorials.

Random Posts

How to Work with Cookies in JSP and Servlet

Share This

Cookies were originally developed to solve the problem of keeping session context using HTTP. 


Cookies consist of a server sending some information, a "cookie", for a client to store locally, and on future requests to that server, the client sends back the cookie information via HTTP headers. 


Assuming the client's software supports cookies, the whole process requires no user interactions and is an ideal tool for keeping session context.


Due to the nature of cookies, they can be used to snoop, to a limited extent, on a client's browsing habits. For the most part cookie abuse is not a problem, but because of the issue, most browsers allow cookies to be disabled.


Cookies are name/value pairs that in HTTP are exchanged via two headers: Set-Cookie and Cookie. Set-Cookie goes from the server to the client, and Cookie from client to server. 


The Set-Cookie header is sent once when the session is first established. Cookies have a time-out, and a client keeps a copy of the cookie's value until that time-out expires.


Let's try the following code

Content of index.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Insert title here</title> </head> <body> <form action="main.jsp" method="GET"> First Name: <input type="text" name="first_name"> <br /> Last Name: <input type="text" name="last_name" /> <input type="submit" value="Submit" /> </form> </body> </html>


Content of main.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Insert title here</title> </head> <body> <% // Create cookies for first and last names. Cookie firstName = new Cookie("first_name", request.getParameter("first_name")); Cookie lastName = new Cookie("last_name", request.getParameter("last_name")); // Set expiry date after 24 Hrs for both the cookies. firstName.setMaxAge(60 * 60 * 24); lastName.setMaxAge(60 * 60 * 24); // Add both the cookies in the response header. response.addCookie(firstName); response.addCookie(lastName); %> <center> <h1>Setting Cookies</h1> </center> <ul> <li><p> <b>First Name:</b> <%=request.getParameter("first_name")%> </p></li> <li><p> <b>Last Name:</b> <%=request.getParameter("last_name")%> </p></li> </ul> </body> </html>


Content of other1.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Insert title here</title> </head> <body> <center> <h1>Reading Cookies</h1> </center> <% Cookie cookie = null; Cookie[] cookies = null; // Get an array of Cookies associated with this domain cookies = request.getCookies(); if (cookies != null) { out.println("<h2> Found Cookies Name and Value</h2>"); for (int i = 0; i < cookies.length; i++) { cookie = cookies[i]; out.print("Name : " + cookie.getName() + ", "); out.print("Value: " + cookie.getValue() + " <br/>"); } } else { out.println("<h2>No cookies founds</h2>"); } %> </body> </html>


Content of other2.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Insert title here</title> </head> <body> <center> <h1>Reading Cookies</h1> </center> <% Cookie cookie = null; Cookie[] cookies = null; // Get an array of Cookies associated with this domain cookies = request.getCookies(); if (cookies != null) { out.println("<h2> Found Cookies Name and Value</h2>"); for (int i = 0; i < cookies.length; i++) { cookie = cookies[i]; if ((cookie.getName()).compareTo("first_name") == 0) { cookie.setMaxAge(0); response.addCookie(cookie); out.print("Deleted cookie: " + cookie.getName() + "<br/>"); } out.print("Name : " + cookie.getName() + ", "); out.print("Value: " + cookie.getValue() + " <br/>"); } } else { out.println("<h2>No cookies founds</h2>"); } %> </body> </html>

Happy Exploring!

No comments:

Post a Comment