Reading:  

Introduction to Servlet technology


Working with Cookies

Cookies are text files stored in client computer which is used for tracking information. Java Servlets supports HTTP cookies.

Below are used for checking returning users:

  • Server script send a set of cookies to browser. For example name etc.
  • Browser stores below information in local machine for future reference.
  • When browser sends any request to web server next time then it sends cookies information to server for identifying the user.

Servlet Cookies Methods

Below are the methods which is used to manipulate cookies: 

Method & Description

public void setDomain(String pattern)


The method sets domain to which cookie applies

public String getDomain()


The method gets domain to which cookie applies

public void setMaxAge(int expiry)


The method sets time (in seconds) to cookie before it expires.

public int getMaxAge()


The method returns maximum age of cookie.

public String getName()


The method returns name of cookie.

public void setValue(String newValue)


The method sets value associated with cookie.

public String getValue()


The method gets value associated with cookie.

public void setPath(String uri)


The method sets path cookie applies.

public String getPath()


The method gets path cookie applies.

public void setSecure(boolean flag)


The method set Boolean value to check cookie should be sent over encrypted (i.e. SSL) connection.

public void setComment(String purpose)


The method set comment which describes a cookie's purpose.

public String getComment()


The method returns comment describing purpose of cookie

Setting Cookies with Servlet

Setting cookies with servlet involves following steps:

Creating Cookie object:

The Cookie constructor contain a cookie name and a cookie value which are strings.

Cookie cookie = new Cookie("key","value");

Setting the maximum age:

The setMaxAge  specify how long (in seconds) cookie should be valid.

cookie.setMaxAge(60*60*24);

Sending Cookie into HTTP response headers:

The response.addCookie to add cookies in HTTP response header:

response.addCookie(cookie);

 

Below is the example of use of cookie in servlets:

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class CookieExample extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response)
    throws IOException, ServletException
    {
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();

        // print out cookies

        Cookie[] cookies = request.getCookies();
        for (int i = 0; i < cookies.length; i++) {
            Cookie c = cookies[i];
            String name = c.getName();
            String value = c.getValue();
            out.println(name + " = " + value);
        }

        // set a cookie

        Cookie c = new Cookie("myCookie", "myCookieValue");
        response.addCookie(c);

// delete a cookie

        for (int i = 0; i < cookies.length; i++){
            cookie = cookies[i];
            if((cookie.getName( )).compareTo("myCookie") == 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/>");
         }
}
}
 

 

 

 

Description

This guide introduces Servlet technology and we will cover below topics

  1. What is a Servlet?
  2. Initial Setup
  3. Life Cycle
  4. Various Examples
  5. Client Request
  6. Server Response
  7. Http Status Codes
  8. Writing Filters
  9. Exception Handling
  10. Cookies Handling
  11. Session Tracking
  12. Database Access
  13. Packaging
  14. Internationalization

This is to the point introduction to the topic to get you started

 



Prerequisites

Working knowledge of Core Java is essential

Audience

Beginner to Java Servlet technology or students looking to brush up their skills quickly

Learning Objectives

Learn Java Servlets

Author: Subject Coach
Added on: 2nd May 2015

You must be logged in as Student to ask a Question.

None just yet!