Servlet interview questions

Servlet interview questions

If you are Java developer, then you must have the good knowledge of servlet. As this is the utmost topic of Java EE. Coupling with this, all the frameworks of web applications such as spring and Strut are built on top of it. There are chances that interviewer asked questions from Servlet. So, equip yourself with these questions.

Download Servlet interview questions PDF

Servlet interview questions

In general, on the very first request of the client, the server container loads a servlet. But in case, the servlet is too heavy that it takes much time to load, at that time we might want it to load during the application startup. To do this, the user can use WebServlet notation or loadOnStartup variable to stimuli container. Or, we can use a load-on-startup element with a servlet configuration in web.xml file to load the servlet on startup.


<servlet-name>coo </servlet-name>
<servlet-class>com.coo.servlets.Coo </servlet-class>
<load-on-startup>6 </load-on-startup>

If you want that the container loads the servlet on client request, then the load-on-startup value should be negative otherwise if it’s positive then the loading will start on application startup.

If there are many servlets with different load-on-startup values, then that servlet will be loaded first which have the lowest value.

There are many ways of login-based authentication. These are:

  • HTTP Digest Authentication
  • HTTP Basic Authentication
  • HTTPS Authentication
  • Form-based Authentication

Use the below snippet code to get the information:


Use the written code to get the actual path:


Servlet filters are pluggable java components that are used to process requests before the requests are sent to the servlets and before a container sends the response back to the client.

Some tasks performed with filters are:

  • Logging request parameters to log files.
  • Authorization of request for services
  • The arrangement of request body and header before sending it to a servlet.
  • Condense the response data sent to client
  • Add cookies, header information etc. In response.

It consists of three methods:

  1. Public void Init: Container initialize the servlet using this method. It is invoked once during the whole life cycle.
  2. Public void service: For every request, this method is called once at a time.
  3. Public void destroys: This is called when a servlet is destroyed from memory.

There are main four phases required in Servet Life Cycle. These are:

  1. Servlet class Loading: On a client request of a servlet, the container first loads the class in the memory and after that calls its default no argument constructor.
  2. Servlet Class Initialization: After the first phase of loading class in memory, container initiates the ServletContext object and then invokes the Init method by passing servlet Config for the servlet. This phase transforms the normal class to a servlet.
  3. Request Handling: It is the phase where a servlet is ready to handle the requests. For each client request, it invokes the method service () by passing request and response object reference.
  4. Removal from service: when the process stops, the container calls the destroy () method and destroy the servlet.

Safety and security are always the priority for any company. Thus, the single Thread Model interface introduces for this reason. The main purpose of this concept is to check whether two or more threads are executing at the same time. Like many threads may be penetrated by many requests, so to execute them in a manner, it is used. But in servlet 2.3, this concept is disapproved as it can stop the multithreading method.

URL Encoding means the changing of data into CGI. It is done to circumvent any type of issue while it is going over the network. URL coding is schemed by removing the white spaces and put any escape symbols. Thus, the special characters are removed. To encode any string (String str, String Unicode) is used and to decode the URL (String str, String Unicode) is used. Let's take an example to simplify it. To encode 'Ravi's data' it is changed into 'Ravi/24d+data'.

To design implement of servlet request and response, these wrapper classes are being used. The users are able to increase these classes to revoke the particular techniques that need to be implemented. There are two wrapper classes that are provided by Servlet HTTP API. These classes are

  1. Http Servlet Response Wrapper
  2. Http Servlet Request Wrapper
  • Print Writer is referred to a character-stream state while Servlet Output Stream is considered as a byte-stream state.
  • Print Writer is used to writing the information that is based on the character for example character design and string to the response. While to write the byte design, Servlet Output Stream is used.
  • To arrange the Print Writer the clients have to use the Servlet Response get Writer () while to arrange Servlet Output Stream the client have to use Servlet Response get Output Stream () method.
  • GET is a safe method (idempotent) where POST is the non-idempotent method.
  • GET is an idempotent method and it is safe while the POST method is non-idempotent.
  • GET is the part of the header while POST is the content of the body.
  • A limited data can be sent with GET while POST method allowed a large data to send
  • GET method can easy to bookmark while POST method can't.
  • It is not secure to use the Get method as it is possible to send the same request over and over while the POST method is secure.
  • GET method is the default method while the POST method needs to be specified.

It is not good to use Http Session in servlets as it does not work by alone. It uses cookies for session management and we can disable the cookie in the client browser. So most of the IT worker believes that it is better to use Servlet API because it can easily perform the function of URL rewriting.

Servlet API is very easy to use as it involves only one step that is encoding the URL. So, it is very beneficially in respect of coding. Another benefit of using Servlet API over Http session is that it works only when the cookies are disabled.

Http Servlet Response encodes URL () can also be used for redirecting the request to the different resources and encode Redirect URL () method is used in the case of client wants to give some session knowledge.

A session is a colloquial link between the server and the client. Session handles a lot of requests and supplies the answer within the client and server. As both the HTTP and web server are unsettled, so for the maintenance of a session it is necessary to have some bizarre request that has to be passed between the server and the client.

The various method that is used to manage session in servlets are

  • Cookies
  • HTML Hidden Field
  • URL Rewriting
  • User Authentication
  • Session Management API