• 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

setting a cookie in jsp

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

I am trying an exercise on setting cookies. This exercise goes like
this:

"Create a sample home page that accepts a person's name, and birthday
using textfields (use text fields for the name, birth month and birth
day ) and stores that info using a cookie that lasts a year.
Then use the Date and Calendar classes to display a greeting to the
person on his birthday."

I have written the first page that stores the cookies. The relevant
code from this JSP page is as follows -

<H1>What is your Birthday?</H1>

<%
String name = request.getParameter("name");
String month = request.getParameter("month");
String day = request.getParameter("day");

Cookie nameCookie = new Cookie("nameCookie", name);
Cookie monthCookie = new Cookie("monthCookie", month);
Cookie dayCookie = new Cookie("dayCookie", day);

nameCookie.setMaxAge(60*60*24*365);
monthCookie.setMaxAge(60*60*24*365);
dayCookie.setMaxAge(60*60*24*365);

response.addCookie(nameCookie);
response.addCookie(monthCookie);
response.addCookie(dayCookie);
%>

<FORM>
Enter Name: <INPUT TYPE="TEXT" NAME="name">
<br>
Enter month of birth: <INPUT TYPE="TEXT" NAME="month">
<br>
Enter date of birth: <INPUT TYPE="TEXT" NAME="day">
<br>
<INPUT TYPE="SUBMIT" VALUE="Submit">
</FORM>

I have a few questions regarding this code.

1. Is it possible to set 1 cookie instead of 3 cookies as I have done?
2. How should I code the submit button so that it stores the cookie?
Should I give the address of the second page in the ACTION which
confirms submission?
3. What is the ideal approach for this exercise? Right now I am thinking that I am supposed to write 2 pages. One that sets the cookie and then another one that has the logic that check whether there is a cookie with the relevant name and if so then check the value and displays a message if the user's birthday is on that day. Am I right?

Shall be really thankful for advice and guidance.

Ros
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic