SET AS HOME PAGE

ADD TO FAVORITES / BOOKMARK THIS WEBSITE (More Details)

Introduction

Servlet

Jsp

Security

Enterprise Beans

Contact Us


Introduction to Java Servlets (Continue...)

 Introduction

A servlet is an object that extends either the javax.servlet.GenericServlet class or the javax.servlet.http.HttpServlet class. The javax.servlet.GenericServlet class defines methods for building generic, protocol-independent servlets. The javax.servlet.http.HttpServlet class extends this class to provide HTTP-specific methods.


 Servlet Class Hierarchy

The following figure shows the high level design of the hierarchy of interfaces and classes in javax.servlet and javax.servlet.http packages:


  • The Servlet Class Hierarchy consists of two top level interfaces which are implemented by the GenericServlet class:
    • javax.servlet.Servlet
    • javax.servlet.ServletConfig
  • The GenericServlet class is extended by the HttpServlet class which in turn is extended by a user defined class.
  • The Servlet Life Cycle includes:
    • Initialization of servlet instance using the init() method.
    • Servicing a client request using the service() method.
    • Destroying a servlet instance using the destroy() method.
 The init() Method

The following code snippet shows the init() method:
public void init (ServletConfig config) throws ServletException
 The service() Method

The following code snippet shows the service() method:
public void service(ServletRequest req, ServletResponse res) throws ServletException,IOException
The service() method dispatches a client request to one of the request handler methods of the HttpServlet interface, such as the doGet(), doPost(), doHead(), or doPut(). The request handler methods accept the objects of the HttpServletRequest and HttpServletResponse as parameters from the service() method.
 The doGet() Method

When you type a URL on the address bar of your browser to view a static Web page, the browser uses the GET method to send the request.

 The doPost() Method

Unlike the GET method, the POST request sends the data as part of the HTTP request body. As a result, the data sent does not appear as a part of the URL.

 The doHead() Method

The doHead() method handles requests sent using the HTTP HEAD method. Similar to the GET method, the HEAD method also sends requests to the server. The only difference between the GET and the HEAD methods is that the HEAD method returns the header of the response, which contains entries, such as Content-Type, Content-Length, and Last-Modified.

 The doPut() Method

The doPut() method handles requests sent using the HTTP PUT method. The PUT method allows a client to store information on the server. For example, you can use the PUT method to post an image file to the server.

 The destroy() Method

The Web container calls the destroy() method when:
  • The time period specified for the servlet has elapsed. The time period of a servlet is the period for which the servlet is kept in the active state by the Web container to service the client request.
  • The Web container needs to release servlet instances to conserve memory.
  • The Web container is about to shut down.
 Basic Syntex

public void init(ServletConfig config) throws ServletException { … … … }
public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException { … … … }
public void destroy() { … … … }
}
public class HttpServletExample extends HttpServlet {
public void init(ServletConfig config) throws ServletException { … … … }
public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { … … … }
public void destroy() { … … … }
}
 Structure of a Java Web application

The following figure shows the standard packaging structure of a Java Web application:


To create a WAR file, type the following command at the command prompt:
jar cvf <war filename>
This creates the .war file that you can deploy in the application server.
 Access the Servlet from a Browser

The syntax of the URL that you use to access the servlet is:
http://<computer_name>:<portnumber>/<context root>/<Servlet-name>
For example to access the servlet, SampleServlet with serv_app as the context root, you can use the following URL:
http://192.168.0.52:8080/serv_app/SampleServlet

 Click for Next Topic
<- PREVIOUSNEXT ->