Java
  Home arrow Java arrow Page 7 - An Introduction to Java Servlets
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Dedicated Servers  
Moblin 
JMSL Numerical Library 
IBM® developerWorks 
Sun Developer Network 
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

An Introduction to Java Servlets
By: Nakul Goyal
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 43
    2002-03-11

    Table of Contents:
  • An Introduction to Java Servlets
  • The dark ages
  • Servlets to the rescue!
  • The servlet runtime environment
  • Servlet interface and life cycle
  • Request and response objects
  • Persistent and Shared Data
  • ServletContext attributes
  • Request attributes and resources
  • Conclusion

  • 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


    An Introduction to Java Servlets - Persistent and Shared Data


    (Page 7 of 10 )

    One of the more interesting features of the Servlet API is its support for persistent data. Since a servlet stays loaded between requests and all servlets are loaded in the same process, it's easy to remember information from one request to another and to let different servlets share data.

    The Servlet API contains a number of mechanisms to support this directly. We'll look at some of them in detail below. Another powerful way to handle shared data is by using a singleton object.

    Session Tracking

    An HttpSession class was introduced in the 2.0 version of the Servlet API. Instances of this class can hold information for one user session between requests. You start a new session by requesting an HttpSession object from the HttpServletRequest in your doGet or doPost method:

    HttpSession session = request.getSession(true);

    This method takes a boolean argument. If true is passed as the argument then a new session will be started if one doesn't exist. If false is passed as the argument then only an existing session is returned. The HttpSession object is unique for one user session only. The Servlet API supports two ways to associate multiple requests with a session: cookies and URL rewriting.

    If cookies are used, a cookie with a unique session ID is sent to the client when the session is established. The client then includes the cookie in all subsequent requests so the servlet engine can figure out which session the request is associated with. URL rewriting is intended for clients that don't support cookies or when the user has disabled cookies.

    With URL rewriting the session ID is encoded in the URLs that your servlet sends to the client. When the user clicks on an encoded URL, the session ID is sent to the server where it can be extracted and have the client request associated with the correct session. To use URL rewriting you must make sure all URLs that you send to the client are encoded with the encodeURL or encodeRedirectURL methods in HttpServletResponse.

    An HttpSession can store any type of object. A typical example is a database connection allowing multiple requests to be part of the same database transaction, or information about purchased products in a shopping cart application so that the user can add items to their cart while browsing through the site. To save an object in an HttpSession you use the putValue method:

    ...

    Connection con = driver.getConnection(databaseURL, user, password);

    session.putValue("myappl.connection", con);

    ...


    In another servlet or the same servlet processing another request, you can get the object with the getValue method:

    ...

    HttpSession session = request.getSession(true);

    Connection con = (Connection)

    session.getValue("myappl.connection");

    if (con != null) {

    // Continue the database transaction

    ...


    You can explicitly terminate (invalidate) a session with the invalidate method or let it be timed-out by the servlet engine automatically. The session times out if no request associated with the session is received within a specified interval. Most servlet engines allow you to specify the length of the interval through a configuration option. In the 2.1 version of the Servlet API there's also a setMaxInactiveInterval so you can adjust the interval to meet the needs of each individual application.

    More Java Articles
    More By Nakul Goyal


     

    JAVA ARTICLES

    - Deploying Multiple Java Applets as One
    - Deploying Java Applets
    - Understanding Deployment Frameworks
    - Database Programming in Java Using JDBC
    - Extension Interfaces and SAX
    - Entities, Handlers and SAX
    - Advanced SAX
    - Conversions and Java Print Streams
    - Formatters and Java Print Streams
    - Java Print Streams
    - Wildcards, Arrays, and Generics in Java
    - Wildcards and Generic Methods in Java
    - Finishing the Project: Java Web Development ...
    - Generics and Limitations in Java
    - Getting Started with Java Web Development in...







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