Differences between Generic and Http Servlet javax.servlet.GenericServlet Signature: public abstract class GenericServlet extends java.lang.Object implements Servlet , ServletConfig , java.io.Serializable
javax.servlet.GenericServlet Signature: public abstract class GenericServlet extends java.lang.Object implements Servlet , ServletConfig , java.io.Serializable GenericServlet defines a generic, protocol-independent servlet . GenericServlet uses service() method to handle request GenericServlet gives a blueprint and makes writing servlet easier. GenericServlet provides simple versions of the lifecycle methods init and destroy and of the methods in the ServletConfig interface. GenericServlet implements the log method, declared in the ServletContext interface. To write a generic servlet , it is sufficient to override the abstract service method.
javax.servlet.http.HttpServlet Signature: public abstract class HttpServlet extends GenericServlet implements java.io.Serializable http is protocol dependent HttpServlet defines a HTTP protocol specific servlet . HttpServlet uses doGet and doPost methods to handle requests. HttpServlet gives a blueprint for Http servlet and makes writing them easier. HttpServlet extends the GenericServlet and hence inherits the properties GenericServlet .
Package javax.servlet.http Interface Summary HttpServletRequest Extends the ServletRequest interface to provide request information for HTTP servlets . HttpServletResponse Extends the ServletResponse interface to provide HTTP-specific functionality in sending a response. HttpSession Provides a way to identify a user across more than one page request or visit to a Web site and to store information about that user. HttpSessionActivationListener Objects that are bound to a session may listen to container events notifying them that sessions will be passivated and that session will be activated. HttpSessionAttributeListener This listener interface can be implemented in order to get notifications of changes to the attribute lists of sessions within this web application. HttpSessionBindingListener Causes an object to be notified when it is bound to or unbound from a session. HttpSessionContext Deprecated. As of Java(tm) Servlet API 2.1 for security reasons, with no replacement. HttpSessionListener Implementations of this interface are notified of changes to the list of active sessions in a web application.
Class Summary 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. HttpServlet Provides an abstract class to be subclassed to create an HTTP servlet suitable for a Web site. HttpServletRequestWrapper Provides a convenient implementation of the HttpServletRequest interface that can be subclassed by developers wishing to adapt the request to a Servlet. HttpServletResponseWrapper Provides a convenient implementation of the HttpServletResponse interface that can be subclassed by developers wishing to adapt the response from a Servlet. HttpSessionBindingEvent Events of this type are either sent to an object that implements HttpSessionBindingListener when it is bound or unbound from a session, or to a HttpSessionAttributeListener that has been configured in the deployment descriptor when any attribute is bound, unbound or replaced in a session. HttpSessionEvent This is the class representing event notifications for changes to sessions within a web application. HttpUtils Deprecated. As of Java(tm) Servlet API 2.3.
Generic Servlet javax.servlet Class GenericServlet java.lang.Object javax.servlet.GenericServlet All Implemented Interfaces: java.io.Serializable , Servlet , ServletConfig
Servlet Life Cycle A servlet is basically a small Java program that runs within a Web server . It can receive requests from clients and return responses.
The whole life cycle of a servlet breaks up into 3 phases: • Initialization: A servlet is first loaded and initialized usually when it is requested by the corresponding clients. • Service: After initialization, the servlets serve clients on request, implementing the application logic of the web application they belong to. • Destruction: When all pending requests are processed and the servlets have been idle for a specific amount of time, they may be destroyed by the server and release all the resources they occupy.
More specifically, the behavior of a servlet is described in javax.servlet.Servlet interface , in which the following methods are defined: • 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. The init method has a ServletConfig parameter. 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. • 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 . Once a servlet is loaded, it remains in the server’s memory as a single object instance. • public void destroy() This method is called once just before the servlet is unloaded and taken out of service.
The following servlet presents information about how many times it has been accessed: import java.io.*; import javax.servlet .*; import javax.servlet.http .*; public class SimpleCounter extends HttpServlet { int count = 0; public void doGet ( HttpServletRequest req , HttpServletResponse res) throws ServletException , IOException { res.setContentType ("text/plain"); PrintWriter out = res.getWriter (); count++; out.println ("Since loading, this servlet has been accessed " + count + " times."); } }
import java.io.*; import javax.servlet .*; public class HelloServlet extends GenericServlet { public void init ( ServletConfig config ) { super.init ( config ); } public void service ( ServletRequest req , ServletResponse res ) throws ServletException , IOException { PrintStream out = new PrintStream ( res.getOutputStream ( ) ); out.println ( "Hello, World!" ); } public void destroy ( ) { super.destroy ( ); } }