473,386 Members | 1,785 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

How to set more than one attribute in same cookie?

17
I try to search information from many websites, but what i can found is they only demonstrate the example with ONE ATTRIBUTE in a Cookie only.

What i want is how to set more than 1 attribute in a same cookie?

Below is my code: currently i only can show the username but cannot show the age. How to enable the ShowCookie.jsp able to show both the attribute of "username" and "age" enter by User?

GetValue.jsp
Expand|Select|Wrap|Line Numbers
  1. <%@ page language="java" %>
  2. <html>
  3. <head><title>Get username & age</title></head>
  4. <body bgcolor="cyan">
  5. <table>
  6. <form method="post" action="http://localhost:8080/SetCookie.jsp">
  7.     <tr>
  8.         <td>Enter Your Name:</td>
  9.         <td><input type="text" name="username" length="20" maxlength="20"></td>
  10.     </tr>
  11.     <tr>
  12.         <td>Enter Your Age:</td>
  13.         <td><input type="text" name="age" length="3" maxlength="3"></td>
  14.     </tr>
  15.     <tr>
  16.         <td colspan="2"><center><input type="submit" value="Submit"></center></td>
  17.     </tr>
  18. </form>
  19. </table>
  20. </body>
  21. </html>
SetCookie.jsp
Expand|Select|Wrap|Line Numbers
  1. <%@ page language="java" %>
  2. <%
  3.     String username=request.getParameter("username");
  4.     String age=request.getParameter("age");
  5.     if(username == null) username = "";
  6.     if(age = null) age = "";
  7.  
  8.     Cookie usernameCookie = new Cookie("username",username);
  9.     usernameCookie.setMaxAge(7*24*60*60);
  10.     response.addCookie(usernameCookie);
  11.  
  12.     Cookie ageCookie = new Cookie("age",age);
  13.     ageCookie.setMaxAge(7*24*60*60);
  14.     response.addCookie(ageCookie);
  15. %>
  16.  
  17. <html>
  18. <head><title>Set Cookie</title></head>
  19. <body bgcolor="cyan">
  20. <a href="http://localhost:8080/ShowCookie.jsp">Show cookie value</a>
  21. </body>
  22. </html>
ShowCookie.jsp
Expand|Select|Wrap|Line Numbers
  1. <%@ page language="java" %>
  2. <%
  3.     String cookieName = "username";
  4.     String cookieAge = "age";
  5.  
  6.     Cookie cookies [] = request.getCookies();
  7.     Cookie usernameCookie = null;
  8.     Cookie ageCookie = null;
  9.  
  10.     if(cookies != null)
  11.     {
  12.         for(int i=0; i<cookies.length; i++)
  13.         {
  14.             if(cookies [i].getName().equals(cookieName))
  15.             {
  16.                 usernameCookie = cookies [i];
  17.                 break;
  18.             }
  19.  
  20.             if(cookies [i].getName().equals(cookieAge))
  21.             {
  22.                 ageCookie = cookies [i];
  23.                 break;
  24.             }
  25.         }
  26.     }
  27. %>
  28.  
  29. <html>
  30. <head><title>Show Cookie</title></head>
  31. <body bgcolor="cyan">
  32. <%
  33.     if(usernameCookie == null)
  34.     {
  35.         out.println("Hello, World!");
  36.     }
  37.      else 
  38.      {
  39.         out.println("Hello, " + usernameCookie.getValue() + "!");
  40.     }
  41. %>
  42.  
  43. <%
  44.     if(ageCookie == null)
  45.     {
  46.         out.println("You forgot enter your age!");
  47.     }
  48.     else
  49.     {
  50.         out.println("Your age is: " + ageCookie.getValue() + "!");
  51.     }
  52. %>
  53. </body>
  54. </html>
Oct 27 '07 #1
0 1378

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Johnny | last post by:
Searched on google for any info relating to this before posting here but found none. I set up a web service using nusoap on apache php 4.3.8 on windows with error_reporting = E_ALL and had that...
15
by: SFX | last post by:
If I have a session ID (string) can I somehow obtain the session object associated to that ID (it exist of course) ? I know this sounds wicked but I have a situation in which I have to make a...
0
by: Denver Developer | last post by:
I have a cookie that is set by a previous ASP page that I wish to update with the following logic. MyCookieColl = Request.Cookies; for (loop1 = 0; loop1 < MyCookieColl.Count ; loop1++) {...
161
by: KraftDiner | last post by:
I was under the assumption that everything in python was a refrence... so if I code this: lst = for i in lst: if i==2: i = 4 print lst I though the contents of lst would be modified.....
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.