Advanced Servlet Interview Questions and Answers - Page 4

    1 Votes

59. What is doHead and doPut method?

doHead() method: It handles requests sent using the http head method. It returns the response header which contains Content-Type, Content-Length, Last-Modified etc. Head method is used to find whether a requested resource exists.
doPut() method: It handles requests sent using http put method. It allows a client to store information on the server.

60. What is the use of setContentType method?
setContentType() method is used to intimate the browser that which type of content is coming as response, depending upon the contentType browser treats with the response.
61. Suppose if we want a greek language support in application then which method we should use getWriter or getOutputStream method and why?

Instead of using PrintWriter out = response.getWriter(), you can also use ServletOutputStream out = response.getOutputStream(), but Unicode support will be not available. For Example, if you want a greek language support in your application then you need to use getWriter instead of getOutputStream.

62. How to get the servlet context object?

we can get ServletContext object by following process:

ServletContext context = getServletConfig().getServletContext();


ServletContext context;
Public void init(ServletConfig cfg)

63. Explain the working of servlet?

  • Reading data submitted by the client – The first step performed by the servlet is to read any data sent by the client. This is usually html form data.
  • The servlet need to check the HTTP information embedded in the user’s request. 
  • As part of the process of responding to the client’s request, the servlet may have to access business tier components such as EJB’s, or RDBMS.
  • When all the necessary processing is done, the servlet formats the request results – usually in an HTML or XML format, although other formats can be sent, such as binary code or GIF images.
  • The servlet has to set the appropriate HTTP response parameters – this includes the telling the browser what type of document is being returned.
  • The final step in the process is for the servlet to return the response to the client.

64. When the servlet object is first created?

Depending on how you register your servlet with the web server, it can be created either when a client first references a URL that corresponds to the servlet, or when the web server is first started.

65. What operations do we basically perform in servlet’s destroy method?

cleanup operations such as closing database connections, writing cookie lists, and halting background threads before it is destroyed.

66. When a web server unloads a servlet instance?

A server may unload a servlet instance if its running incorrectly, or if the servlet has been idle for some time.

67. What is the main task of init() method?

The main task of the init() method is to allow the servlet to access deployment – specific initialization parameters, such as database settings.

68. What is the use of getParameter method of http servlet request object?

getParameter() method is used to read data from a request object as String.

69. What are the main responsibilities of Servlet, JSP and EJB?

JSP’S typically provide presentation functionality for users. EJB components are responsible for defining application’s business model and providing clients with access to database information. Servlets act as a intermediaries between JSP’s and EJB’s or databases.

70. How client state is maintained using HTTPSession interface?

It maintains client state by using servlet getAttribute() and setAttribute() methods of session scope.

71. Why the servlet are packaged in a standard packaging structure?

J2EE defines a standard packaging structure to package a servlet into a J2EE application to make it portable across different application servers. A standard packaging structure will allow application servers to easily locate and load application files from the standard directory structure.

72. On which existing session-tracking technologies do you think the HTTPSession interface is built?

The HTTPSession interface is founded on cookie and URL rewriting technology. It hides the complexity of having to manipulate cookies or information appended to URL’s from the servlet developer. This work is automatically done by the web container, which generates HTTPSession
objects as required.

73. HTTPSession interface handles the routine work of cookies, such as associating hash tables of session information with client requests. What other tasks do you think are carried out by the HTTPSession interface?

The HTTPSession interface generates the unique session IDs, identifies and extracts the particular cookie that stores the session ID, and sets a suitable expiration time for a session.

74. What are the functions of the Cookie class?

Creation of cookies, including long lived cookies, and setting duration for the lifetime of a cookie are functions of the cookie class.

75. How servlet filter chain works?

  • Multiple filters can be chained. Order is dictated by the order of <filter> elements in the deployment descriptor. 
  • The first filter of the filter chain is invoked by the container via doFilter(ServletRequest req, ServletResponse res, FilterChain chain) method. The filter then perform whatever filter logic and then call the next filter in the chain by calling chain.doFilter(…) method.
  • The last filter’s call to chain.doFilter() ends up calling service() method of the Servlet.
Page 4 of 4

Popular Videos


How to improve your Interview, Salary Negotiation, Communication & Presentation Skills.

Got a tip or Question?
Let us know

Related Articles

JSP Interview Questions and Answers
Servlet Interview Questions and Answers
EJB Interview Questions and Answers
Spring Interview Questions and Answers