Servlet Interview Questions and Answers
Question - 31 : - What is the use of RequestDispatcher Interface?
Answer - 31 : -
The RequestDispatcher interface defines the object that receives the request from the client and dispatches it to the resources such as a servlet, JSP, HTML file. The RequestDispatcher interface has the following two methods:
1
public void forward(ServletRequest request, ServletResponse response)
Forwards request from one servlet to another resource like servlet, JSP, HTML etc.
1
public void include(ServletRequest request, ServletResponse response)
Includes the content of the resource such as a servlet, JSP, and HTML in the response.
Question - 32 : - Can a JSP be called using a Servlet?
Answer - 32 : -
Yes, Servlet can call a JSP using RequestDispatcher interface.
Example:
1 RequestDispatcher reqdis=request.getRequestDispatcher("log.jsp");
2 reqdis.forward(request,response);
Question - 33 : - Explain the Servlet Filter.
Answer - 33 : - A Filter is defined as a pluggable object that is invoked either at the pre-processing or post-processing of a request.
Question - 34 : - Why do we need Servlet Filter?
Answer - 34 : -
We need Servlet Filters for the following reasons:
- Logging the request parameters to log files.
- Authentication and Authorization of the request for the needed resources.
- Formatting of the request body/header before sending it to the servlet.
- Compressing response data sent to the client.
- Change the response by adding some cookies and header information.
Question - 35 : - Why is init() method is used in Servlets?
Answer - 35 : - The init() method is used to create or load some data that will be used throughout the life of the servlet.
Question - 36 : - What is load-on-startup in Servlet?
Answer - 36 : - The load-on-startup element of servlet in web.xml is used to load the servlet at the time of deploying the project or the server to start. This saves time for the response of the first request.
Question - 37 : - What is the ServletConfig object?
Answer - 37 : - javax.servlet.ServletConfig is used to pass configuration information to Servlet. Every servlet has its own ServletConfig object and servlet container is responsible for instantiating this object. We can provide servlet init parameters in web.xml file or through use of WebInitParam annotation.
Question - 38 : - What do you mean by MIME type?
Answer - 38 : - The “Content-Type” response header is called MIME (Multipurpose Internet Mail Extensions) Type. The server sends MIME type to the client to let the client know the kind of data it’s sending. It helps the client in rendering the data for the user. Some of the most commonly used mime types are text/HTML, text/XML, application/XML.
Question - 39 : - Can we fetch the attributes related to a servlet on a different servlet?
Answer - 39 : -
The attributes of another servlet that we are looking for can be accessed by using its name. The syntax is described as follows:
- Context.setAttribute (“name”,” value”)
- Context.getAttribute (“name”)
Question - 40 : - Can you create a Deadlock condition on a servlet?
Answer - 40 : - Yes, a Deadlock situation can be created on a servlet by calling doPost() method inside doGet() method, or by calling a doGet() method inside doPost() method will successfully create a deadlock situation for a servlet.