Servlet Interview Questions and Answers
Question - 61 : - What do you mean by Servlet Context?
Answer - 61 : -
Servlet Context is referred to as an object that has information about the application and the Web Container. Using the Servlet context we can log events, get the URL of the specific resource, and store the attributes for other servlets to use.
The important methods of servlet context are as follows:
- getInitParameter (): return the value of parameter.
- getInitParameterNames (): returns the name of parameter.
- void setAttribute (): used to set the values of attributes.
- void getAttribute (): used to get the values of attributes.
- void removeAttribute (): used to remove the attribute.
Question - 62 : - Why doesn’t a Servlet include main()? How does it work?
Answer - 62 : -
Servlets don’t have a main() method. Because servlets are executed using web containers. When a client places request for a servlet, then the server hands the requests to the web container where the servlet is deployed.
Question - 63 : - Why do we use sendredirect() method?
Answer - 63 : -
The sendredirect() method basically works at the client-side. It is used to redirect the response to another resource like Servlet, JSP, HTML.
The syntax for sendredirect() method is as follows:
1 void send Redirect(URL);
Example:
1 response.sendredirect(“http://www.google.com”);
Question - 64 : - What do you mean by the Servlet Chaining?
Answer - 64 : -
Servlet Looping or Chaining is a process where the output of one servlet is given as an input to another servlet and the last servlet output is considered as the actual output which is provided to the client.
Question - 65 : - Explain Web Container.
Answer - 65 : -
A web container or a Servlet container is used to interact with the Servlet and includes all the Servlet, JSP, XML files inside it. Web Container’s responsibility is to manage the life cycle of a servlet and to help to map the URL of a specific servlet. A web container is also used creates the object of a servlet.
Question - 66 : - How can you create a session in servlet?
Answer - 66 : -
We can get HttpSession object by calling the public method getSession() of HttpServletRequest. The following code segment will help us.
1 HttpSession session = request.getSession();
Question - 67 : - Explain JSESSIONID and when is it created?
Answer - 67 : -
JSESSIONID is basically a cookie that is used to manage the session in a Java Web Application. It is created by the Web Container when a new session is created.
Question - 68 : - What is the difference between sendRedirect() and Forward() in a Servlet?
Answer - 68 : -
The difference between sendRedirect() and Forward() can be explained as follows:
sendRedirect():
- sendRedirect() method is declared in HttpServletResponse Interface.
- The syntax for the function is as follows:
1 void sendRedirect(String URL)
- This method redirects the client request for further processing, the new location is available on a different server or different context. The web container handles this and transfers the request using the browser, this request is visible in the browser in the form of a new request. It is also called a client-side redirect.
Forward():
- Forward() method is declared in the RequestDispatcher Interface.
- The syntax for the function is as follows:
1 forward(ServletRequest request, ServletResponse response)
- This passes the request to another resource for processing within the same server, another resource could be any servlet, JSP page. Web container handles the Forward() method. When we call Forward() method, a request is sent to another resource without informing the client, about the resource that will handle the request. It will be mentioned on the requestDispatcher object which we can be got in two ways. Either using ServletContext or Request.
Question - 69 : - Explain the working of service() method of a servlet.
Answer - 69 : -
The service() method is actually the main method that is expected to perform the actual task. The servlet container calls the service() method to handle requests coming from the client/browsers and to provide the response back to the client.
Each time the server receives a request for a servlet, the server creates a new thread and calls for the service. The service() method checks the HTTP request type and calls the respective methods as required.
The sample code for the same is as follows:
- public void service(ServletRequest request, ServletResponse response)
- throws ServletException, IOException{
- }
The container calls the service() method and service method invokes doGet(), doPost(), doPut(), doDelete(), methods as needed. So you have nothing to do with service() method but you override either doGet() or doPost() depending on the request you receive from the client-end.
Question - 70 : - Can you send an Authentication error from a Servlet?
Answer - 70 : -
Yes, we can use setStatus(statuscode) method of HttpServletResponse to send an authentication error. All we have to do is to set an error code and a valid reason along with the error code.
1 response.sendError(404, "Page not Found!!!" );