Professional Documents
Culture Documents
Servlet VC
Servlet VC
Servlet VC
Web applications are helper applications that resides at web server and build dynamic web pages. A
dynamic page could be anything like a page that randomly chooses picture to display or even a page that
displays the current time
The mostly used approach is by extending HttpServlet because it provides http request specific method such
as doGet(), doPost(), doHead() etc.
Servlet API consists of two important packages that encapsulates all the important classes and interface,
namely :
javax.servlet
javax.servlet.http
Servlet Lifecycle
init()
The init method is designed to be called only once. If an instance of the servlet does not exist, the web
container:
The init method must complete successfully before the servlet can receive any requests. The servlet
container cannot place the servlet into service if the init method either throws a ServletException or does not
return within a time period defined by the Web server.
service()
This method is only called after the servlet's init() method has completed successfully.
The Container calls the service() method to handle requests coming from the client, interprets the HTTP
request type (GET, POST, PUT, DELETE, etc.) and calls doGet, doPost, doPut, doDelete, etc. methods as
appropriate.
// ...
}
destroy()
This method is only called once all threads within the servlet's service method have exited or after a timeout
period has passed. After the container calls this method, it will not call the service method again on the
Servlet.
//
public ServletConfig
4. It is used to get the object of ServletConfig.
getServletConfig()
HttpServlet class
HttpServlet is also an abstract class. This class gives implementation of various service() methods
of Servlet interface.
To create a servlet, we should create a class that extends HttpServlet abstract class. The Servlet class that
we will create, must not override service() method. Our servlet class will override only
the doGet() and/or doPost() methods.
The service() method of HttpServlet class listens to the Http methods (GET, POST etc) from request stream
and invokes doGet() or doPost() methods based on Http Method type.
Methods of HttpServlet interface
protected long getLastModified(HttpServletRequest It is used for getting the time of last modified
10
req) HttpServletRequest.
GenericServlet class
In Servlet, GenericServlet is an abstract class. This class implements the servlet, ServletConfig and
Serializable interface. This class provides the implementation of most of the basic servlet methods. The
protocol of this class is independent as it can handle any type of request.
Implemented Interfaces:
Constructor:
GenericServlet() : this constructor does nothing. Everything is initialized by the init method.
S.NO
Method Desciption
.
10 public String getServletName() It is used for getting the name of a servlet object.