Servlets FAQ

Servlet Life Cycle?

The life cycle of a servlet is controlled by the container in which the servlet has been deployed. When a request is mapped to a servlet, the container performs the following steps. 

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. 
  • Invokes the service method, passing a request and response object.  
  • If the container needs to remove the servlet, it finalizes the servlet by calling the servlet's destroy method.

—————

about load on startup in Servlet ?

Used to decide whether servlet will be " lazily " or " eagerly " loaded 
Specified in web.xml file 

If the value is not specified or is a Number < 0 then it means " lazy loading " 

What " lazy loading " means is that servlet is NOT loaded by container on startup 

Servlet in this case is loaded on the first client request - so the first client can experience poor performance

" Eager " loading means that the servlet is initialised on container startup 

If there are two servelts A & B with values 0 & 1 than it means that Servlet A ( having value = 0 ) will be loaded first 

So if there are more than one servlet this element specifies the order of loading - lower integer values ( including zero ) are loaded first 

If you specify this element but do not provide the value - even then the servlet will be the first servlet that gets loaded 

To ensure that servlet follows " lazy loading " - do not provide this entry at all in web.xml file OR provide a negative number

—————

What is the difference between ServletContext and PageContext?

ServletContext: Gives the information about the container 
PageContext: Gives the information about the Request

—————

Difference forward() and response.sendRedirect() ?

difference between the two is that sendRedirect always sends a header back to the client/browser. this header then contains the resource(page/servlet) which u wanted to be redirected. the browser uses this header to make another fresh request. thus sendRedirect has a overhead as to the extra remort trip being incurred. its like any other Http request being generated by ur browser. the advantage is that u can point to any resource(whether on the same domain or some other domain). for eg if sendRedirect was called at www.mydomain.com then it can also be used to redirect a call to a resource on www.techfaq360.com. 

where as in case of forward() call, the above is not true. resources from the server, where the fwd. call was made, can only be requested for. but the major diff between the two is that forward just routes the request to the new resources which u specify in ur forward call. that means this route is made by the servlet engine at the server level only. no headers r sent to the browser which makes this very eficient. also the request and response objects remain the same both from where the forward call was made and the resource which was called. 

In case of forward you can get the data from request object but in case of sendRedirect() you can get from session object, request object is not avilable. 

—————

Why don't we write a constructor in a servlet?

Container writes a no argument constructor for our servlet.

—————

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

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

—————

What is servlet container?

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.

—————

Why IllegalStateException in jsp/servet?

by attempting to redirect from within the middle of a JSP page you will get IllegalStateException . For Example 
<div>News </div> 
<% 
if ("not logged in") 
response.sendRedirect("login.jsp"); 
%> 
<div>more news</div> 

You can avoid this bu using return ; Example : 
<div>News </div> 
<% 
if ("not logged in") 
response.sendRedirect("login.jsp"); 
return; 
%> 
<div>more news</div> 

or 
public void doGet(HttpServletRequest request, 
HttpServletResponse response) throws ServletException, IOException{ 

if("client".equals(request.getParameter("user_type"))){ 
response.sendRedirect("client-screen.jsp"); 
return; // <------- This return statement prevents any further writing to the outputStream 


// 
// Other code that may write to the outputStream.... 
// 

—————

What's the difference between response.sendRedirect() and requestDispatcher.forward() ?

response.sendRedirect() : 


This is complete new request to browser. 
Request is not maintained so data stored in request object not avilable to redirect page. 
You have to store data in session to get in the reditect jsp. 
You can pass data like response.sendRedirect("/techfaq360.jsp?name=satya). then in the techfaq360.jsp you have to get name using request.getParameter("name"); 

This is fresh call to server. 

For example .. you forwarded from /login to login.jsp 
in the browser you can see /login.jsp.... 

requestDispatcher.forward() :

This not new request. Just transfer the content to forwarded jsp. 
You can store data in request and able to get in redirect jsp. 

For example .. you forwarded from /login to login.jsp 
in the browser you can see /login .... 

Disadvantage - browser refresh call to /loginj again. may be duplicate data submit. 

—————

What is the difference between ServletContext and ServletConfig?

ServletContext Defines a set of methods that a servlet uses to communicate with its servlet container. 
ServletConfig is a servlet configuration object used by a servlet container used to pass information to a servlet during initialization. All of its initialization parameters can ONLY be set in deployment descriptor. 

The ServletContext object is contained within the ServletConfig object, which the Web server provides the servlet when the servlet is initialized. 

You can specify param-value pairs for ServletContext object in <context-param> tags in web.xml file. 

The ServletConfig parameters are specified for a particular servlet and are unknown to other servlets. 

The ServletContext parameters are specified for an entire application outside of any particular servlet and are available to all the servlets within that application. 

—————


Topic: Servlets FAQ

No comments found.