Top 80+ Servlet Interview Questions And Answers

In this article, I am going to share Top 80+ Servlet Interview Questions And Answers frequently asked on Interview. You can expect many questions from this topic in the interview.

Q1. What is servlet?
Ans:
Servlets is Web Technology which is used to develop the Applications provided by Sun MicroSystem.

Q2. What do you understand by Web Technology?
Ans:
Web Application is a type of application in which whole application will be on a server machine and the client will access the application using a browser. To develop Web Applications you need to develop client-side component as well as Server side Component.

Q3. What technologies are required to develop a client-side component?
Ans: Client side component can be developed using HTML/JSP/XHTML etc.

Q4. What technologies are required to develop a Server-side component?
Ans:
Server side component can be developed using Servlet/Jsp/Filter etc.

Q5. What are the types of Web Application?
Ans:
Two types of Web Application are:

  • Static Web Application
  • Dynamic Web Application

Q6. What is a Static Web Application?
Ans: In this type of application, a view will be the same for all the client and client cannot interact with the application by sending some data to the server for processing.

Q7. What is a Dynamic Web Application?
Ans:
In this type of application, a client can send the data to a server and that data will be processed and depending on the processing the response will be given to the client so the view of application will vary from client to client.
Servlet can be used for developing Web Application. Servlets is a server side component which receives the request from a browser, processes the request and sends the response to the browser.

Q8. What are the Types of Applications Java Servlet?
Ans:
Types are:

  • Standalone Applications
  • Client-Server Applications
  • Web Applications
  • Distributed Applications
  • Enterprise Applications

Q9. What is a Standalone Application?
Ans:
Applications that can be accessed by a single user at a time are called Standalone Applications. Ex: MS-Word, Music Player, etc.

  • Standalone Applications can be implemented using c, c++, and Java, etc.

Problems:

  • Applications have to be installed across all the machines. This may give maintenance when you change application features.
  • Data sharing is not possible.

Q10 What is Client-server Application?
Ans:
In this case of Client-Server Application, an application can be divided into two parts. One part will be installed on the server Machine and other parts will be installed on multiple client machines i.e. multiple clients can access the centralized server.
Ex. Yahoo Messenger Talk etc.

  • Client-Server Applications can be implemented using C, C++, and Java, etc.
  • Data sharing is possible

Problems: Client software has to install across all the machines. This may give maintenance when you change client software features.

Q11. What is Web Applications?
Ans:
To solve the problem with client-server application Web-based application is introduced which is run in WWW. In this case of Web Applications, the Application software will be installed on the Web Server Machine only. i.e. multiple clients can access the centralized web server using any web browser.
Ex. gmail.com

  • Web Applications can be implemented using Servlets, JSP, Struts, and JSF, etc.
  • Data sharing is possible.
  • No Maintenance problem because modifications will happen only at the Web server.

Q12. What is Distributed Applications?
Ans:
Distributed Application is a current trend in a company which allows the business partner to share the information among them.

Q13. What is an Enterprise Application?
Ans:
An enterprise application is a term used to describe applications or software that a business would use to assist the organization in solving enterprise problem. When the word “enterprise” is combined with “application”, it usually refers to a software platform that is too large and too complex for an individual or small business use.
Enterprise applications are typically designed to interface or integrate with other enterprise applications used within the organization, and to be deployed across a variety of networks(Internet, Intranet and corporate networks) while meeting strict requirements for security and administration management.

Q14. How does Http Request work?
Ans: When you hit the browser with some URL. The first request will be given to DNS server. In DNS server domain will be resolved of IP address and a request will be sent to the server where an application is running. Once the server receives the request it processes the request and sends the response to the client. Servers identify the client IP address from the Http request.

 

Q15. What do know about Web Server?
Ans:
Web server is an application which receives the Http Request from Client and processes that request with the help of Container and send the response to a client.
Ex: Apache Server, PWS (ASP), IIS

