setting a cookie in jsp

Discussion in 'Java' started by ros, Apr 15, 2007.

  1. ros

    ros Guest

    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?

    Shall be really thankful for advice and guidance.

    Ros
     
    ros, Apr 15, 2007
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ben
    Replies:
    3
    Views:
    5,862
    Steven Cheng[MSFT]
    Jun 3, 2004
  2. Shapper

    Cookie and Session Cookie Questions.

    Shapper, Apr 27, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    577
  3. =?Utf-8?B?UGF1bA==?=

    Cookie Question (IP as domain and cookie file location)

    =?Utf-8?B?UGF1bA==?=, Jan 10, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    2,488
    Bruce Barker
    Jan 10, 2006
  4. Replies:
    0
    Views:
    4,403
  5. Timur Tabi
    Replies:
    1
    Views:
    128
    Timur Tabi
    May 14, 2004
Loading...

Share This Page