Home arrow Java & J2EE arrow Page 6 - The JSP Files (part 6): State Of Grace

What's In A Name? - Java

Now that you've mastered the basics, it's time to bring out thebig iron. This week, The JSP Files explores the various techniquesavailable to "maintain state" on a JSP-based Web site. Learn about theCookie and Session objects, find out how to build a cookie-based hitcounter, and read about a simple yet effective way of protecting sensitiveWeb pages with the Session object.

TABLE OF CONTENTS:
  1. The JSP Files (part 6): State Of Grace
  2. Wasted, Dude!
  3. A Few Ground Rules
  4. Learning To Write...
  5. ...And Read
  6. What's In A Name?
  7. Plan B
  8. Session Dissection
  9. Access Denied
By: Vikram Vaswani and Harish Kamath, (c) Melonfire
Rating: starstarstarstarstar / 5
March 26, 2001

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement
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.

<% // 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 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.

<% // 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?

 
 
>>> More Java & J2EE Articles          >>> More By Vikram Vaswani and Harish Kamath, (c) Melonfire
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

JAVA & J2EE ARTICLES

- More Java Bugs Lead to More Attacks
- Oracle's Java One Brings News, Surprises
- Oracle Patches Java Runtime Environment
- Apple Syncs Java Update with Oracle
- Spring 3.1 Java Development Framework Compat...
- Jelastic Java PaaS Availability and Pricing ...
- NetBeans 7.1 Released, Supports JavaFX 2
- SolarWinds Releases Newest Version of Java M...
- Free Monitoring Tool for Java Apps on Heroku
- Heroku Adds JCloud Platform Support, Java 7 ...
- Java SE 8 Speculation in Full Swing
- Java SE 7 Now Available
- New JVM Language and Java Reporting Tool
- Java 7 Release Update and New Eclipse Toolkit
- The Best Java Netbeans IDE Plugins

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: