Java
  Home arrow Java arrow Page 6 - 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  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
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? 
Google.com  
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: starstarstarstarstar / 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:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log 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


    The JSP Files (part 6): State Of Grace - What's In A Name?
    ( Page 6 of 9 )

    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?

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

       

    JAVA ARTICLES

    - Exception Handling Techniques in Java
    - More About Multithreading in Java
    - The Basics of Multiple Threads in Java
    - Data Access Using Spring Framework JDBC
    - New Object Initialization in Java
    - 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...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 1 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek