• +91 9971497814
  • info@interviewmaterial.com

Servlet Interview Questions Answers

Servlet Interview Questions Answers

Question - 1 : - When a client request is sent to the servlet container, how does the container choose which servlet to invoke?

Answer - 1 : - The servlet container determines which servlet to invoke based on the configuration of its servlets, and calls it with objects representing the request and response.

Question - 2 : - Can we  use the constructor, instead of init(), to initialize servlet?

Answer - 2 : - Yes , of course you can use the constructor instead of init(). There's nothing to stop you. But you shouldn't. The original reason for init() was that ancient versions of Java couldn't dynamically invoke constructors with arguments, so there was no way to give the constructur a ServletConfig. That no longer applies, but servlet containers still will only call your no-arg constructor. So you won't have access to a ServletConfig or ServletContext.

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

Answer - 3 : - 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.

Question - 4 : - What is servlet context ?

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

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

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

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

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

Answer - 7 : - 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 - 8 : - What mechanisms are used by a Servlet Container to maintain session information?

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

Question - 9 : - What is servlet container?

Answer - 9 : - The servlet container is a part of a Web server or application server that provides the network services over which requests and responses are sent, decodes MIME-based requests, and formats MIME-based responses. A servlet container also contains and manages servlets through their lifecycle.

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

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


Selected

 

Servlet Contributors

krishan

Share your email for latest updates

Name:
Email:

Our partners