This week's book giveaway is in the Cloud/Virtualizaton forum.
We're giving away four copies of Mesos in Action and have Roger Ignazio on-line!
See this thread for details.
Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Why isn't my cookie being set?

 
Katie McCann
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • 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
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13061
6
  • Mark post as helpful
  • send pies
  • 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
  • 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
Rancher
Posts: 13061
6
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Thanks. I'll give that a try and see what happens.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic