Servlet QA

Qn. 1 What is a Servlet ?

Ans. Servlets are modules that extend request/response-oriented servers, such as Java-enabled web servers. For example, a servlet might be responsible for taking data in an HTML order-entry form and applying the business logic used to update a company’s order database. Servlets are to servers what applets are to browsers. Unlike applets, however, servlets have no graphical user interface, servlet is used to create a web application reside at server side and generates a dynamic web pages, and provides many interfaces and classes including documentation.

Qn. 2 Explain the life cycle methods of a Servlet ?

Ans. The javax.servlet.Servlet interface defines the three methods known as life-cycle method.

public void init(ServletConfig config) throws ServletException

public void service( ServletRequest req, ServletResponse res) throws ServletException, IOException

public void destroy()

Servlet's Life Cycle

The web container maintains the life cycle of a servlet instance. Let's see the life cycle of the servlet

  1. Servlet class is loaded.
  2. Servlet instance is created.
  3. init( ) method is invoked.
  4. service( ) method is invoked.
  5. destroy( ) method is invoked.
  • Servlet class is loaded

The classloader is responsible to load the servlet class. The servlet class is loaded when the first request for the servlet is received by the web container.

  • Servlet instance is created

The web container creates the instance of a servlet after loading the servlet class. The servlet instance is created only once in the servlet life cycle.

  • init( ) method is invoked

The web container calls the init method only once after creating the servlet instance. The init method is used to initialize the servlet. It is the life cycle method of the javax.servlet.Servlet interface. Syntax of the init method is given below:

public void init(ServletConfig config) throws ServletException

  • service() method is invoked

The web container calls the service method each time when request for the servlet is received. If servlet is not initialized, it follows the first three steps as described above then calls the service method. If servlet is initialized, it calls the service method. Notice that servlet is initialized only once. The syntax of the service method of the Servlet interface is given below:

public void service(ServletRequest request, ServletResponse response) throws ServletException, IOException

  • destroy() method is invoked

The web container calls the destroy method before removing the servlet instance from the service. It gives the servlet an opportunity to clean up any resource for example memory, thread etc. The syntax of the destroy method of the Servlet interface is given below:

public void destroy()

Method Description

  • public void init(ServletConfig config)

initializes the servlet. It is the life cycle method of servlet and invoked by the web container only once.

  • public void service(ServletRequest request,ServletResponse response)

provides response for the incoming request. It is invoked at each request by the web container.

  • public void destroy()

is invoked only once and indicates that servlet is being destroyed.

Qn. 3 Steps to create a servlet example

Ans. There are given 6 steps to create a servlet example. These steps are required for all the servers. 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.

Qn. 4 How Servlet works ?

Ans. It is important to learn how servlet works for understanding the servlet well. Here, we are going to get the internal detail about the first servlet program.

  1. The server checks if the servlet is requested for the first time.
  2. If yes, web container does the following tasks:
  • loads the servlet class.
  • instantiates the servlet class.
  • calls the init method passing the ServletConfig object

else

  • calls the service method passing request and response objects

The web container calls the destroy method when it needs to remove the servlet such as at time of stopping server or undeploying the project.

Qn. 5 How web container handles the servlet request ?

Ans. The web container is responsible to handle the request. Let's see how it handles the request.

  • maps the request with the servlet in the web.xml file.
  • creates request and response objects for this request
  • calls the service method on the thread
  • The public service method internally calls the protected service method
  • The protected service method calls the doGet method depending on the type of request.
  • The doGet method generates the response and it is passed to the client.
  • After sending the response, the web container deletes the request and response objects. The thread is contained in the thread pool or deleted depends on the server implementation.

Qn. 6 What is the difference between the getRequestDispatcher(String path) method of javax.servlet.ServletRequest interface and javax.servlet.ServletContext interface ?

Ans. The getRequestDispatcher(String path) method of javax.servlet.ServletRequest interface accepts parameter the path to the resource to be included or forwarded to, which can be relative to the request of the calling servlet. If the path begins with a "/" it is interpreted as relative to the current context root. The getRequestDispatcher(String path) method of javax.servlet.ServletContext interface cannot accepts relative paths. All path must sart with a "/" and are interpreted as relative to curent context root.

Qn. 7 Explain the directory structure of a web application ?

Ans. The directory structure of a web application consists of two parts.

  • A private directory called WEB-INF
  • A public resource directory which contains public resource folder.

WEB-INF folder consists of

  1. web.xml
  2. classes directory
  3. lib directory

Qn. 8 What are the common mechanisms used for session tracking ?

Ans. Cookies, SSL sessions, URL- rewriting

Qn. 9 Explain ServletContext ?

Ans. ServletContext interface is a window for a servlet to view it's environment. A servlet can use this interface to get information such as initialization parameters for the web applicationor servlet container's version. Every web application has one and only one ServletContext and is accessible to all active resource of that application.

Qn. 10 What is pre initialization of a servlet?

Ans. A container does not initialize the servlets as soon as it starts up, it initializes a servlet when it receives a request for that servlet first time. This is called lazy loading. The servlet specification defines the <load-on-startup> element, which can be specified in the deployment descriptor to make the servlet container load and initialize the servlet as soon as it starts up. The process of loading a servlet before any request comes in is called preloading or pre initializing a servlet.

Qn. 11 What is the difference between Difference between doGet() and doPost() ?

Ans. doGet() method is limited with 2k of data to be sent, and doPost() method doesn't have this limitation. A request string for doGet() looks like the following:

http://www.vyutpatti.blogspot.com/svt1?p1=v1&p2=v2&...&pN=vN

doPost() method call doesn't need a long text tail after a servlet name in a request. All parameters are stored in a request itself, not in a request string, and it's impossible to guess the data transmitted to a servlet only looking at a request string.

doGet -

  • To retrieve or download from server
  • It performs read operation only
  • Query string or data should be append in URL
  • ASCII character data can be send
  • Limited amount of info ( upto 2Kb - 2048)
  • Security is less
  • Bookmarking of GET method always possible
  • Caching is possible

doPost -

  • To post or submit information to the server
  • Update/Write operation
  • No restriction on length of body
  • Both binary and character can be send
  • Security is more(data is encapsulate in body)
  • Bookmarking is not possible
  • Caching is not possible