Servlet VC

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 5

Servlet Technology is used to create web applications.

Servlet technology uses Java language to create web


applications.

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 servlet example can be created by three ways:

1. By implementing Servlet interface,

2. By inheriting GenericServlet class, (or)

3. By inheriting HttpServlet class

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:

1. Loads the servlet class

2. Creates an instance of the servlet class

3. Initializes it by calling the init method

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.

public void service(ServletRequest request, ServletResponse response)

throws ServletException, IOException {

// ...

}
destroy()

Called by the Servlet Container to take the Servlet out of service.

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 void destroy() {

//

Methods of Servlet interface

S.No. Method Description

It is used for initializing the servlet. It is invoked only once


1. public void init(ServletConfig config)
by the web container in a servlet life cycle.

It is used for providing a response to all the incoming


public void service(ServletRequest
2. request. It is invoked every time by the web container for
req, ServletResponse res)
each request.

It is used for destroying the servlet. It is invoked only once


3. public void destroy()
in a life cycle of a servlet.

public ServletConfig
4. It is used to get the object of ServletConfig.
getServletConfig()

It is used to get information about writer, copyright etc of a


5. Public String getServletInfo()
servlet.

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

S.No. Method Description

public void service(ServletRequest It is used for securing the service method by


1
req,ServletResponse res) creating objects of request and response.

protected void service(HttpServletRequest req,


2 It is used for receiving a service method.
HttpServletResponse res)

protected void doGet(HttpServletRequest req, It is invoked by the web container and it is


3
HttpServletResponse res) used for handling the GET request.

protected void doPost(HttpServletRequest req, It is invoked by the web container and it


4
HttpServletResponse res) handles the POST request.

protected void doHead(HttpServletRequest req, It is invoked by the web container and it


5
HttpServletResponse res) handles the HEAD request.

protected void doOptions(HttpServletRequest req, It is invoked by the web container and it


6
HttpServletResponse res) handles the OPTIONS request.

protected void doPut(HttpServletRequest req, It is invoked by the web container and it


7
HttpServletResponse res) handles the OPTIONS request.

protected void doTrace(HttpServletRequest req, It is invoked by the web container and it


8
HttpServletResponse res) handles the TRACE request
protected void doDelete(HttpServletRequest req, It is invoked by the web container and it
9
HttpServletResponse res) handles the DELETE request.

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.

Methods of GenericServlet interface

Implemented Interfaces:

java.io.Serializable, Servlet, ServletConfig

Constructor:

GenericServlet() : this constructor does nothing. Everything is initialized by the init method.

S.NO
Method Desciption
.

1 public void init(ServletConfig config) It is used for initialization of a servlet.

It is used for providing all the services for the


public abstract void service(ServletRequest
2 incoming request. When a user request then only
request, ServletResponse response)
it invokes.

It is used for destroying the servlet. It is invoked


3 public void destroy()
only once in a life cycle of a servlet.

4 public ServletConfig getServletConfig() It is used to get the object of ServletConfig

It is used to get information about writer,


5 public String getServletInfo()
copyright etc of a servlet.

It is a very easy and convenient method for


6 public void init()
programmers.

7 public ServletContext getServletContext() It is used for getting object of a servlet


It is used for getting all the parameter values
8 public String getInitParameter(String name)
from the given parameter names.

It is used for getting parameters which are


9 public Enumeration getInitParameterNames()
defined in web.xml files

10 public String getServletName() It is used for getting the name of a servlet object.

It is used for writing a message in a servlet log


11 public void log(String msg)
file.

It is used for writing a message in a servlet log


12 public void log(String msg, Throwable t)
file and stack trace.

You might also like