Q16. What is Web Client?
Ans:
This is an application which sends the Http Request to Web Server and receives the response from Server.
Ex: IE, Opera, Mozilla, etc.

Q17. What is Web Container?
Ans:
This is an application is responsible for managing the complete lifecycle of Servlet or JSP.

Q18. What is Http?
Ans:
Http sits on the top of TCP/IP for transferring Client into Server info to Client.

Q19. What is TCP/IP?
Ans:
Original data transfer will happen through TCP/IP. IP(Internet Protocol) is responsible for carrying data from one place to another. TCP(transport control protocol) sits on the top of the IP and monitors the Data Transmission.

Q20. What is DNS?
Ans:
Domain Naming Service is a registry where domain names will be bound with IP address.

Q21. Name some of the Web Technologies?
Ans:
Web Technologies are Servlet and JSP.

Q22. Name some of the Web Framework?
Ans:
Struts, JSP, SpringMVC, etc are the web frameworks.

Q23. What are the Parameters in Servlet?
Ans:

  • Parameters are name-value pair.
  • Parameter Name and Parameter value are of type String only.
  • Parameters are Read-only. We can’t change the request data.

Q24. What are the types of Parameters?
Ans:
Three types of Parameters are :

  • ServletRequest parameters
  • ServletConfig parameters
  • ServletContext parameters

Q25. What are the ServletRequest parameters?
Ans:
Client submitted data coming from the web client (Browser) to Server(Apache) along with Http Request are called as Request Parameters
Web container collects client submitted data and stores that in HttpServeltRequest object as Request parameters.
As a developer, you can collect that data from the request object as follows:
String un= request.getParameter(“name”);

Q26 What are the ServletConfig parameters?
Ans:
If you want to use any data which is common for all the users and specific to a particular servlet, that data can be specified as config parameters in the web.xml as follows:

<servlet> ... <init-param> 
<param-name>email</param-name> 
<param-value>[email protected]</param-value>
 </init-param> <servlet>

Web container collects data from web.xml and stores that in ServletConfig object as Config parameters. As a Developer, you can collect that data from the config object as follows;
String en= config.getInitParameter(“email);

Q27. What are the ServletContext Parameters?
Ans:
If you want to use any data which is common for all the users and common to all the servlets, that data can be specified as Context parameters in the Web.xml as follows;

<context-param> 
<param-name>city</param-name> 
<param-value>Noida</param-value> 
</context-param>

Web Container collects data from web.xml and stores that in ServletContext object as context parameters. As a developer, you can collect that data from the context object as follows:
String ci= context.getInitParameter(“city”);
All the servlets will have only one ServletContext object and can be shared with all the servlets running in the container. i.e One Web application will have one ServletContext object.

Q28 What are the three lifecycle methods of the servlet?
Ans:
The three lifecycle methods of the servlet are:

  • init() method
  • service() method
  • destroy() method

Q29 What init() method do?
Ans:
The web container calls the init method only once after creating the servlet instance. It is used to initialize the servlet class. This method is of javax.servlet.Servlet interface.

Q30 What is the syntax of the init method?
Ans
:
public void init(ServletConfig config) throws ServletException

Q31. What service() method do?
Ans:
The web container calls the service method whenever the request for the servlet is received. This method is meant to do the actual task of the servlet.

Q32. What is the syntax of the service method?
Ans:
public void service(ServletRequest request, ServletResponse response) throws ServletException, IOException

Q33. What is destroy() method do?
Ans:
The web container calls the destroy method before removing the servlet instance from the service. It is used for the cleanup purpose of the resource.

Q34. What is the syntax of the destroy method?
Ans:
The syntax of the destroy method is
public void destroy()
Q35. when servlet will be initialized?
Ans:
it will be initialized at container startup when you write <load-on-startup> tag. When you are not writing <load-on-startup> tag then servlet will be initialized when you send the first request.

