Java
  Home arrow Java arrow Page 5 - The JSP Files (part 6): State Of Grace
Dev Shed Forums 
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Sun Developer Network 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Actuate Whitepapers 
Moblin 
VPS Hosting 
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
JAVA

The JSP Files (part 6): State Of Grace
By: Vikram Vaswani and Harish Kamath, (c) Melonfire
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 5
    2001-03-26

    Table of Contents:
  • The JSP Files (part 6): State Of Grace
  • Wasted, Dude!
  • A Few Ground Rules
  • Learning To Write...
  • ...And Read
  • What's In A Name?
  • Plan B
  • Session Dissection
  • Access Denied

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT

    Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here

    The JSP Files (part 6): State Of Grace - ...And Read


    (Page 5 of 9 )

    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 Articles
    More By Vikram Vaswani and Harish Kamath, (c) Melonfire


     

       

    JAVA ARTICLES

    - Adding Images With iTextSharp
    - Adding Columns With iTextSharp
    - Creating Simple PDF Files With iTextSharp
    - The Spring Framework: Understanding IoC
    - Introducing the Spring Framework
    - Java Classes
    - Completing the Syntactic Comparison of Java ...
    - Syntactic Comparison of Java and C/C++
    - Java Statements
    - Conditionals, Expressions and Other Java Ope...
    - Java Operators
    - Primitive Data Types and Basic Language Rule...
    - Java and Object-Oriented Programming
    - Java Beginning Programming
    - Gaming Development Setup




    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway