Saturday, March 14, 2009

Do objects stored in a HTTP Session need to be serializable Or can it

Question :Do objects stored in a HTTP Session need to be serializable? Or can it
store any object? (Servlets)


Answer :Yes, the objects need to be serializable, but only if your servlet container
supports persistent sessions. Most lightweight servlet engines (like
Tomcat) do not support this. However, many EJB-enabled servlet engines
do. Even if your engine does support persistent sessions, it is usually
possible to disable this feature.

What is URL Encoding and URL Decoding (Servlets)

Question :What is URL Encoding and URL Decoding ? (Servlets)
Answer :URL encoding is the method of replacing all the spaces and other extra characters into their Characters and Decoding is the reverse process converting all Hex Characters back their normal For Example consider this URL, /ServletsDirectory/Hello'servlet/
When Encoded using URLEncoder.encode("/ServletsDirectory/Hello'servlet/") the output is
http%3A%2F%2Fwww.javacommerce.com%2FServlets+Directory%2FHello%27servlet%2FThis back using
URLDecoder.decode("http%3A%2F%2Fwww.javacommerce.com%2FServlets+Directory%2FHello%

Can I just abort processing a JSP (Servlets)

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

Is it true that servlet containers service each request by creating a new

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? (Servlets)


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.

What do the differing levels of bean storage (page, session, app) mean (Servlets)

Question :What do the differing levels of bean storage (page, session, app)
mean? (Servlets)

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

Is there some sort of event that happens when a session object gets

Question :Is there some sort of event that happens when a session object gets
bound or unbound to the session? (Servlets)


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).

What?s the difference between sendRedirect( ) and forward( )

Question :What?s the difference between sendRedirect( ) and forward( )
methods? (Servlets)


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.
Your Ad Here