Q36 What steps will happen when a container is initializing servlet?
Ans-

  1. Container reads information from web.xml and stores in the main memory using SAX parser(class loader used to read XML). If any problem happened while reading the web.xml container will not be ready to receive the request from the server.
  2. The container creates ServletContextObject and initializes the context object with context parameters specified in the web.xml.
  3. The container creates the thread pool.
  4. All the listener configured in the web.xml will be initialized (it is java class loading, creating an instance, initialization, and installation.
  5. All the filters configured in the web.xml will be initialized.
  6. Container checks whether any servlet is configured with <load-on-startup> tag. If any one or more servlets found with <load-on-startup> tag then those servlets will be initialized by the container at container startup with the given priority by doing the following tasks.
    1. Container loads the Servlet class into the main memory.
    2. The container creates the instance of the servlet by invoking default constructor.
    3. The container creates a servlet config object and initializes the config object with the config parameters specified in web.xml
    4. Container associates the servlet context object with a servlet config object.
    5. A container invokes the init() method by passing the ServletConfig object as a parameter to initialize the servlet instance with required resources.

Q37 what is the importance of the number which you are providing for <load-on-startup>tag and what are the valid values?
Ans- Number represents the priority for initializing the servlet at container startup valid values are 0 to any +ve integer.

Q38 How many times init() will be called in the servlet life cycle?
Ans-
Only once

Q39 Can I write the argument constructors in servlet class?
Ans-
Yes, you can write argument constructors but the container always creates the instance with a default constructor.

Q40 How many times service() will be called in the servlet life cycle?
Ans-
Many times, depending on the no.of request coming to the servlet.

Q41 How many times destroy() will be called in the servlet life cycle?
Ans- only once at container shutdown time.

Q42 How can I access config parameters and context parameter in the service() method?
Ans:

 //1. config parameters
ServletConfig sc= getServletConfig();
sc.getInitParameter(“email”);
//2 context parameters
ServletContext ctx= sc.getServletContext();
ctx.getInitParameter(“phone”);

Q43 what is the use of init() and destroy()?
Ans-
init() is used to initialize the servlet instance with the required resources.
Destroy() is used to clean up the resources which are initialized by init().

Q44 How many instances will be created for one servlet?
Ans:
Container creates one or more servlet instances depending on the servlet model you are using.

Q45 What are Two servlet Models?
Ans:
Two Servlet Models are:

1) Single Thread Model
2) Multi-Thread Model

Q46. What is Single Thread Model?
Ans:
To follow STM your java servlet class has to implement an interface called SingleThreadModel(Marker Interface). In the case of STM, the servlet instance will be created for every incoming request. i.e, Multiple Instance will be created for STM. It is not recommendable to use STM because multiple instances of the servlet will be reused, so no use of maintaining many instances for a long time. This may cause performance/memory wastage problem.

Q47. What is the Multi-Thread Model?
Ans:
By default, container follows MultiThreadModel. Only one servlet instance will be created and the same instance will provide the service to every incoming request. It is recommendable, because of no problem.

Q48. What do you understand by Http request?
Ans: Request you are sending to a server using Http protocol.
HttpRequest contains two parts:

  • Http Request Header
  • Http Request Body

Q49. What is Http Response?
Ans:
Response which you are delivering to the client using Http protocol. It also contains two parts:

  • Http Response Header
  • Http Response Body
    Container is responsible for creating HttpServletRequest and HttpServletResponse objects.

Q50 What information does HttpServletRequest contains?
Ans:
It contains:

1. Request Parameters

2 Request Headers

3. Request Cookies

4. other information.

Q51. What information does HttpServletResponse contain?
Ans:

  • Response Headers
  • Response Stream

Q52 What is Request Dispatcher?
Ans:
It is an interface available in Javax.servlet. you can access the RequestDispatcher object by using the following method using ServletRequest or ServletContext object.

