Win a copy of Spring Boot in Practice this week in the Spring forum!
  • 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
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

cookie and jsp

 
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I try to test cookie settings in jsp. Something strange result. Here is the code
<%out.println("start");
Cookie cookies[]=request.getCookies();
String sessionid=session.getId();
Cookie SessionCookie=null;
if (cookies==null) {
out.println("<br>Session Cookie added<br>");
SessionCookie = new Cookie("SessionID", sessionid);
SessionCookie.setMaxAge(600*600);
response.addCookie(SessionCookie);
%>
<html><head><title>Test Cookies</title></head>
<body>
<%
} else if (cookies!=null) {
out.println("Cookie exists");
out.println(cookies[0].getValue());
out.println(cookies[0].getMaxAge());
}
%></body></html>
The first time I run it, I got a "array out of bound" error. I refresh it, the cookies information will show. However, the max age is showing as -1. I am not sure whether this cookie is set by the page or not. Also, I can not found the cookie text file on my hard drive.
Thank in advance
 
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
The reason you got that error is that the array can exist but still be empty - try
else if (cookies!=null && cookies.length > 0 ) {
The first time you visit the page, there may be no cookies.
The default age of -1 means the cookie only lasts until the browser is shut down - you need to make the cookie persistent before it will be stored on disk.
Bill

------------------
author of:
 
Cynthia Yao
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks William.
What you said stop the error message.
However, the displayed cookie is not actually set by me, but rather by either the HttpSession or request because when I comment out my create cookie and add cookie part, the cookie information still displays. I wonder whether it is because I use jsp not servlet. Somehow I can not change the cookie maxtime. Has anyone have similiar experience?
Thanks
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic