• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Cookie Help

 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).]
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It looks like you are setting the cookies and then examining them before they have been sent to the browser. What do they look like when they come back from the browser?
Bill
 
Jeff Sunder
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I thought if I put the request.addCookies() statement before checking for the existence of the cookies, then that should be fine. In any case, I created a jsp page that I ran after ran the servlet that set the cookies, but I got the same results.
The values for getMaxAge() and getPath() are returning -1 and null respectively.
Any ideas?
 
William Brogden
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You do realize that the purpose of path is to determine which URL addresses get the cookie returned, right?
In other words, if the path is "/" all applications on a server get the cookie. If the path is "/thingee" only applications on the thingee url get the cookie.
Bill
 
Jeff Sunder
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes I realize that, I was putting that as an example. But why does the Path and MaxAge not get set?
 
Jeff Sunder
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I looked in my Cookie folder and after an exhaustive search, I found that the cookies were being written to my hard drive. So they seem to be persisting at least for a while. Now the question is why can't I retrieve these cookies. The cookies are saved under weird file names on my hard disk. For example, I set three cookies and they were saved with the following file names on my hard drive: 1)servlet[1].txt 2)servlet[3].text 3)for the third one I had tried to set the path of the cookie using the setPath method of the Cookie class and the path of cookie became the name of it.
Does any one know how the cookies are getting named? And why I cannot retrieve these cookies?
 
A day job? In an office? My worst nightmare! Comfort me tiny ad!
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic