Java & J2EE Page 6 - The JSP Files (part 6): State Of Grace |
Once you've understood these two fundamental techniques, the rest of thecode should be simple to decipher. If a cookie is found, the countervariable is incremented, and the setValue() method is used to write a newvalue 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 tothe page (or a visit made after a previous cookie has expired); a newcookie 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 thecookie to a number, increment it, and then convert it back to a string forstorage in the cookie. Here's another example, this one a simple form. Enter your name and submitthe form - a cookie will be created containing the name you entered. Whenyou next visit the page, your name will be automatically filled in for you.
This is the initial login form, "form.jsp" - as you can see, it checks forthe presence of a cookie, and uses it to fill in the account username ifavailable. When the form is submitted, "login.jsp" is called to process the dataentered into the form; it will also set cookie attributes appropriately.
Simple, huh?
blog comments powered by Disqus |