Tims Servlet Tutorial

Tutorial Home

  1. What are servlets ?
  2. The Hello World Servlet
  3. Running Servlets
  4. Reading Form Data
  5. Servlet Configuration
  6. Client Side Form Validation
  7. Cookies
  8. Sessions
  9. Authentication
  10. Dynamic Images
  11. Design, Testing, Security
  12. Servlets and Apache
  13. Using Servlets in SOCS

Cookies


Cookies are small pieces of information which can be sent to a browser by a server program and stored by the web browser. The web browser will then pass the cookie back to the server every time it makes a request from that server. This facility is particularly useful for allowing authentication, for example, when a user logs into a password restricted system, a cookie containing that users login/password details can be set with those details, so that the user does not have to re-type their password for every new page they wish to download.

Below, the MyNameServlet has been modified so that it can set a cookie and 'remember' the name of the person using it.

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

public class MyNameServlet extends HttpServlet
{
 private String message;

 /**
 * Servlet method to initialise the class
 * @param conf a ServletConfig object describing the default startup params
 *   for this class
 **/

 public void init(ServletConfig conf) throws ServletException
 {
  message=conf.getInitParameter("MESSAGE");
 }

 /**
 * Method to receive get requests from the web server
 * (Passes them onto the doPost method)
 * @param req The HttpServletRequest which contains the information submitted via get
 * @param res A response containing the required response data for this request
 **/

 public void doGet(HttpServletRequest req, HttpServletResponse res)
  throws ServletException, IOException
 {
  doPost(req,res);
 }

 /**
 * Method to receive and process Post requests from the web server
 * @param req The HttpServletRequest which contains the information submitted via post
 * @param res A response containing the required response data for this request
 **/

 public void doPost(HttpServletRequest req, HttpServletResponse res)
  throws ServletException, IOException
 {
  String name="none";

  //******See if a name has been specified*****
  String[] x=req.getParameterValues("yourname");

  if(x!=null)
  {
   //***A name has been supplied from the form, 
   //  so give the user a cookie with this name***
   name=x[0];
   res.addCookie(new Cookie("MyNameServlet_cookie",name));
  }
  else
  {
   //*****See if there is a cookie in the request containing a name*****
   Cookie[] list=req.getCookies();
   for (int loop=0; loop < list.length ; loop++)
    if(list[loop].getName().equals("MyNameServlet_cookie"))
    {
     //*****The cookie has been found, get its value and exit the loop*****
     name=list[loop].getValue();
     loop=list.length;
    }

  }

  //*****Construct the HTML Reply*****
  String reply="<HTML>\n"+
    "<HEAD><TITLE>My Name Servlet Response</TITLE></HEAD;>\n"+
    "<BODY>\n"+
    "<CENTER><BR><B> \n"+
    message+" "+name+"\n"+
    "</B></CENTER>\n</BODY>\n"+
    "</HTML>";

  //*****Send the reply*****

  res.setContentType("text/html");
  PrintWriter out=res.getWriter();
  out.println(reply);
  out.close();
 }
}

When activated the servlet checks to see if the 'yourname' parameter has been supplied. If it has then this is used for the name of the user and a cookie is sent back to the browser containing this name. If the 'yourname' parameter is not found, then the browser looks for a cookie containing the name of the user and user the value of this cookie if it is found. If the users name cannot be determined, then 'none' will be used. The servlet will then construct and HTML reply, as before.

If you want to try out this servlet the links below link to a version running on our server.

If you try the test cookie link first, you should get the message 'Hello none'. This is because there is currently no cookie stored on your browser. If you now use the form, you will get the normal 'Hello xxx' message. However, a cookie will have been stored, so if you go back to the test cookie link, you will now get a 'hello xxx' message.

Click here to test cookie

Please Enter your name


<< Previous Page | Next Page >>




Tims Home Page       |       Page last modified : 19 May 2003