Giving and deleting cookies


File KillCookies.java

The page has three forms: two one-button forms for showing and erasing cookies, and one with parameters for creating a cookie. Cookies need to be added to the response before the getWriter() method is invoked, so information about the cookies is stored in the string cookiesString (via the method addC()) and displayed later, after the writer is obtained and the header for the page is generated.

I can't figure out why the age of the cookies is -1 (i.e. for the duration of the session) instead of the age set by setMaxAge() method when the coookie is created.



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


public class KillCookies extends HttpServlet {

    String cookiesString = ""; // to store cookie info: cannot add to the 
                               // writer until all the cookies are added 
                               // to the response

        public void doGet(HttpServletRequest request,
                      HttpServletResponse response)
        throws IOException, ServletException
    {
	String toDo = request.getParameter("toDo");

	// get the cookies array
	Cookie[] cookies = request.getCookies();

	// show cookies
	if (toDo != null && toDo.equals("Show")) {
	    if (cookies.length > 0) {
		addC("Your cookies: <P>");
		for (int i = 0; i < cookies.length; i++) {
		    Cookie cookie = cookies[i];
		    addC("Cookie Name: " + cookie.getName() + "<br>");
		    addC("Cookie Value: " + cookie.getValue() + "<br>");
		    addC("Cookie Age: " + cookie.getMaxAge() + "<br><BR>");
		}
	    } else {
		addC("No cookies detected!");
	    }
	}
	// give a cookie
	else if (toDo != null && toDo.equals("Give")) {
	    String cookieName = request.getParameter("Name");
	    String cookieValue = request.getParameter("Value");
	    String cookieAge = request.getParameter("Age");
	    if (cookieName != null && cookieValue != null) {
		Cookie cookie = new Cookie(cookieName, cookieValue);
		cookie.setMaxAge((new Integer(cookieAge)).intValue());
		response.addCookie(cookie);
		addC("<P>");
		addC("added a cookie. Name = " + cookieName);
		addC(" Value = " + cookieValue + " Age = " + cookieAge + "<P>");
	    }
	    // erase cookies:
	} else if (toDo != null && toDo.equals("Kill")) {
	    for (int i = 0; i < cookies.length; i++) {
                Cookie cookie = cookies[i];
		cookie.setMaxAge(0); // set the max age to 0
		response.addCookie(cookie); // add them to the response
	    } 
	    addC("Cookies erased!");
	}

	response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        out.println("<html>");
        out.println("<body bgcolor=\"white\">");
        out.println("<head>");

        out.println("<title> Cookies!</title>");
        out.println("</head>");
        out.println("<body>");
	out.println("<b>One of the cookies is the session cookie<BR>");
	out.println("It's generated automatically!</b><P>");
       
        out.println(cookiesString);
	cookiesString = "";

        out.print("<form action=\"");
        out.println("KillCookies\" method=POST>");
        out.println("<input type=hidden name=\"toDo\" value=\"Show\">");
        out.println("<input type=submit value=\"Show cookies\"></form>");	
	out.println("<P>");
        out.print("<form action=\"");
        out.println("KillCookies\" method=POST>");
        out.println("Name: ");
        out.println("<input type=text size=15 name=\"Name\"><P>");
        out.println("Value: ");
        out.println("<input type=text size=15 name=\"Value\">");
        out.println("<P>");
        out.println("Age:  ");
	out.println("<input type=int size=10 name=\"Age\">");
        out.println("<P>");		
        out.println("<input type=hidden name=\"toDo\" value=\"Give\">");
        out.println("<input type=submit value=\"Give a cookie\"></form>"); 	

	//out.println("<P>");
        out.print("<form action=\"");
        out.println("KillCookies\" method=POST>");
        out.println("<input type=hidden name=\"toDo\" value=\"Kill\">");
        out.println("<input type=submit value=\"Erase 'em all!\"></form>");
            
            
        out.println("</body>");
        out.println("</html>");
    }

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

    private void addC(String s) {
	cookiesString = cookiesString + s;
    }

}


This page has been created and is maintained by Elena Machkasova
Comments and suggestions are welcome at emachkas@wellesley.edu

Spring Semester 2002