Java
  Home arrow Java arrow Page 5 - 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  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
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 / 45
    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 - Servlet interface and life cycle


    (Page 5 of 10 )

    Let's implement our first servlet. A servlet is a Java class that implements the servlet interface. This interface has three methods that define the servlet's life cycle:
    • public void init(ServletConfig config) throws ServletException: This method is called once when the servlet is loaded into the servlet engine, before the servlet is asked to process its first request.
    • public void service(ServletRequest request, ServletResponse response) throws ServletException, IOException: This method is called to process a request. It can be called zero, one or many times until the servlet is unloaded. Multiple threads (one per request) can execute this method in parallel, so it's thread safe.
    • public void destroy(): This method is called once just before the servlet is unloaded and taken out of service.
    The init method has a ServletConfig attribute. The servlet can read its initialization arguments through the ServletConfig object. How the initialization arguments are set is servlet engine dependent, but they are usually defined in a configuration file.

    A typical example of an initialization argument is a database identifier. A servlet can read this argument from the ServletConfig at initialization and then use it later to open a connection to the database during processing of a request:

    ...

    private String databaseURL;

    public void init(ServletConfig config) throws ServletException {

    super.init(config);

    databaseURL = config.getInitParameter("database");

    }


    The servlet API is structured so that servlets implementing a protocol other than HTTP are also possible. The javax.servlet package contains interfaces and classes that are intended to be protocol independent and the javax.servlet.http package contains HTTP specific interfaces and classes. Since this is just an introduction to servlets, I will ignore this distinction here and focus on HTTP servlets. Our first servlet, named ReqInfoServlet, will therefore extend a class named HttpServlet. HttpServlet is part of the JSDK and implements the Servlet interface plus a number of other convenience methods. We define our class like this:

    import javax.servlet.*;

    import javax.servlet.http.*;

    public class ReqInfoServlet extends HttpServlet {

    ...

    }


    The important set of methods in HttpServlet are those that specialize the service method in the servlet interface. The implementation of service in HttpServlet looks at the type of request it's asked to handle (GET, POST, HEAD, etc.) and calls a specific method for each type. By doing this, the servlet developer is relieved from handling the details about obscure requests like HEAD, TRACE and OPTIONS, and can focus on taking care of the more common request types such as GET and POST. In this first example we will only implement the doGet method.

    protected void doGet(HttpServletRequest request, HttpServletResponse response)

    throws ServletException, IOException {

    ...

    }

    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-2009 by Developer Shed. All rights reserved. DS Cluster 1 Hosted by Hostway
    Stay green...Green IT