Presentation Transcript
Slide 1:SERVLET INTERFACE
Servlet interface
{
void init(ServletConfig sc)
throws ServletException;
void service(ServletRequest req,ServletResponse res);
throws
ServletException,
IOException;
void destroy();
}
Slide 2:A CLOSER LOOK AT A SERVLET
A Java class that extends HttpServlet
Compiled and placed in the appropriate directory
(more on that later)
When a request for a servlet arrives, the servlet container
(a JVM):
checks if an instance of that servlet exists
if not, it creates/loads an instance
calls the servlet’s service() method (which in turn may call
doGet() /doPost() /doXXX())
Slide 3:STRUCTURE OF A SERVLET
A servlet does not have a main() method.
a Java program: yes
an applet: no
Certain methods of a servlet are invoked by the server in
the process of handling requests.
Each time the server dispatches a request to a servlet, it
invokes the servlet's service() method.
Slide 4:A GENERIC SERVLET HANDLING A REQUEST
Slide 5:REQUEST/RESPONSE
The service() method accepts two parameters:
a request object: tells the servlet about the request
a response object: the response object is used to return
a response
Slide 6:AN HTTP SERVLET HANDLING GET AND
POST REQUEST
Slide 7:SERVLET LIFE CYCLE
Slide 8:HTTPSERVLET CLASS
Servlet is a simple Java class which must
implement javax.servlet.Servlet interface.
GenericServlet
class provides a default implementation of this interface so that we don't have to implement every method of it.
HttpServlet
class extends GenericServlet to provide an HTTP protocol specific implementation of Servlet interface.
Slide 9:HTTPSERVLET CLASS METHODS
init() Called only once during the initialization of the Servlet.
destroy() Called only once when Servlet instance is about to be destroyed.
service() Do not override this method!!.
doGet(), doPost(), doPut(), doDelete(), doOptions(),
doTrace() These methods are called according to the type of HTTP request received. Override them to generate your own response.
log() Writes messages to the Servlet's log files.
getLastModified() Override this method to return your Servlet's last modified date.
Slide 10:HTTPSERVLET CLASS METHODS
getServletInfo() Override this method to provide a String of general info about your Servlet such author, version, copyright etc.
getServletName() Override this method to return name of the Servlet.
getInitParameter(), getInitParameterNames() First one returns value of given initialization parameter, second one returns an Enumeration object containing names of all initialization parameters provided.
getServletConfig() Returns a reference to ServletConfig object.More on it later in this article.
getServletContext() Returns reference to ServletContext object.More on it later in this article.