RequestDispatcher getRequestDispatcher(String resource): A resource can be either JSP, HTML, or URL of another servlet.

Q53. What are methods of the Request Dispatcher?
Ans:
methods of the Request Dispatcher are as follows:

  • void forward(request, response)
  • void include(request, response)

Q54. Explain the Forward method of the Request Dispatcher?
Ans:
Forward() method is used to forward the request from a Servlet to Servlet or Servlet to JSP or JSP to Servlet or JSP to JSP. Once the request is forwarded to another component, then control will not be returned back.

Q55. Explain the Include() method of the Request Dispatcher?
Ans:
Include() method is used to include the response of one servlet or JSP in another servlet or JSP and after evaluating the request control will return back. We can have more than one include() statement in one servlet or JSP but we can have only one forward() method in one servlet or JSP. After using forward you cannot use request and response object processing statements.

Q56. What are Http Methods?
Ans:
These are 7 Http methods which can be used to send the request.

  1. GET()
  2. POST()
  3. PUT()
  4. DELETE()
  5. HEAD()
  6. TRACE()
  7. OPTIONS()

Q60. How you can specify the required Http Method in Jsp page?
Ans:

<form action=”..” method=”XX”>
...
</form>

Q61. List out the difference between GET() and POST() method?
Ans: GET() method:

  • We can see request data in the URL
  1. By using Http method get() data will be attached to the Url as a query string.
  2. By using Http method get() you can send an only limited amount of data to browser limitation.
  3. Data will be visible at URL, not secured.

POST() method:

  1. By using Http method post() data will be placed in the Http request body.
  2. By using Http method post() you can send an unlimited amount of data.
  3. You can send the data in a secure way.

Q62. What do you know about the Model 1 Architecture of servlet?
Ans:

With Model 1 Architecture, One component is responsible for doing the following tasks.

  1. Controlling the request processing (Controller logic)
  2. Interacting with Database or External System(Persistence logic/Business Logic)
  3. Sending the response to the client(Presentation logic)

Consider, the requirement that same persistence Logic/ Business Logic, you need to modify all the Servlet. This is given you maintenance problem.
Consider, the requirement that same presentation logic is written in all the servlets(maybe presentation logic related to a header, menu, footer, and formatting style). This gives the code duplication problem. In the future, if you want to change presentation Logic, you need to modify all the servlets. This gives you a maintenance problem.

Q63. What do you know about the Model II Architecture or MVC (Model View Controller) of servlet?
Ans:

With Model 2 or MVC Architecture. These 3 tasks are distributed at 3 separate components called Model, View, and Controller. This clean Separation of Responsibilities avoids the code duplication and maintenance problem.

Servlet API consists of two important packages that encapsulate all the important classes and interface, namely :

  • javax.servlet
  • javax.servlet.http

Q64. What are the packages that Servlet API consists of that encapsulate all the important classes and interfaces?
Ans:

  • javax.servlet
  • javax.servlet.http

Q65 What are the classes and Interfaces of javax.servlet?
Ans:

INTERFACES

CLASSES

Servlet ServletInputStream
ServletContext ServletOutputStream
ServletConfig ServletRequestWrapper
ServletRequest ServletResponseWrapper
ServletResponse ServletRequestEvent
ServletContextListener ServletContextEvent
RequestDispatcher ServletRequestAttributeEvent
Single ThreadModel ServletException
Filter UnavailableException
FilterConfig GenericServlet
FilterChain
ServletRequestListener

 

Q66. What are the classes and Interfaces of javax.servlet.http?
Ans:

CLASSES INTERFACES
HttpServlet HttpServletRequest
HttpServletResponse HttpSessionAttributeListener
HttpSession HttpSessionListener
Cookie HttpSessionEvent

Q67. What is Servlet Interface?
Ans:

  • javax.servlet.Servlet is an interface which is root for all the servlets you are developing.
  • Java Servlet interface contains life cycle methods of a Java servlet.
  • In Servlet interface service() method signature is public void service(ServiceRequest, ServletResponse);

