Java
  Home arrow Java arrow Page 8 - 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 - ServletContext attributes


    (Page 8 of 10 )

    All servlets belong to one servlet context. In version 1.0 and 2.0 of the servlet API, all servlets on one host belong to the same context, but with version 2.1 of the API, the context becomes more powerful and can be seen as the humble beginnings of an Application concept. Future versions of the API will make this even more apparent.

    Many servlet engines implementing the Servlet 2.1 API let you group a set of servlets into one context and support more than one context on the same host. The ServletContext in the 2.1 API is responsible for the state of its servlets and knows about resources and attributes available to the servlets in the context. Here we will only look at how ServletContext attributes can be used to share information among a group of servlets.

    There are three ServletContext methods dealing with context attributes: getAttribute, setAttribute and removeAttribute. In addition, the servlet engine may provide ways to configure a servlet context with initial attribute values. This serves as a welcome addition to the servlet initialization arguments for configuration information used by a group of servlets, for instance the database identifier we talked about above, a style sheet URL for an application, the name of a mail server, etc.

    A servlet gets a reference to its ServletContext object through the ServletConfig object. The HttpServlet actually provides a convenient method (through its superclass, GenericServlet) named getServletContext to make it really easy:

    ...

    ServletContext context = getServletContext();

    String styleSheet = request.getParameter("stylesheet");

    if (styleSheet != null) {

    // Specify a new style sheet for the application

    context.setAttribute("stylesheet", styleSheet);

    }

    ...


    The code above could be part of an application configuration servlet, processing the request from an HTML FORM where a new style sheet can be specified for the application. All servlets in the application that generate HTML can then use the style sheet attribute like this:

    ...

    ServletContext context = getServletContext();

    String styleSheet = context.getAttribute("stylesheet");

    out.println("<HTML><HEAD>");

    out.println("<LINK HREF=" + styleSheet + " TYPE=text/css REL=STYLESHEET>");

    ...

    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 6 hosted by Hostway