Servlet Interview Questions - Page 2

Question: What are the lifecycle methods of Servlet? Answer: The interface javax.servlet.Servlet, defines the three life-cycle methods.

Servlet Interview Questions - Page 2

Servlet Interview Questions - Page 2

     

Question: What are the lifecycle methods of Servlet?
Answer:
The interface javax.servlet.Servlet,  defines the three life-cycle methods. These are:
public void init(ServletConfig config) throws ServletException
public void service( ServletRequest req, ServletResponse res) throws ServletException, IOException
public void destroy()
The container manages the lifecycle of the Servlet. When a new request come to a Servlet, the container performs the following steps.
1. If an instance of the servlet does not exist, the web container
  * Loads the servlet class.
  * Creates an instance of the servlet class.
  * Initializes the servlet instance by calling the init method. Initialization is covered in Initializing a Servlet.
2. The container invokes the service method, passing request and response objects.
3. To remove the servlet, container finalizes the servlet by calling the servlet's destroy method.

Question: What are the type of protocols supported by HttpServlet?
Answer:
It extends the GenericServlet base class and provides an framework for handling the HTTP protocol. So, HttpServlet only supports HTTP and HTTPS protocol.

Question: What are the directory Structure of Web Application?
Answer:
Web component follows the standard directory structure defined in the J2EE specification. 

Directory Structure of Web Component
 /
   index.htm, JSP, Images etc..
  Web-inf
   web.xml
   classes
     servlet classes
   lib
     jar files

Question: What is ServletContext?
Answer:
ServletContext is an Interface that defines a set of methods that a servlet uses to communicate with its servlet container, for example, to get the MIME type of a file, dispatch requests, or write to a log file. There is one context per "web application" per Java Virtual Machine. (A "web application" is a collection of servlets and content installed under a specific subset of the server's URL namespace such as /catalog and possibly installed via a .war file.)

Question: What is meant by Pre-initialization of Servlet?
Answer:
When servlet container is loaded, all the servlets defined in the web.xml file does not initialized by default. But the container receives the request it loads the servlet. But in some cases if you want your servlet to be initialized when context is loaded, you have to use a concept called pre-initialization of Servlet. In case of Pre-initialization, the servlet is loaded when context is loaded. You can specify <load-on-startup>1</load-on-startup>
in between the <servlet></servlet> tag.

Question: What mechanisms are used by a Servlet Container to maintain session information?
Answer:
Servlet Container uses Cookies, URL rewriting, and HTTPS protocol information to maintain the session.

Question: What do you understand by servlet mapping?
Answer:
Servlet mapping defines an association between a URL pattern and a servlet. You can use one servlet to process a number of url pattern (request pattern). For example in case of Struts *.do url patterns are processed by Struts Controller Servlet.

Question: What must be implemented by all Servlets?
Answer:
The Servlet Interface must be implemented by all servlets.

Question: What are the differences between Servlet and Applet?
Answer:
Servlets are server side components that runs on the Servlet container. Applets are client side components and runs on the web browsers. Servlets have no GUI interface.

Question: What are the uses of Servlets?
Answer: *
Servlets are used to process the client request.
   * A Servlet can handle multiple request concurrently and be used to develop high performance system
   * A Servlet can be used to load balance among serveral servers, as Servlet can easily forward request.

Question:  What are the objects that are received when a servlets accepts call from client?
Answer: The objects are ServeltRequest  and
ServletResponse . The ServeltRequest encapsulates the communication from the client to the
server. While ServletResponse encapsulates the communication from the Servlet back to the client.