I am trying to set a persistent cookie to the end users hard drive and I am using the Cookie class. But I am unable to set any of the properties for the cookie besides the name and value. That is I cannot set the MaxAge value or the Path value. Any one know what is going on? Am I missing something?
Here is the code for a
servlet that sets 2 cookies and then retrieves their information:
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
public class CookieCreator1 extends HttpServlet {
private static final
String CONTENT_TYPE = "text/html";
public void init(ServletConfig config) throws ServletException {
super.init(config);
}
public void service(HttpServletRequest request, HttpServletResponse response)
throws IOException {
response.setContentType(CONTENT_TYPE);
PrintWriter out= response.getWriter();
Cookie cookie1 = new Cookie("userIden", "jeff");
Cookie cookie2 = new Cookie("userIden2", "sunder");
cookie1.setMaxAge(100000);
cookie2.setMaxAge(120000);
cookie1.setPath("C:\\documents and settings\\jeff\\cookies");
response.addCookie(cookie1);
response.addCookie(cookie2);
out.println("COOKIES SET");
out.println("<br><table border=\"1\">");
out.println("<tr><td>"+cookie1.getName()+"</td><td>"+cookie1.getValue()+"</td><td>"+cookie1.getMaxAge()+"</td><td>"+cookie1.getPath( )+"</td></tr>");
out.println("<tr><td>"+cookie2.getName()+"</td><td>"+cookie2.getValue()+"</td><td>"+cookie2.getMaxAge()+"</td><td>"+cookie2.getPath( )+"</td></tr>");
out.println("</table><br>");
out.println("<table border=\"1\">");
Cookie[] c = request.getCookies();
for (int z=0; z<=c.length-1; z++) {
if (c[z].getName().equals("userIden")) {
out.println("<tr><td>cookie 1:</td><td>"+c[z].getName()+"</td><td>"+c[z].getValue()+"</td><td>"+c[z].getMaxAge()+"</td><td>"+c[z].getPath()+"</td></tr& gt;");
}else if(c[z].getName().equals("userIden2")) {
out.println("<tr><td>cookie 2:</td><td>"+c[z].getName()+"</td><td>"+c[z].getValue()+"</td><td>"+c[z].getMaxAge()+"</td><td>"+c[z].getPath()+"</td></tr& gt;");
}
}
out.println("</table>");
}
}
[This message has been edited by Jeff Sunder (edited July 25, 2001).]