9. Differences between Generic and Http
Servlet
• javax.servlet.GenericServlet
Signature: public abstract class GenericServlet
extends java.lang.Object implements
Servlet, ServletConfig, java.io.Serializable
10. 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.
11. 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.
12. Package javax.servlet.http
Interface Summary
Extends the ServletRequest interface to provide request
HttpServletRequest
information for HTTP servlets.
Extends the ServletResponse interface to provide HTTP-
HttpServletResponse
specific functionality in sending a response.
Provides a way to identify a user across more than one page
HttpSession request or visit to a Web site and to store information about
that user.
Objects that are bound to a session may listen to container
HttpSessionActivationListener events notifying them that sessions will be passivated and
that session will be activated.
This listener interface can be implemented in order to get
HttpSessionAttributeListener notifications of changes to the attribute lists of sessions
within this web application.
Causes an object to be notified when it is bound to or
HttpSessionBindingListener
unbound from a session.
Deprecated. As of Java(tm) Servlet API 2.1 for security
HttpSessionContext
reasons, with no replacement.
Implementations of this interface are notified of changes to
HttpSessionListener
the list of active sessions in a web application.
13. Class Summary
Creates a cookie, a small amount of information sent by a servlet
Cookie to a Web browser, saved by the browser, and later sent back to
the server.
Provides an abstract class to be subclassed to create an HTTP
HttpServlet
servlet suitable for a Web site.
Provides a convenient implementation of the HttpServletRequest
HttpServletRequestWrapper interface that can be subclassed by developers wishing to adapt
the request to a Servlet.
Provides a convenient implementation of the
HttpServletResponseWrapper HttpServletResponse interface that can be subclassed by
developers wishing to adapt the response from a Servlet.
Events of this type are either sent to an object that implements
HttpSessionBindingListener when it is bound or unbound from a
HttpSessionBindingEvent session, or to a HttpSessionAttributeListener that has been
configured in the deployment descriptor when any attribute is
bound, unbound or replaced in a session.
This is the class representing event notifications for changes to
HttpSessionEvent
sessions within a web application.
HttpUtils Deprecated. As of Java(tm) Servlet API 2.3.
14. Generic Servlet
• javax.servlet
Class GenericServlet
• java.lang.Object javax.servlet.GenericServlet
All Implemented Interfaces:
java.io.Serializable, Servlet, ServletConfig
15. import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.GenericServlet;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
public class GenericServletExample extends GenericServlet {
public void init()
{ log("inside init() method"); }
public void service(ServletRequest request, ServletResponse response)
throws ServletException, IOException
{ log("Handling request");
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.write("<html><head><title>GenericServle example</title></head>");
out.write("<body><h1>GenericServlet: Hallo world
</h1></body></html>");
out.close(); }
public void destroy()
{ log("inside destroy() method"); } }
16. HttpServlet
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class Hello extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException
{
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<body>");
out.println("<h1>hello</h1>");
out.println("</body>");
out.println("</html>");
}
}
19. 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.
20. • 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.
21. 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.
22. 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.");
}
}
23. 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 ( ); }
}