Questions About Servlet

1. What are servlets? Explain with an example?

Ans.Servlets are programs that run on a Web server and create Web pages on the fly.  To be a servlet, a class should extend HttpServlet and override doGet or doPost or both methods, depending on whether data is sent by GET or POST. These methods take two arguments, HTTPServletRequest and HttpServletResponse. The HTTPServletRequest has methods that let you find out about incoming information such as FORM data and HTTP request headers. The HttpServletResponse has method that lets you specify the HTTP response line, response headers, and obtain a PrintWriter object, used to send output back to the client. For simple servlets, most of the effort is spent in println statements that generated the desired page. A sample servlet is given below:

package hallo;

import java.io.*;

import javax.servlet.*;

import. Javax.servlet.http.*;

public class HelloWorld extends HttpServlet {

public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOexception {

PrintWriter out = response.getWriter();

Out.println(“Hello World”);

}

}

2. Explain Servlet life cycle.

Ans.

The lifecycle of a servlet consists of the following main stages:

Instantiation: The Web server creates an instance of a servlet. This is based on  a request or a container startup.

Initialization: The Web server creates the instance’s init() method. When the Web server loads a Web application, it also loads the initialization parameters associated with the application. This and the previous step occur only once during the lifetime of a servlet.

Service: This is the third state in the servlet lifecycle. In this state, the servlet’s service() method is called, which generates response. The generation of response involves the following steps:

  1. Setting the content type of the response. The receiving application (browser) uses this information to know how to treat the response data. For example, to generate an HTML output, the content-type is set to “text html”.
  2. The second step is to get PrintWriter object from response. PrintWriter is a class that extends the java.io writer abstract class. In the case of servlets, the Web server constructs the Print Writer object from the java.io. OutputStream object associated with the underlying connection from the client. With TCP/IP  based implementations, Web server usually gets OutputStream object from the socket, uses the object to create the PrintWriter object, and associates with the HttpServletResponse object. As a result, from within the servlet, you will able to write to object stream associated with the network association.

Destroy: This is the final stage in a servlet lifecycle. In this stage, the destroy() method is called before shutting down the servlet.

3. What is the difference between HttpServlet and GenericServlet?

Ans.

GenericServlet is the base class, which is extended by an abstract class HttpServlet.

4. What is the difference between doGet() and doPost().

Ans.

doGet() needs to be overridden when the data is submitted to the server using the GET method whereas the doPost() method needs to be overridden when POST method is used to submit data to the server.

5. Explain the relationship between a servlet, its servlet container and the servlet API.

Ans.

A servlet is a Java program, which runs in a servlet container and is written according to servlet API specification. Servlet API is a set of classes and interfaces, which outlines all necessary operations, restrictions, and behaviour for a servlet. A servlet container is software, which provides network services over which requests, responses are sent. It should provide abstraction in obtaining requests and formatting responses and at least support communication over HTTP. For example, Tomcat is a servlet container.

6. Which two packages implement the Servlet API?

Ans.

The two packages are:

  1. javax.servlet

javax.servlet.http

8. What are the advantages of using servlets to extend server functionality?

Ans.

Following are the advantages of using servlets:

  1. It helps to achieve platform independence.
  2. It provides performance improvement as classes stays in the memory once called.
  3. It is much more secure over traditional server side languages. For example, no problems of memory leakages and buffer overflows are faced in case of servlets.
  4. It uses Java’s multithreading capabilities, which gives it a performance gain.

9. Describe the request handling cycle and the methods invoked on the servlet by the servlet container.

Ans.Following are the operations done on a servlet by servlet container when a servlet is requested:

  1. If servlet instance does not exist in memory, the container:
  2. Loads the servlet class and thus creates its instance
  3. Calls the init() method on the above class and passes ServletConfig as parameter
  4. On any further request service() method of servlet class is called and HttpServletRequest and HttpServletResponse are passed as parameters to it.

10. Which class provides methods of reading the parameters submitted by a client?

Ans.

There is no class for this but HttpServletRequest interface provides methods getParameter(), getParameterValues(), and getParameterMap() for obtaining parameters submitted by client.

11. Which class provides methods of reading the parameters specified in the deployment descriptor file?

Ans.

ServletConfig class provides methods getInitParameter() and getInitParameterNames() for reading parameters specified in deployment descriptor.

12. Which streams are used for sending the contents of the entity-body in the response?

Ans.

ServletOutputStream is used for sending contents in response, if the data is binary. It extends the OutputStream class. Its object is normally obtained by calling HttpServletResponse.getOutputStream.

For sending character data PrintWriter class is used, which extends the Writer class. Its object is obtained by calling the HttpServletResponse.getWriter() method.

13. Outline the main steps in implementing the servlet.

Ans

Following are the steps used in implementing a servlet for HTTP communication:

  1. Import required servlet packages in the class.
  2. Make the class extend HttpServlet.
  3. Override methods doGet(), doPost(), or doService() as required by problem in hand.
  4. Make an entry for the above servlet in deployment descriptor.
  5. Configure any extra options or filters, which are required by the server to function.

14. Explain the semantics of the HTML FORM element.

Ans.The HTML form element represents a form in which users can input data and submit it to the server for processing. The HTML form tag is as follows:

<FORM NAME=”name” ACTION = “action.jsp” METHOD = “POST/GET”>

The name attribute specified the name for a form. The action attribute specified the URL of the document or the name of the file to which the form data is submitted for action and the method attribute specified the HTTP method through which the data is sent to the server.

The action attribute can be a simple static Web page or a servlet. The method can be POST or GET.

A form includes various other <INPUT> elements such as textbox, list, buttons, or checkboxes.

15. Which statements are true.

  1. A web application can have more than web.xml.
  2. The HTTPServlet class implements the ServletConfig interface.
  3. The HTTPServlet class implements the ServletContext interface
  4. In a doHttpServletMethodName( ) method, the request and the response objects can be manipulated in any order.
  5. The servlet configuration can change from request to request.
  6. The servlet context can change from request to request.
  7. The servlet element must occur before the servlet-mapping in an element in web.xml file.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>