Search This Blog

Tuesday, 8 November 2011

Basic Architecture of a Servlet!

0 comments

THE BASIC SERVLET ARCHITECTURE! 
         
          A Servlet, in its most general form, is an instance of a class which implements the javax.servlet.Servlet interface. Most Servlets, however, extend one of the standard implementations of that interface, namely javax.servlet.GenericServlet and javax.servlet.http.HttpServlet. The set of all classes and interfaces required for developing and running a Servlet is called Servlet API.

The Servlet API is a part of the Servlet specification designed by Sun Microsystems. This API is supported by all Servlet containers, such as Tomcat and Weblogic. The API contains classes and interfaces that define a standard contract between the Servlet class and Servlet container (or Servlet engine). These classes and interfaces are packaged in the following two packages of the Servlet API:
  • javax.servlet &
  • javax.servlet.http
These packages contain classes and interfaces that allow the Servlet to access the basic services provided by the Servlet container. The javax.servlet package contains classes and interfaces to support generic, protocol-independent Servlet. These classes are extended by the classes in javax.servlet.http package to add HTTP-specific functionality.
Every Servlet must implement the javax.servlet.Servlet interface. Most servlets implement this interface by extending one of the two special classes given below:
  • javax.servlet.GenericServlet
  • javax.servlet.http.HTTPServlet
HTTPServlet is a sub class of GenericServlet super class and adds HTTP functionality to a Servlet.
Servlet - The Root Interface of Servlets
Architecturally, all servlets must implement the Servlet interface of package javax.servlet. It provides five methods, which are executed automatically by the web server. There is no main() method in a servlet just like an applet. The methods described in this interface are as follows:
Method
Description
void init(servletConfig config)
This method is automatically called once during a servlet’s execution cycle to initialize the servlet.
ServletConfig getServletConfig()
This method returns a reference to the object that implements the interface ServletConfig. It provides access to the servlet’s configuration information.
String getServletInfo()
This method is defined by a servlet programmer to return a string containing servlet information such as the servlet’s author and version.
void Service(ServletRequest request, ServletResponse response)
This method is called one or more times by the server to respond based on the number of requests made by the clients simultaneously.
void destroy()
This “cleanup” method is called when a servlet is terminated by its servlet container. Resources used by the servlet, such as an open file or an open database connection should be de-allocated here.

A servlet’s life cycle begins when the servlet container loads the servlet into memory – normally, in response to the first request that the servlet receives. Before the servlet can handle the request, the servlet container invokes the servlet’s init method for initializing the servlet's variables and resources. Then the servlet’s service() method is called as a response to the client’s request.
All requests are handled by the servlet’s service method, which receives the request, processes the request, and sends a response to the client in the form of html. During the servlet’s life time, method service is called one per request. Each new request typically results in a new thread of execution (created by the servlet container) in which method service gets executed. When the servlet container terminates the servlet, the servlet’s destroy method is called to release servlet resources.
Contents of javax.servlet Package:
The classes and interfaces defined in the javax.servlet package are listed below. The Servlet container provides the implementation of these classes and interfaces.
Interfaces: Servlet, RequestDispatcher, ServletConfig, ServletContext, ServletContextListener, ServletRequest, ServletResponse, SingleThreadModel, Filter, FilterChain, FilterConfig, ServletContextAttributeListener, ServletRequestAttributeListener, SerlvetRequestListener
Classes: ServletContextEvent, ServletInputStream, ServletOutputStream, ServletRequestWrapper, ServletResponseWrapper, GenericServlet, ServletContextAttributeEvent, ServletRequestAttributeEvent, ServletRequestEvent, ServletException, UnavailableException
Contents of javax.servlet.http Package:
The javax.servlet.http package supports the development of Servlets that make use of HTTP protocol. The classes in this package extend the Servlet functionality to support HTTP specific features that include request and response headers, support for different request methods and cookies. The classes and interfaces defined in javax.servlet.http package are given below:
Type
Name
Description
Interface HttpServletRequest Extends the ServletRequest interface to provide request information for HTTP servlets
Interface HttpServletResponse Extends the ServletResponse interface to provide HTTP-specific functionality in sending a response
Interface HTTPSession Provides a way to identify the user across more than one page request or visit to a website and store the information about the user
Interface HttpSessionBindingListener Causes an object to be notified when it is bound to or unbound from a session.
Class Cookie Creates a cookie, a small amount of information sent by a Servlet to a web browser, saved by the browser, and later sent back to the server
Class HttpServlet Provides an abstract class to be sub classed to create an HTTP Servlet suitable for a website.
Class HttpSessionBindingEvent Sent to an object that implements HttpSessionBindingListener when the object is bound to or unbound from the session.
Class HttpUtils Provides a collection of methods that are useful in writing HTTP servlets


Leave a Reply