Pr.Pg Next Pg

Setting Cookies with Servlet tutorials

  • Setting cookies with servlet involves three steps:

  1. Creating a cookies object: You create a cookie by calling the Cookie constructor, which takes two strings, the cookie name and the cookie value as below.

Cookie cookie = new Cookie(“key”,”value”);

Keep in mind, neither the name nor the value should contain white space or any of the following characters:

[ ] ( ) = , “ / ? @ : ;

 

  1. Setting the maximum age: If you create a cookie and send it to the browser, by default it is a session-level

cookie: a cookie that is stored in the browser’s memory and deleted when the user quits the browser. If you want the browser to store the cookie on disk, use setMaxAge with a time in seconds, as below.

Cookie.setMaxAge(60*60*24*7); // One week

  1. Sending the Cookie into the HTTP response headers: You use response.addCookie to add cookies in the HTTP response header as follows:

response.addCookie(cookie);

 

Example: Servlet program to create a cookie

Sam Sir

//Program name cookiecreation.java

// Program to create a cookie

 

import java.io.IOException;

import java.io.PrintWriter;

import javax.servlet.http.Cookie;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

 

public class cookiecreation extends HttpServlet

{

public void doGet(HttpServletRequest request, HttpServletResponse response)

throws IOException{

 

response.setContentType(“text/html”);

PrintWriter pw = response.getWriter();

 

Cookie cookie = new Cookie(“url”,”cbtsam dot com”);

cookie.setMaxAge(60*60); //1 hour

response.addCookie(cookie);

 

pw.println(“Cookies created”);

}

}

Output

Setting up cookies in servlet

Note: To check in firefox browser that cookies is been created, click on tool àthan click on Options in options click on Privacy tab than in History group select Use custom setting for history from a dropdown box à than click on Show Cookies button which will display cookies information as shown below:

 

checking cookies in Firefox

 

 

Example: Servlet program to set a cookies in form processing parameters

Sam Sir

//Program name CookiesForm.java

// Program to set a cookies in form processing parameters

 

import java.io.IOException;

import java.io.PrintWriter;

import javax.servlet.*;

import javax.servlet.http.*;

 

public class CookiesForm extends HttpServlet

{

protected void doGet(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException

{

Cookie firstName = new Cookie(“first_name”,

request.getParameter(“firstname”));

Cookie lastName = new Cookie(“last_name”,

request.getParameter(“lastname”));

Cookie sex = new Cookie(“sex”,

request.getParameter(“sex”));

Cookie email = new Cookie(“email”,

request.getParameter(“email”));

 

firstName.setMaxAge(60*60*24);

lastName.setMaxAge(60*60*24);

 

response.addCookie( firstName );

response.addCookie( lastName );

response.addCookie(sex);

response.addCookie(email);

 

response.setContentType(“text/html;charset=UTF-8”);

PrintWriter out = response.getWriter();

try {

out.println(“<html>”);

out.println(“<head>”);

out.println(“<title>Setting up cookies</title>”);

out.println(“</head>”);

out.println(“<body>”);

out.println(“<Center><h1> Setting up cookies </h1></Center><BR>”);

out.println(“First Name : “+request.getParameter(“firstname”)+”<BR>”);

out.println(“Last Name : “+request.getParameter(“lastname”)+”<BR>”);

out.println(“Sex : “+request.getParameter(“sex”)+”<BR>”);

out.println(“Email : “+request.getParameter(“email”)+”<BR>”);

out.println(“</body>”);

out.println(“</html>”);

} finally {

out.close();

}

}

}

 

 

 

 

HTML program for user input

Sam Sir

<!—index5.html-->

<!--HTML program for user input-->

<HTML>

<HEAD>

<TITLE>

Setting cookies

</TITLE>

</HEAD>

<BODY>

<FORM action=”CookiesForm” method=”get”>

<P>

<LABEL for=”firstname”>First name: </LABEL>

<INPUT type=”text” name=”firstname”><BR>

<LABEL for=”lastname”>Last name: </LABEL>

<INPUT type=”text” name=”lastname”><BR>

<LABEL for=”email”>email: </LABEL>

<INPUT type=”text” name=”email”><BR>

<INPUT type=”radio” name=”sex” value=”Male”> Male<BR>

<INPUT type=”radio” name=”sex” value=”Female”> Female<BR>

<INPUT type=”submit” value=”Send”> <INPUT type=”reset”>

</P>

</FORM>

</BODY>

</HTML>

Output

Creating cookies

 setting up cookies

Note: Try to enter form data and then click send button. This data would display on your screen and same time it would set four cookies firstName, lastName, sex and email which would be passed back to the server when next time you would press send button

Pr.Pg border                                              Next Pg