Friday, June 25, 2010

SERVLET

QUESTION: What are the phases in JSP?
ANSWER: a) Translation phase ? conversion of JSP to a Servlet source, and then Compilation of servlet source into a class file. The translation phase is typically carried out by the JSP engine itself, when it receives an incoming request for the JSP page for the first time

b) init(), service() and destroy() method as usual as Servlets.

QUESTION: How many cookies can one set in the response object of the servlet? Also, are there any restrictions on the size of cookies?
ANSWER: If the client is using Netscape, the browser can receive and store 300 total cookies

4 kilobytes per cookie (including name)
20 cookies per server or domain

QUESTION: What?s the difference between sendRedirect( ) and forward( ) methods?
ANSWER: A sendRedirect method creates a new request (it?s also reflected in browser?s URL ) where as forward method forwards the same request to the new target(hence the chnge is NOT reflected in browser?s URL).
The previous request scope objects are no longer available after a redirect because it results in a new request, but it?s available in forward.
SendRedirectis slower compared to forward.

QUESTION: Is there some sort of event that happens when a session object gets bound or unbound to the session?
ANSWER: HttpSessionBindingListener will hear the events When an object is added and/or remove from the session object, or when the session is invalidated, in which case the objects are first removed from the session, whether the session is invalidated manually or automatically (timeout).

QUESTION: What do the differing levels of bean storage (page, session, app) mean?
ANSWER: page life time - NO storage. This is the same as declaring the variable in a scriptlet and using it from there.
session life time - request.getSession(true).putValue "myKey", myObj);
application level ? getServletConfig().getServletContext().setAttribute("myKey ",myObj )
request level - The storage exists for the lifetime of the request, which may be forwarded between jsp's and servlets

QUESTION: Is it true that servlet containers service each request by creating a new thread? If that is true, how does a container handle a sudden dramatic surge in incoming requests without significant performance degradation?
ANSWER: The implementation depends on the Servlet engine. For each request generally, a new Thread is created. But to give performance boost, most containers, create and maintain a thread pool at the server startup time. To service a request, they simply borrow a thread from the pool and when they are done, return it to the pool.
For this thread pool, upper bound and lower bound is maintained. Upper bound prevents the resource exhaustion problem associated with unlimited thread allocation. The lower bound can instruct the pool not to keep too many idle threads, freeing them if needed.

QUESTION: Can I just abort processing a JSP?
ANSWER: Yes. Because your JSP is just a servlet method, you can just put (whereever necessary) a < % return; % >

QUESTION: What is URL Encoding and URL Decoding ?
ANSWER: URL encoding is the method of replacing all the spaces and other extra characters into their corresponding Hex Characters and Decoding is the reverse process converting all Hex Characters back their normal form.