|
|
| |||||||
| |||||||||
|
| 3. The Servlet EnvironmentThis chapter shows how to access resources of the Web Server and communicate with Servlets and other kinds of active resources (e.g. JSP documents, CGI programs). The examples in this chapter are often only fragments of Java source code and not necessarily complete Servlets. 3.1 Inter-Servlet CommunicationThis section shows how to
The inter-Servlet communication method which is described in this section
can only be used with Servlet engines which implement version 1.0 or 2.0
of the Servlet API. It will not work with Servlet engines which comply
strictly to version 2.1. The new way of doing inter-Servlet communication
which was introduced in the 2.1 API is described in detail in sections
3.2 and 3.5.
Servlets are not alone in a Web Server. They have access to other
Servlets in the same Servlet Context (usually a Servlet directory),
represented by an instance of A Servlet can get a list of all other Servlets in the Servlet Context
by calling After obtaining the reference to another Servlet that Servlet's
methods can be called. Methods which are not declared in
Note that in Java the identity of a class is not only defined by the class
name but also by the This means that classes which are loaded by a Servlet class loader
cannot be used for inter-Servlet communication. A class literal
A way to overcome this problem is using a superclass or an interface which
is loaded by the system loader and thus shared by all Servlets. In a Web
Server which is written in Java those classes are usually located in the
class path (as defined by the Example. Servlet 1: public class FooServlet extends HttpServlet 2: { 3: protected void doGet(HttpServletRequest req, 4: HttpServletResponse res) 5: throws ServletException, IOException 6: { 7: ... 8: ServletContext context = getServletConfig().getServletContext(); 9: BarInterface bar = (BarInterface)context.getServlet("BarServlet"); 10: bar.bar(); 11: .. 12: } 13: 14: ... 15: } 1: public interface BarInterface 2: { 3: public void bar(); 4: } 1: public class BarServlet extends HttpServlet implements BarInterface 2: { 3: public void bar() 4: { 5: System.err.println(""bar() called""); 6: } 7: 8: ... 9: } 3.2 Communication with Active Server Resources [API 2.1]This section shows how to
A Servlet can make a request to an active resource on the web server just like a client can (by requesting a URL). The Servlet API supports a more direct way of accessing server resources from within a Servlet which is running in the server than opening a socket connection back to the server. A Servlet can either hand off a request to a different resource or include the response which is created by that resource in its own response. It is also possible to supply user-defined data when calling an active resource which provides for an elegant way of doing inter-Servlet communication.
See section 3.1
for an inter-Servlet communication method which can be used with
Servlet API versions 1.0 and 2.0.
Example. We're building an online shop with an
A second Servlet, the public class PresentationServlet extends HttpServlet { private static class ItemNotFoundException extends Exception { ItemNotFoundException() { super("Item not found"); } } protected void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { String item = req.getParameter("item"); if(item == null) { req.setAttibute("exception", new ItemNotFoundException()); getServletContext() .getRequestDispatcher("/servlet/ErrorServlet") .forward(req, res); } else { res.setContentType("text/html"); PrintWriter out = res.getWriter(); out.print("<HTML><HEAD><TITLE>Item " + item + "</TITLE>"+ "</HEAD><BODY>Item " + item + ":<P>"); getServletContext() .getRequestDispatcher("/servlet/ItemServlet?item="+item) .include(req, res); out.print("</BODY></HTML>"); } } } In the If no The Note that a server which supports load balancing could run the Servlets on different JVMs. All custom request attributes should be serializable to allow them to be moved from one JVM to another. 3.3 Accessing Passive Server Resources [API 2.1]This section shows how to
Passive server resources (e.g. static HTML pages which are stored in
local files) are not accessed with If you only want to read the resource's body you can directly
ask the
In Servlets which have to use version 1.0 or 2.0 of the Servlet API
you can use the
ServletContext method
getRealPath(String path) to find a path in the local
file system to a resource. This method is
less general than the one described above because it doesn't allow
you to access resources which are not stored in local files.
3.4 Accessing Servlet ResourcesThis section shows how to
A Servlet may need to access additional resources like configuration
files whose locations should not need to be specified in init
parameters. Those resources can be accessed with the methods
Example. The following code gets an
InputStream confIn =
getClass().getResourceAsStream("myservlet.cfg");
Note that the Servlet engine's Servlet class loader must implement
the 3.5 Sharing Data Between Servlets [API 2.1]This section shows how to
Version 2.1 of the Servlet API offers a new way of sharing
named objects between all the Servlets in a
Servlet context (and also other contexts, as you'll
see below) by binding the objects to the The
The separation of Servlets into Servlet contexts depends on the
Servlet engine. The |
| ||||||
|
This page's content was last updated 1999-11-04. © 1997-2005 Stefan Zeiger (http://www.novocode.com/contact.html). All rights reserved. Page Location: http://www.novocode.com/doc/servlet-essentials/chapter3.html |
| |||||