wood burning stoves 2.0
The moose likes Servlets and the fly likes Problems with RequestDispatcher Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of OCA Java SE 8 Programmer I Study Guide 1Z0-808 this week in the OCAJP forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Problems with RequestDispatcher " Watch "Problems with RequestDispatcher " New topic

Problems with RequestDispatcher

Andres Gonzalez
Ranch Hand

Joined: Nov 27, 2001
Posts: 1561
Hi again. My directory structure looks like this:
If DBServlet.java is running, how do I use the RequestDispatcher to send the request to CreateConsumer.java??
is it
RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(../../../..???);

I'm not going to be a Rock Star. I'm going to be a LEGEND! --Freddie Mercury
David O'Meara

Joined: Mar 06, 2001
Posts: 13459

The short answer is that no, you shouldn't. Each of your servlets exist in separate application contexts, meaning that they pretend to be completely different web applications.
The advantage that you have is that YOU KNOW that the two applications are running side by side, and there is a way to cros over between them.
If you get the current servlet context (via getServletContext() ), which represents the container for the current servlet, it can get a reference to another (named) context. Then you can use this context to dispatch to an external resource.
To recap:
get current context
get another named context (usually as "\openjms", but it may be "openjms")
then get the dispatcher to the servlet name (ie CreateConsumer, if that is what you have mapped it as)
then include or forward to it.
(please note that the support for this sort of functionality is vendor specific and you may find it doesn't work on you server...)
Mike Curwen
Ranch Hand

Joined: Feb 20, 2001
Posts: 3695

You are going cross-context, so you'll have to investigate how to enable this in your servlet container (because for the most part, this feature is disabled by default, for security reasons).

Also, check out the API on getContext()
Andres Gonzalez
Ranch Hand

Joined: Nov 27, 2001
Posts: 1561
Yes. The applications are running side by side.
Thanks, I got it working in this way.

there was another thing after typing this code. I was getting an error 405, saying that the POST method was not implemented, or something like that. So in my CreateConsumer.java I added:

and it worked. Why did I have to do that? I had only implemented the doGet method. (note that DBServlet.java inserts information to a database. The info comes from a form.)
Now, this question:
- Should you avoid a situation like this when you communicate with different webapps? I think is a bit cumbersome....
Andres Gonzalez
Ranch Hand

Joined: Nov 27, 2001
Posts: 1561
another quick question:
if DBServlet.java uses a bean (which is stored in the same place as DBServlet), How do I access the properties of this bean when CreateConsumer.java is running?
Do I have to import the bean? How would the import statement be like?
import project.FormBean???
Meng Tan
Ranch Hand

Joined: Jan 20, 2001
Posts: 115
From my understanding,
If the request object that is passed into the forward or include method of the RequestDispatcher is a POST request then the doPost() method of the Servlet implementing the RequestDispatcher will be called. The same is also true for GET request.
I agree. Here's the link: http://aspose.com/file-tools
subject: Problems with RequestDispatcher