Takayuki Tachikawa wrote:

> Hi,
>
> I have some questions about RequestDispatcher class defined in API 2.1.
>
> 1) I can get an instance of RequestDispatcher by calling
>    ServletContext#getRequestDispatcher(String urlpath) method.
>    Here, if the urlpath is set like "/fooServlet", it works.
>    Can I specify a full URL path like "http://.../..." to the urlpath?
>    By using JSDK2.1ea's servletrunner, I could not get an instance of
>    RequestDispatcher for servlets on other servlet engines.
>

According to the spec, a full URI path is *not* allowed.  The reason for this
is that the URI path you use is supposed to be interpreted locally to the
servlet context, rather than globally to the URI space of the web server.
This will allow webmasters to install a servlet context attached to any URI
prefix they want to, and then move it somewhere else, without affecting the
paths that are used internally by a servlet.

The spec does allow for using the ServletContext.getContext() call to retrieve
the ServletContext for a different context -- usually a different application,
with its own servelts -- but whether or not this actually does what you want
depends on the servlet engine.  If not prevented by security-related
configuration settings, you will probably be able to look up the context, and
therefore get a RequestDispatcher, for contexts that are executing in the same
JVM as the calling servlet.

If you want to access a servlet from another servlet engine, the best thing to
do would be to use a java.net.URLConnection to have your servlet do an HTTP
request to the remote servlet.

>
> 2) Both cases when I called forward() and include() method,
>    if the first servlet is called by HTTP GET, the target servlet is
>    also called by doGet method, and if the first servlet is called by
>    HTTP POST, the target is also called by doPOST (i.e. both servlets
>    are called by same service method). When I use forward() method,
>    it has no problem. But when I use include(), I want to change it.
>    (e.g. first method is called by HTTP POST and target servlet is
>    called by doGet). is this feature supported in some servlet
> engines?
>    Or, I have to write a following code?
>
>         void doPOST(req, res) {
>                 doGET(req, res);
>         }
>

This sounds like an indirect result of the fact that if you pass the original
request object to your request dispatcher, then only the "path related" parts
of the request are modified -- in particular, the request method ("GET" or
"POST") will be identical to the original request in the destination servlet.

One way to avoid this, for your scenario, would be to write a "wrapper" class
around the original request that passed every call except getMethod() on to
the original request object, but always returned "GET" for getMethod().  Then,
you could pass the wrapped request to your request dispatcher object, instead
of the original one.  Alternatively, you could just make the forwarded-to (or
included) servlet deal correctly with either type of request.


>
> 3) In the API document, include() method in the RequestDispatcher
>    class throws IOException
>    "if the ServletOutputStream or a writer had already been obtained
>     from the response object"
>    Is this correct?
>

Hmmm, that is an interesting one that deserves a little more looking in to.
It makes sense to have the include() method throw an IOException because it
could have come from the service() method of the servlet that was called by
the request dispatcher -- but the text of this description seems to be at odds
with the earlier statement that the included servlet, via the response object,
*does* have access to the calling servlet's ServletOutputStream or PrintWriter
object -- even though it cannot necessarily set response headers.

Craig McClanahan

>   Takayuki TACHIKAWA  /   NTT SOFT, Advanced Technology Development Dept.
> _____________________/    [EMAIL PROTECTED]
> phone/fax:045-212-8018/7948

___________________________________________________________________________
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff SERVLET-INTEREST".

Archives: http://archives.java.sun.com/archives/servlet-interest.html
Resources: http://java.sun.com/products/servlet/external-resources.html
LISTSERV Help: http://www.lsoft.com/manuals/user/user.html

Reply via email to