Java
  Home arrow Java arrow Page 2 - Developing Your Servlet Skills
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

Developing Your Servlet Skills
By: Murach Publishing
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 3 stars3 stars3 stars3 stars3 stars / 4
    2006-09-21

    Table of Contents:
  • Developing Your Servlet Skills
  • Other skills for working with servlets
  • How to code instance variables
  • How to code thread-safe servlets

  • 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


    Developing Your Servlet Skills - Other skills for working with servlets


    (Page 2 of 4 )

    Now that you have a basic understanding of how to code and test a servlet, you’re ready to learn some other skills for working with servlets. To start, you can learn about other methods that are available when you code servlets.

    The methods of a servlet

    Figure 5-6 presents some common methods of the HttpServlet class. When you code these methods, you need to understand that the servlet engine only creates one instance of a servlet. This usually occurs when the servlet engine starts or when the servlet is first requested. Then, each request for the servlet starts (or “spawns”) a thread that can access that one instance of the servlet.

    When the servlet engine creates the instance of the servlet, it calls the init method. Since this method is only called once, you can override it in your servlet to supply any necessary initialization code. In the next figure, you’ll see an example of this.

    After the servlet engine has created the one instance of the servlet, each request for that servlet spawns a thread that calls the service method of the servlet. This method checks the method that’s specified in the HTTP request and calls the appropriate doGet or doPost method.

    When you code servlets, you shouldn’t override the service method. Instead, you should override the appropriate doGet or doPost methods. To handle a request that uses the Get method, for example, you can override the doGet method. If, on the other hand, you want to handle a request that uses the Post method, you can override the doPost method. To handle both types of requests, you can override both of them and have one call the other as shown in figure 5-3.

    If a servlet has been idle for some time or if the servlet engine is shut down, the servlet engine unloads the instances of the servlets that it has created. Before unloading a servlet, though, it calls the destroy method of the servlet. If you want to provide some cleanup code, such as writing a variable to a file or closing a database connection, you can override this method. However, the destroy method may not be called if the server crashes. As a result, you shouldn’t rely on it to execute any code that’s critical to your application.

    Five common methods of a servlet

      public void init() throws ServletException{}

      public void service(HttpServletRequest request,  
                          HttpServletResponse response)
                          throws IOException, ServletException{}

      public void doGet(HttpServletRequest request,
                        HttpServletResponse response)
                        throws IOException, ServletException{}

      public void doPost(HttpServletRequest request, 
                         HttpServletResponse response)
                         throws IOException, ServletException{}

      public void destroy(){}

    How the server handles a request for a servlet


    Figure 5-6.  The methods of a servlet

    The life cycle of a servlet

    1. A server loads and initializes the servlet by calling the init method.
    2. The servlet handles each browser request by calling the service method. This method then calls another method to handle the specific HTTP request type.
    3. The server removes the servlet by calling the destroy method. This occurs either when the servlet has been idle for some time or when the server is shutdown.

    Description

    • All the methods shown above are located in the abstract HttpServlet class. This means you can override these methods in your own servlets. However, you shouldn’t need to override the service method. Rather, you should override a method like doGet or doPost to handle a specific HTTP request. 

    More Java Articles
    More By Murach Publishing


       · This article is an excerpt from the book "Murach's Java Servlets and JSP," published...
     

    Buy this book now. This article is excerpted from chapter five of the book Murach's Java Servlets and JSP, written by Andrea Steelman and Joel Murach (Murach; ISBN: 1890774189). Check it out today at your favorite bookstore. Buy this book now.

    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 3 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek