|
21) How do you share session objects
between servlets and JSP?
Sharing sessions between a servlet and a JSP
page is straight forward. JSP makes it a little easy by creating a session
object and making it availabe already. In a servlet you would have to do it
yourself. This is how:
//create a session if one is not created already now
HttpSession session = request.getSession(true);
//assign the session variable to a value.
session.putValue("variable","value");
in the jsp page this is how you get the
session value:
<%
session.getValue("varible");
%>
22) What is a
servlet?
A servlet is a way of extending your web
server with a Java program to perform tasks previously dealt with by CGI
scripts or proprietary server extension frameworks.
23) Is there any method to unload a servlet
from Web Server memory without restarting the
server?
There is no standard method/mechanism to
unload a servlet from memory. Some servers, like JWS, provide the means to
load and unload servlets from their administration module. Others, like
Tomcat, require you to just replace the WAR file.
24) What distinguishes a JavaBean from a
Servlet?
JavaBeans are a set of rules to follow to
create reusable software components, or beans. This contains properties and
events. At the end you have a component which could be examined by a program
(like an IDE) to allow the user of your JavaBean component to configure it and
to run in its Java programs.
Servlets are Java classes running in a Servlet
engine implementing a particular interface: Servlet, forcing you to implement
some methods (service()). The servlet is an extension of your web server where
this servlet is running on and only lets you know when a user requests a GET
or POST calls from a web page to your servlet.
So, both have nothing in common except
Java.
25) How much data we can store in a session
object?
Any amount of data can be stored there because
the session is kept on the server side.
The only limitation is sessionId length, which
shouldn't exceed ~4000 bytes - this limitation is implied by HTTP header
length limitation to 4Kb since sessionId may be stored in the cookie or
encoded in URL (using "URL rewriting") and the cookie specification
says the size of cookie as well as HTTP request (e.g. GET /document.html\n)
cannot be longer then 4kb.
26) What is the difference between the
doGet and doPost methods?
doGet is called in response to an HTTP GET
request. This happens when users click on a link, or enter a URL into the
browser's address bar. It also happens with some HTML FORMs (those with
METHOD="GET" specified in the FORM tag).
doPost is called in response to an HTTP POST
request. This happens with some HTML FORMs (those with METHOD="POST" specified
in the FORM tag).
Both methods are called by the default
(superclass) implementation of service in
the HttpServlet base class. You should override
one or both to perform your servlet's actions. You probably shouldn't override
service().
27) What is the difference between
encodeRedirectUrl and
encodeURL?
encodeURL and encodeRedirectURL are methods of
the HttpResponse object. Both rewrite a raw URL to include session data if
necessary. (If cookies are on, both are no-ops.)
encodeURL is for normal links inside your HTML
pages.
encodeRedirectURL is for a link you're passing
to response.sendRedirect(). It has slightly different syntax requirements too
gory to get into here.
28) Can I use System.exit() in
servlets?
Gack! No no no no no...
At best, you'll get a security exception. At
worst, you'll make the servlet engine, or maybe the entire web server, quit.
You don't really want to do that, huh? :-)
29) I am opening a single JDBC connection
in my init() method. Do I need to synchronize on the Connection or the
Statement object?
You shouldn't have to. If your JDBC driver
supports multiple connections, then the various createStatement methods will
give you a thread-safe, reentrant, independent Statement that should work OK,
even if other requests/threads are also accessing other Statements on the same
Connection.
Of course, crossing your fingers never
hurts... Many early JDBC drivers were not re-entrant. The modern versions of
JDBC drivers should work OK, but there are never any guarantees.
Using connection pooling will avoid the whole
issue, plus will lead to improved performance. See
this FAQ for
more information.
30) How can I determine the name and
version number of the servlet or JSP engine that I am
using?
From within a servlet, you can invoke the
ServletContext.getServerInfo() method as follows:
String thisServer= getServletConfig().getServletContext().getServerInfo();
If you are using JSP, you can use this
expression:
<%= application.getServerInfo() %>
java6 ejb3 jsf hibernate eclipse ajax groovy spring seam java struts webservice j2me guice java5 jca tapestry soa linux ria books
|