Servlet Cookies tutorial with example
This servlet cookies tutorial shows you how to create, retrieve and delete a cookie from the browser using servlet application
A cookie is a small amount of information sent by a servlet to a Web browser, saved by the browser, and later sent back to the server. A cookie’s value can uniquely identify a client, so cookies are commonly used for session management.
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.
How to create cookies in java
Cookie ck=new Cookie("auth","candidjava");
Servlet cookie max age
You can set expire time for the cookie using the setmaxAge method, this method accepts seconds.
ck.setMaxAge(600); // 600 sec ie.. 10 min
Servlet set a cookie to the browser
You can use the response object to add a cookie to the browser, you can also create a number of cookies and add it to a single response object.
response.addCookie(ck;)
Servlet request get the cookie
A cookie is retrieved from browser as an array, you can use request object getCookies method to access all cookie created by your domain.
Cookie[] cks=request.getCookies()
Delete Servlet cookie
To delete a cookie just recreate the cookie in the same name and set the value as null and age as null.
A negative value in the above method means that the cookie is not stored persistently and will be deleted when the Web browser exits. A zero value causes the cookie to be deleted.
Browser cookie limits and the limit per domain
The browser is expected to support 20 cookies for each Web server, 300 cookies total and may limit cookie size to 4 KB each.
Servlet Cookie Example
index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>Simple Cookie example</h1>
<form action="CookieController" method="post">
Username: <input type="text" name="uname"> <br>
Password: <input type="password" name="pass"> <br>
<input type="submit" value="Login">
</form>
</body>
</html>
Servlet CookieController to create a cookie
package com.candidjava;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class CookieController
*/
@WebServlet("/CookieController")
public class CookieController extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String un = request.getParameter("uname");
String pw = request.getParameter("pass");
Cookie ck = new Cookie("mycookie", un);
response.addCookie(ck);
response.sendRedirect("home.jsp");
}
}
Display list of Cookie in browser
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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=UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>Retrieving Cookie from browser</h1>
<br>
<%
Cookie[] cks=request.getCookies();
for(Cookie ck:cks)
{
String cn=ck.getName();
String cv=ck.getValue();
%>
Cookie name : <b><%=cn %> </b><br>
Cookie Value : <b><%=cv %> </b><br>
<%
}
%>
</body>
</html>