Article
The JSP Files - Parts 1 to 8: Tagged and Bagged
Page: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 Next
...And Read
So that takes care of writing a cookie - but how about reading it? Here's the code.
<%
// declare some variables
Cookie cookieCounter = null;
// the cookie you want
String cookieName = "counter";
int cookieFound = 0;
// a few more useful variables
String tempString;
int count=0;
// get an array of all cookies available on client
Cookie[] cookies = request.getCookies();
// iterate through array looking for your cookie
for(int i=0; i<cookies.length; i++)
{
cookieCounter = cookies[i];
if (cookieName.equals(cookieCounter.getName()))
{
cookieFound = 1;
break;
}
}
%>
Before you can read the cookie, you need to find it on the client's hard drive. Since JSP does not currently allow you to directly locate and identify the cookie by name, you need to iterate through all available cookies until you find the one you're looking for. In the example above, the "for" loop does just that; if and when it finds the cookie, it sets the "cookieFound" variable to 1 and breaks out of the loop.
At this point, the cookie is stored in the Cookie object "cookieCounter". You can then use the getValue() object method to get the current value of the cookie variable, and use it in your script.
<%
// if found
if(cookieFound == 1)
{
// get the counter value as string
tempString = cookieCounter.getValue();
// convert it to a number
count = Integer.parseInt(tempString);
// increment it
count++;
// back to a string
tempString = Integer.toString(count);
// store it in the cookie for future use
cookieCounter.setValue(tempString);
// set some other attributes
cookieCounter.setMaxAge(300);
cookieCounter.setPath("/");
// send cookie to client
response.addCookie(cookieCounter);
}
%>
What's In A Name?
Once you've understood these two fundamental techniques, the rest of the code should be simple to decipher. If a cookie is found, the counter variable is incremented, and the setValue() method is used to write a new value to the cookie; this counter variable is then displayed on the page. If a cookie is not found, it implies that this is the user's first visit to the page (or a visit made after a previous cookie has expired); a new cookie is set and an appropriate message displayed.
Again, since this example deals with numbers rather than strings, innumerable contortions are required to convert the string value in the cookie to a number, increment it, and then convert it back to a string for storage in the cookie.
Here's another example, this one a simple form. Enter your name and submit the form - a cookie will be created containing the name you entered. When you next visit the page, your name will be automatically filled in for you.
<%
// form.jsp
// declare some variables
Cookie thisCookie = null;
// the cookie you want
String cookieName = "username";
int cookieFound = 0;
String username = "";
// get an array of all cookies available on client
Cookie[] cookies = request.getCookies();
// iterate through array looking for your cookie
for(int i=0; i<cookies.length; i++)
{
thisCookie = cookies[i];
if (cookieName.equals(thisCookie.getName()))
{
cookieFound = 1;
break;
}
}
// if found
if(cookieFound == 1)
{
// get the counter value
username = thisCookie.getValue();
}
%>
<html>
<head>
<basefont face="Arial">
</head>
<body>
<form action="login.jsp" method="post">
<table>
<tr>
<td>Your name</td>
<td><input type=text name=username value="<%=
username %>"> <input type="Submit" value="Click
me"></td> </tr>
</table>
</form>
</body>
</html>
This is the initial login form, "form.jsp" - as you can see, it checks for the presence of a cookie, and uses it to fill in the account username if available.
When the form is submitted, "login.jsp" is called to process the data entered into the form; it will also set cookie attributes appropriately.
<%
// login.jsp
// get values from form
String username = request.getParameter("username");
// create a new cookie to store the username
Cookie alpha = null;
alpha = new Cookie("username", username);
alpha.setMaxAge(300);
alpha.setPath("/");
// send it to client
response.addCookie(alpha);
%>
<html>
<head>
<basefont face="Arial">
</head>
<body>
Get lost, <b><%= username %></b>!
</body>
</html>
Simple, huh?
Copyright Melonfire, 2000. All rights reserved.