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

...And Read - 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
So that takes care of writing a cookie - but how about reading it? Here'sthe 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 harddrive. Since JSP does not currently allow you to directly locate andidentify the cookie by name, you need to iterate through all availablecookies 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 ofthe 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); } %>


 
 
>>> 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: