• 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

Why isn't my cookie being set?

 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Another question about cookies (this is my first time using them and I'm struggling a bit). My code (and output from the logs) is below. The code is part of a voting system, and we only want someone to vote once a day- so we check if they already have the cookie, and if not, set the cookie and process the vote. It seems that my cookie is never being set- no matter how many times I run the servlet, it only retrieves 1 cookie, and enters into the block to set the cookie (evidenced by the output in the logs)- and I can't find the cookie on my hard drive. Anyway, I am completely stumped and would welcome any input.
--------------
Source Code:
(in doPost method):
Cookie [] cookies = null;
cookies = request.getCookies();
if (cookies != null)
{
System.out.println("Length of cookie array= " + cookies.length);
for (int i=0; i< cookies.length; i++)
{
System.out.println("Cookie " + i + " name= " + cookies[i].getName());
System.out.println("Cookie " + i + " value= " +
cookies[i].getValue());
String name= cookies[i].getName();
if (name.equals("voting"))
{
cookieExists= true;
break;
}
}
}
if (!cookieExists)
{
setCookie(request,response);
... rest of servlet code
}
private void setCookie(HttpServletRequest request,
HttpServletResponse response)
{
Cookie votingCookie = new Cookie ("voting", "voted");
Calendar now= new GregorianCalendar();
Calendar tonight= new GregorianCalendar();
Date time= new Date();
now.setTime(time);
tonight.set(now.get(now.YEAR), now.get(now.MONTH),
now.get(now.DATE), 23,59);
long nowSeconds= (now.get(now.HOUR_OF_DAY) * 3600) +
(now.get(now.MINUTE) * 60);
long tonightSeconds= (tonight.get(tonight.HOUR_OF_DAY) * 3600) + (tonight.get(tonight.MINUTE) * 60);
System.out.println("Num seconds= " + ((int)(tonightSeconds - nowSeconds)) );
votingCookie.setMaxAge((int)(tonightSeconds - nowSeconds));
response.addCookie(votingCookie);
System.out.println("COokie added, name= " + votingCookie.getName());
}
-------
Relevant Standard output:
Length of cookie array= 1 (I always only get this one cookie)
Cookie 0 name= jrunsessionid
Cookie 0 value= =974499644521165506
NumSeconds= 42014 (or some other number that represents the number of seconds until midnight tonight)
Cookie added, name= voting
 
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
That sure looks like it ought to work. The only thing I can think to check - are you doing all of the cookie setting before any output has been written to the response?
Bill

------------------
author of:
 
Katie McCann
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, I'm actually not writing any more output to the response. The servlet is forwarding the request/response to a JSP. Basically, it's a voting system- the user clicks on the vote button, the servlet checks for a cookie (ie have they already voted), if not, updates the database and record the vote, and then forwards to a JSP that displays the voting results. Could something with this setup be causing the response not to write the cookie?
 
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
"Well, I'm actually not writing any more output to the response. The servlet is forwarding the request/response to a JSP."
I think there was a thread about this on the Tomcat mailing list the other day. As I recall, when you forward like that to a JSP, anything in the response gets cleared out!! So thats what happened to your cookies. Probably you should include the JSP rather than forwarding.
I snipped this from a message on that mailing list - it is an
example of including a JSP. The author was kedar@cysphere.com (Kedar Choudary)
----------------
public class Controller extends HttpServlet {
public void service(ServletRequest req, ServletResponse res) throws
ServletException, IOException {
res.setContentType("text/html");
PrintWriter out = res.getWriter();
out.println("<h1>This is from Contrller</h1>\n");
out.flush();
ServletContext ctx = getServletContext();
RequestDispatcher rd = ctx.getNamedDispatcher("jsp");
rd.include(req, res);
}
}
Warning - I have not tried this.
Bill
 
Katie McCann
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks. I'll give that a try and see what happens.
reply
    Bookmark Topic Watch Topic
  • New Topic