SunQuest
 
       Java
  Home arrow Java arrow Page 8 - 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 
VeriSign Whitepapers 
VPS Hosting 
Weekly Newsletter

 
Developer Updates  
Free Website Content 
IBM developerWorks
 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 - Session Dissection


    (Page 8 of 9 )

    Creating a JSP session is much simpler than writing a cookie. Todemonstrate this, here's the session equivalent of the cookie-based counteryou saw a few pages back.

    <html> <head> </head> <body> <% // get the value of the session variable Integer visits = (Integer)session.getValue("counter"); // if null if (visits == null) { // set it to 0 and print a welcome message visits = new Integer(0); session.putValue("counter", visits); out.println("Welcome, stranger!"); } else { // else increment and write the new value visits = new Integer(visits.intValue() + 1); session.putValue("counter", visits); out.println("You have visited this page " + visits + " time(s)! Don't you have anything else to do, you bum?! "); } %> </body> </html>

    There isn't much you have to do to create a session - simply use theputValue() method of the Session object to create one or more sessionvariable, and JSP will automatically create a session and register thevariables. You can then use the getValue() method to retrieve the values ofthe session variables automatically.

    An important point to be noted here is that it is necessary to typecast thesession variable while using getValue() - in the example above, we'vespecifically stated the type of the variable in parentheses beforeassigning it to a regular JSP variable. Since JSP allows you to bindobjects to the session, you can bind an Integer object and thereby bypasssome of the string-to-number conversion routines in the equvalent cookieexample.

    With this information in mind, the example above becomes much simpler toread. An "if" statement is used to take care of the two possiblealternatives: a first-time visitor (no prior session) or a returningvisitor (pre-existing session). Depending on whether or not the "counter"variable exists, appropriate action is taken.

    The Session object also comes with a bunch of other interesting methods -here are some of them:

    getId() - returns a string containing the unique session ID

    setMaxInactiveInterval(someSeconds) - keeps the session active forsomeSeconds duration after the last client request

    invalidate() - destroy the session

    getAttribute() and setAttribute() - try these is getValue() and putValue()don't work

    getCreationTime() - returns the time at which this session was created, inseconds, as an offset from midnight January 1 1970

    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