• +91 9971497814
  • info@interviewmaterial.com

Servlet Interview Questions Answers

Question 1 : Given the request path below, which are context path, servlet path and path info?

Answer 1 : /bookstore/education/index.html context path: /bookstore servlet path: /education path info: /index.html

Question 2 : Request parameter How to find whether a parameter exists in the request object?

Answer 2 : 1.boolean hasFoo = !(request.getParameter("foo") == null || request.getParameter("foo").equals("")); 2. boolean hasParameter = request.getParameterMap().contains(theParameter); (which works in Servlet 2.3+)

Question 3 : What is filter? Can filter be used as request or response?

Answer 3 : A filter is a reusable piece of code that can transform the content of HTTP requests,responses, and header information. Filters do not generally create a response or respond to a request as servlets do, rather they modify or adapt the requests for a resource, and modify or adapt responses from a resource.

Question 4 : What is Servlet?

Answer 4 : A servlet is a Java technology-based Web component, managed by a container called servlet container or servlet engine, that generates dynamic content and interacts with web clients via a request\/response paradigm.

Question 5 : When is the servlet instance created in the life cycle of servlet? What is the importance of configuring a servlet?

Answer 5 : An instance of servlet is created when the servlet is loaded for the first time in the container. Init() method is used to configure this servlet instance. This method is called only once in the life time of a servlet, hence it makes sense to write all those configuration details about a servlet which are required for the whole life of a servlet in this method.

Question 6 : Why is Servlet so popular?

Answer 6 : Because servlets are platform-independent Java classes that are compiled to platform-neutral byte code that can be loaded dynamically into and run by a Java technology-enabled Web server.

Question 7 : When we don't write any constructor for the servlet, how does container create an instance of servlet?

Answer 7 : Container creates instance of servlet by calling Class.forName(className).newInstance().  

Question 8 : Why don't we write a constructor in a servlet?

Answer 8 : Container writes a no argument constructor for our servlet.

Question 9 : What mechanisms are used by a Servlet Container to maintain session information?

Answer 9 : Cookies, URL rewriting, and HTTPS protocol information are used to maintain session information

Question 10 : If a servlet is not properly initialized, what exception may be thrown?

Answer 10 : During initialization or service of a request, the servlet instance can throw an UnavailableException or a ServletException.

Servlet Contributors

krishan

Share your email for latest updates

Name:
Email:

Our partners