|
91) Suppose I have 2 servers, server1 and
server2. How can I take data in a cookie from server1, and send it to
server2?
You'll have to create a (new) similar cookie
on server 2.
Have a ReadCookieServlet running on
server1 that
·
Reads the cookie, using
request.getCookies()
·
Redirects to
WriteCookieServlet running on server2, passing the cookie name, value
and expiration date as request parameters, using
response.sendRedirect().
Have a WriteCookieServlet running on
server2 that
·
Reads the cookie name,
value and expiration date request parameters, using
request.getParameter().
·
Creates a similar cookie,
using response.addCookie().
92) How can I pass data from a servlet
running in one context (webapp) to a servlet running in another
context?
There are three ways I can think of off the
top of my head:
1.
Store the information you
want to share in a persistant format, such as in a file system or database.
That way, any servlet that is running in a JVM that can "see" these resources
can get to this information.
2.
If persisting this
information is not an option, you can bind this information to a context that
is accessible to all servlet contexts, such as the application server's
context. This way, you can keep the data you want to share in memory.
3.
Use the old fashion way
of passing information to a servlet - HTTP. One servlet could foward a request
to another servlet and include the data that needs to be shared as parameters
in the request.
93) How can I write an "error page" -- that
is, a servlet or JSP to report errors of other servlets?
The Servlet 2.2 specification allows you to
specify an error page (a servlet or a JSP) for different kinds of HTTP errors
or ServletExceptions. You can specify this in deployment descriptor of the web
application as:
<error-page>
<exception-type>FooException</exception-type>
<location>/error.jsp</location>
</error-page>
where FooException is a subclass of
ServletException.
The web container invokes this servlet in case
of errors, and you can access the following information from the request
object of error servlet/JSP: error code, exception type, and a message.
94) What is the difference between
ServletContext and ServletConfig?
A ServletContext
represents the context in a servlet container of a servlet instance operates.
A servlet container can have several contexts (or web applications) at one
time. Each servlet instance is running in one of these contexts. All servlets
instances running in the same context are part of the same web application
and, therefore, share common resources. A servlet accesses these shared
resource (such as a RequestDispatcher and
application properties) through the ServletContext
object.
This notion of a web application became very
significant upon the Servlet 2.1 API, where you could deploy an entire web
application in a WAR file. Notice that I always said "servlet instance", not
servlet. That is because the same servlet can be used in several web
applications at one time. In fact, this may be common if there is a generic
controller servlet that can be configured at run time for a specific
application. Then, you would have several instances of the same servlet
running, each possibly having different configurations.
This is where the ServletConfig
comes in. This object defines how a servlet is to be configured is passed to a
servlet in its init method. Most
servlet containers provide a way to configure a servlet at run-time (usually
through flat file) and set up its initial parameters. The container, in turn,
passes these parameters to the servlet via the ServetConfig.
95) Under what circumstances will a servlet
be reloaded?
That depends on the Servlet container.
Most of the Servlet containers reload the
servlet only it detects the code change in the Servlet, not in the referenced
classes.
In Tomcat's server.xml deployment descriptor,
if you have mentioned
<Context path="/myApp"
docBase="D:/myApp/webDev"
crossContext="true"
debug="0"
reloadable="true"
trusted="false" >
</Context>
The reloadable = true makes the magic.
Every time the Servlet container detects that the Servlet code is changed, it
will call the destroy on the currently loaded Servlet and reload the new code.
But if the class that is referenced by the
Servlet changes, then the Servlet will not get loaded. You will have to change
the timestamp of the servlet or stop-start the server to have the new class in
the container memory.
96) What is a Servlet
Filter?
A filter is basically a component that is
invoked whenever a resource is invoked for which the filter is mapped. The
resource can be something like a servlet, or a URL pattern. A filter normally
works on the request, response, or header attributes, and does not itself send
a response to the client.
97) I am using the RequestDispatcher's
forward() method to redirect to a JSP. The problem is that the jsp's url is
now relative to the servlet's url and all my url's in the jsp such as <img
src="pic.gif"> will be corrupt. How do I solve this
problem?
You can use absolute urls like:
<BODY>
<% String base = request.getContextPath(); %>
<IMG src="<%=base%>/img/pic.gif">
</BODY>
or write out a BASE tag like:
<% String base = request.getContextPath(); %>
<HEAD>
<BASE HREF="<%=base%>">
</HEAD>
<BODY>
<IMG src="img/pic.gif">
</BODY>
That should take care of the problem.
98) How can I return a readily available
(static) HTML page to the user instead of generating it in the
servlet?
To solve your problem, you can either send a
"Redirect" back to the client or use a RequestDispatcher
and forward your request to another page:
1.
Redirect:
A redirection is made using the HttpServletResponse
object:
2. if(condition) {
3. response.sendRedirect("page1.html");
4. } else {
5. response.sendRedirect("page2.html");
6. }
7.
RequestDispatcher:
A request dispatcher can be obtained through the ServletContext.
It can be used to include another page or to forward to it.
8. if(condition) {
9. this.getServletContext()
10. .getRequestDispatcher("page1.html").forward();
11. } else {
12. this.getServletContext()
13. .getRequestDispatcher("page2.html").forward();
14. }
Both solutions require, that the pages are
available in you document root. If they are located somewhere else on your
filesystem, you have to open the file manually and copy their content to the
output writer.
If your application server is set up in
combination with a normal web server like Apache, you should use solution (1),
because the the web server usually serves static files much faster than the
application server.
99) What is the difference between static
variables and instance variables in a servlet?
According to the Java Language definition, a
static variable is shared among all instances of a class, where a non-static
variable -- also called an instance variable -- is specific to a single
instance of that class.
According to the Servlet specification, a
servlet that does not declare SingleThreadModel usually has one and only one
instance, shared among all concurrent requests hitting that servlet.
That means that, in servlets (and other
multithreaded applications), an instance variable behaves very much like a
static variable, since it is shared among all threads. You have to be very
careful about synchronizing access to shared data.
The big difference between instance variables
and static variables comes when you have configured your servlet engine to
instantiate two instances of the same servlet class, but with different
init parameters. In this case, there will be two instances of the same
servlet class, which means two sets of instance variables, but only one set of
static variables.
Remember that you can store data in lots of
different places in a servlet. To wit:
·
Local
variables - for loop iterators,
result sets, and so forth
·
Request
attributes - for data that must be
passed to other servlets invoked with the RequestDispatcher
·
Session
attributes - persists for all
future requests from the current user only
·
Instance
variables - for data that persists
for the life of the servlet, shared with all concurrent users
·
Static
variables - for data that persists
for the life of the application, shared with all concurrent users -- including
any other servlet instances that were instantiated with different init
parameters
·
Context
attributes - for data that must
persist for the life of the application, and be shared with all other servlets
100) How can I share data between two
different web applications?
Different servlets may share data within one
application via ServletContext. If you have a compelling to put the servlets
in different applications, you may wanna consider using EJBs.
java6 ejb3 jsf hibernate eclipse ajax groovy spring seam java struts webservice j2me guice java5 jca tapestry soa linux ria books
|