Cookie in Java EE 6

In this tutorial you will learn about the changes made in Cookie in Java EE 6.

Cookie in Java EE 6

In this tutorial you will learn about the changes made in Cookie in Java EE 6.

Cookie in Java EE 6

Cookie in Java EE 6

In this tutorial you will learn about the changes made in Cookie in Java EE 6.

Cookie is an information that contains in a text form is sent to the browser by a servlet which is saved and resend by the browser to the server for uniquely identifying a client. This feature of cookie makes it useful for session management. Cookies are send by a servlet to the browser using addCookie() method of HttpServletResponse interface. Using this method cookies are send to the browser by adding the fields to HTTP response header. And cookies can be found from the request by the use of getCookie() method of HttpServletRequset interface. Size and number of cookie is varied i.e. per web server browser expected to support a 20 cookies, and in total it supports 300 cookies and the size of per cookie may fix by 4 KB.

In Java EE 6 some new methods are added in Cookie class. Some of these are as follows :

  • isHttpOnly() : This method is used to identify for whether the cookie has been noticed for HttpOnly.
  • setHttpOnly() : This method is used to mark or unmark the cookie HttpOnly.

Example :

Here I am giving a very simple example of cookie which will demonstrate you about how to use methods that are newly added in Java EE 6.

cookieExample.html

<!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="CookieExample">
Name <input type="text" name= "name"/>
<input type="submit" value="submit"/>
</form>
</body>
</html>

CookieExample.java

package roseindia.Cookie;

import java.io.IOException;
import java.io.PrintWriter;

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;

@WebServlet("/CookieExample")
public class CookieExample extends HttpServlet
{
private static final long serialVersionUID = 1L;

public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String name = request.getParameter("name");
Cookie cookie = new Cookie("Cookie", name);
response.addCookie(cookie);

out.println("Cookie is set for " + name);
cookie.setHttpOnly(true);
boolean bol = cookie.isHttpOnly();
out.println("<br>Cookie is Marked as HttpOnly = " + bol);
cookie.setHttpOnly(false);
boolean bol1 = cookie.isHttpOnly();
out.println("<br>Cookie is Marked as HttpOnly = " + bol1);
out.close();
}

public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
doGet(request, response);
}
}

Output :

When you will execute the above example you will get the output as :

After when you will enter the value in the appropriate text box and click on submit button you will get the output as :

Download Source Code