• +91 9971497814
  • info@interviewmaterial.com

Computer Interview Questions Answers

Servlet Interview Questions Answers

Question - 11 : - What is servlet context ?

Answer - 11 : - The servlet context is an object that contains a servlet's view of the Web application within which the servlet is running. Using the context, a servlet can log events, obtain URL references to resources, and set and store attributes that other servlets in the context can use. (answer supplied by Sun's tutorial).

Question - 12 : - Why is Servlet so popular?

Answer - 12 : - 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 - 13 : - What is filter? Can filter be used as request or response?

Answer - 13 : - 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 - 14 : - When is the servlet instance created in the life cycle of servlet? What is the importance of configuring a servlet?

Answer - 14 : - 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 - 15 : - Request parameter How to find whether a parameter exists in the request object?

Answer - 15 : - 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 - 16 : - How can I send user authentication information while making URL Connection?

Answer - 16 : - You'll want to use HttpURLConnection.setRequestProperty and set all the appropriate headers to HTTP authorization.

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

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

Question - 18 : - Once the destroy() method is called by the container, will the servlet be immediately destroyed? What happens to the tasks(threads) that the servlet might be executing at that time?

Answer - 18 : - Yes, but Before calling the destroy() method, the servlet container waits for the remaining threads that are executing the servlet’s service() method to finish.

Question - 19 : - What is session?

Answer - 19 : - The session is an object used by a servlet to track a user's interaction with a Web application across multiple HTTP requests.

Question - 20 : - Explain the life cycle of Servlet.

Answer - 20 : - Loaded(by the container for first request or on start up if config file suggests load-on-startup), initialized( using init()), service(service() or doGet() or doPost()..), destroy(destroy()) and unloaded.

Computer Contributors

Rajeev Katiyar
Yes Baroda

Share your email for latest updates


Our partners