by Dinesh Thakur Category: Session And Cookies

Sending cookies to the client involves the following steps,

1. Create a Cookie object.

2. Setting the maximum age.

3. Placing the Cookie into the HTTP response headers.

Creating a Cookie Object

You create a cookie by calling the Cookie constructor that takes the following form Cookie(String cookieName, String cookieValue)

For example:

Cookie c = new cookie (“email”, “ This email address is being protected from spambots. You need JavaScript enabled to view it. ”);

It creates a cookie c with a name email and value This email address is being protected from spambots. You need JavaScript enabled to view it.

Setting the Maximum Age (OPTIONAL)

By default, when a cookie is sent to the browser it is stored in the browser's memory and deleted when the user closes the browser. If you want to store the cookie on the hard disk instead of memory, use setMaxAge () method to specify how long (in seconds) the browser should keep the cookie before it expires. For example, on using the statements

c.setMaxAge (60*60);

The browser will delete the cookie after 60 minutes.

Place the Cookie in Response Headers

After creating the cookie object and calling the setMaxAge () method, the next step is to send the cookie to the client. In order to send the cookie, call the addCookie () method of HttpServletResponse. This method will insert the cookie into a Set-Cookie response header. For example,

response.addCookie (c);

Now let us consider a servlet that will create two cookies named Name and email and send it to the client.

import java.io.*;

import javax.servlet.*;

import javax.servlet.http.*;

public class setCookie extends HttpServlet

{

    public void doGet (HttpServletRequest request, HttpServletResponse

    response) throws ServletException, IOException

    {

        PrintWriter out;

        //set content type and other response header fields first

        response.setContentType (“text/html”);

        //write the response of the data

        out = response.getWriter ();

        //create a cookie            

       Cookie c1 = new Cookie (“name”, “Daljeet”);

       Cookie c2= new Cookie (“email”, “ This email address is being protected from spambots. You need JavaScript enabled to view it. ”);

       response.addCookie (c1);  

       response.addCookie (c2); 

       out.println (“<HTML> <HEAD> <TITLE>”);

       out.println (“Settings cookies”);

       out.println (“</TITLE> </HEAD> <BODY>”);

       out.println (“Two cookies are set named: name and email”);

       out.println (“</BODY> </HTML>”);

       out.close ();

    }

}

Output:   Two cookies are set named: name and email    





About Dinesh Thakur

Dinesh ThakurDinesh Thakur holds an B.SC (Computer Science), MCSE, MCDBA, CCNA, CCNP, A+, SCJP certifications. Dinesh authors the hugely popular blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps. For any type of query or something that you think is missing, please feel free to Contact us.