Q68. What is Generic Servlet-Abstract class?
Ans:

  • GenericServlet class is the direct subclass of Servlet interface.
  • Overriding all the methods of Servlet interface except service() method because of that only GenericServlet is declared as abstract.
  • When you develop the servlet by extending GenericServlet then you have to override the service() method as follows:
    • void service(ServletRequest req, ServletResponse res);
  • It is protocol-independent class and it can be used if the client will send the request with any protocol.

Q69. What is HttpServlet-Abstract class?
Ans:

  • HttpServlet class is the direct subclass of GenericServlet.
  • HttpServlet is overriding all the methods of GenericServlet, but HttpServlet is declared as abstract to restrict the container to create the object of HttpServlet class.
  • HttpServlet method() implementation is available but it is not for the Customs requirement.
  • HttpServlet is a protocol-dependent servlet class and it will be used only if the client sends the request using the HTTP protocol.
  • When you develop the servlet by extending HttpServlet then you can override any of the following methods.

Q70 What is the method of Generic-Servlet?
Ans:
Method of Generic-Servlet is:
void service(ServletRequest req, ServletResponse res);

Q71. What are the methods of Http Servlet Abstract class?
Ans:
The methods of Http Servlet Abstract classes are:

  • void service(HttpServletRequest req, HttpServletResponse res);
  • void doGet(HttpServletRequest req, HttpServletRespnse res);
  • void doPost(HttpServletRequest req, HttpServletResponse res);
  • void doPut(HttpServletRequest req, HttpServletResponse res);
  • void doDelete(HttpServletRequest req, HttpServletResponse res);
  • void doHead(HttpServletRequest req, HttpServletResponse res);
  • void doTrace(HttpServletRequest req, HttpServletResponse res);
  • void doOptions(HttpServletRequest req, HttpServletResponse res);

Q72. What will happen when I send the request to get() method.
Ans:
Http service() method of HelloServlet will be called.

Q73. What will happen when I send the request with POST.
Ans:
Http service() method of HelloServlet will be called.

Q74. What is Attribute?
Ans:
It is the element of name and value format when the name will be String type and value will be an Object type.
E.g
Customer cust= new Customer();
req.setAttribute(“CUST”, cust);

An attribute will be updatable for the developer. An attribute will be stored by the developer explicitly as well as if required it can be replaced from the corresponding object.

Q75. What are the Objects in which you can set the attribute?
Ans:
The objects are as follows:

  • ServletContext
  • HttpSession
  • ServletRequest

Q76. What are the scopes in which you can store the attribute in servlet?
Ans:

  • Context/ Application Scope
  • Session scope
  • Request scope

Q77. What is the Context/Application scope?
Ans:
if the attribute stored in context object then it is known as context/Application scope.
These data can be accessed in any component of the application.

Q78. What is session scope?
Ans:
If the attribute stored in HttpSession object then it is known as Session scope
These data can be accessed by the client in multiple requests related to one session.

Q79. What is the Request scope?
Ans:
If the Attribute will be stored in ServletRequest object then it is known as Request scope
These data can be accessed by the client in one request only.

Q80. What are the methods to manage attribute in multiple scopes?
Ans: 
The methods to manage attribute in multiple scopes are as follows:

  • public Object getAttribute(String name);
  • public void setAttribute (String name, Object obj);
  • public void removeAttribute(String name);

Q81. How can you access all the attribute name?
Ans:
You can access all the attribute name by the use of the following method:
public Enumeration getAttributeName().

That’s all about Top 80+ Servlet Interview Questions And Answers from Core Java Interviews. I have covered almost everything as per my knowledge for both freshers and experienced with 0 to 5 years, so prepare well for the Java interview Questions, I suggest you take a look on more Java Programming Interview Questions with answers on different topics of the core java posted in this xadmin website. Keep Learning!  All the Best!