aspose file tools*
The moose likes Servlets and the fly likes Serlet request Dispatcher Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Serlet request Dispatcher" Watch "Serlet request Dispatcher" New topic
Author

Serlet request Dispatcher

JeanLouis Marechaux
Ranch Hand

Joined: Nov 12, 2001
Posts: 906
Hi,
I use a request dispatcher to forward from a servlet to another.
In servlet1 I have :
getServletContext().getRequestDispatcher("/servlet2").forward(request, response);
It only works if I have an URL mapping (web.xml)
<servlet-mapping >
<servlet-ame>Servlet2</servlet-name>
<url-pattern>/servlet2</url-pattern>
</servlet-mapping>
I would like to avoid this servlet mapping.
How can I have my forward() working without the mapping ??


/ JeanLouis<br /><i>"software development has been, is, and will remain fundamentally hard" (Grady Booch)</i><br /> <br />Take a look at <a href="http://www.epfwiki.net/wikis/openup/" target="_blank" rel="nofollow">Agile OpenUP</a> in the Eclipse community
Matthew Phillips
Ranch Hand

Joined: Mar 09, 2001
Posts: 2676
Try this:
getServletContext().getRequestDispatcher("/servlet/<servlet2>").forward(request, response);
where <servlet2> is the name of your servlet.


Matthew Phillips
JeanLouis Marechaux
Ranch Hand

Joined: Nov 12, 2001
Posts: 906
Originally posted by Matthew Phillips:
Try this:
getServletContext().getRequestDispatcher("/servlet/<servlet2>").forward(request, response);
where <servlet2> is the name of your servlet.


Nope matthew
It raises a 404 error
Roy Ben Ami
Ranch Hand

Joined: Jan 13, 2002
Posts: 732
to use a relative path u need to call the requestdispatcher from the request and not the servercontect like this:
request.getrequestDispatcher() etc...
hope it works.
JeanLouis Marechaux
Ranch Hand

Joined: Nov 12, 2001
Posts: 906
Unfortunately, it does not help
Maybe I misexplained my problem :
From myServletA, I call :
getServletConfig().getServletContext().getRequestDispatcher("/myServletB").forward(request,response)
or
request.getRequestDispatcher("/myServletB").forward(request,response)
myServletA and myServletB are in com.myapp.action
I obtain a 404 error if I do not have a servlet mapping for myServletB in web.xml
<servlet>
<servlet-ame>myServletB</servlet-name>
<description>desc</description>
<servlet-class> com.myapp.action.myServletB</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>myServletB</servlet-name>
<url-pattern>/myServletB</url-pattern>
</servlet-mapping>
I would like to be able to perform a forward without this servlet-mapping.. Is it possible... How ?
[ January 22, 2002: Message edited by: Bill Bailey ]
Jeff Pierce
Greenhorn

Joined: Jan 04, 2002
Posts: 13
You may have to use the fully qualified class name in the forward(com.myapp.action.myServletB.
What are you seeing for the url in you servlet log?
JeanLouis Marechaux
Ranch Hand

Joined: Nov 12, 2001
Posts: 906
Originally posted by Jeff Pierce:
You may have to use the fully qualified class name in the forward(com.myapp.action.myServletB.
What are you seeing for the url in you servlet log?

I have already tried the fully qualified class and it does not help.
no erro in the logs (I use WebSphere).
But the HTTP server returns 404
JeanLouis Marechaux
Ranch Hand

Joined: Nov 12, 2001
Posts: 906
What I have tried so far :
request.getRequestDispatcher("/myServletB");
request.getRequestDispatcher("/myServletB");
getServletContext.getRequestDispatcher("/servlet/myServletB");
getServletContext.getRequestDispatcher("/myServletB");
getServletContext.getRequestDispatcher("myServletB");
getServletContext.getRequestDispatcher("/servlet/com/myapp/action/myServletB");
getServletContext.getRequestDispatcher("/com/myapp/action/myServletB");
AND
getServletContext.getRequestDispatcher("/servlet/com.myapp.action.myServletB");

But a forward to a jsp file works fine
getServletContext.getRequestDispatcher("/myrelativepath/myJSPPage.jsp");
Driving me crazy
What is the right URL to use ??
[ January 22, 2002: Message edited by: Bill Bailey ]
Matthew Phillips
Ranch Hand

Joined: Mar 09, 2001
Posts: 2676
Try this:
getServletContext.getRequestDispatcher("/servlet/com.myapp.action.myServletB");
The fully qualified name uses periods instead of slashes.
JeanLouis Marechaux
Ranch Hand

Joined: Nov 12, 2001
Posts: 906
Originally posted by Matthew Phillips:
Try this:
getServletContext.getRequestDispatcher("/servlet/com.myapp.action.myServletB");
The fully qualified name uses periods instead of slashes.

No.. Actually, the fully qualified I mentioned was a typo (copy/paste you know). I've modified my previous post accordingly
The one I used is the same as yoursgetServletContext.getRequestDispatcher("/servlet/com.myapp.action.myServletB");
But it does not work
[ January 22, 2002: Message edited by: Bill Bailey ]
Mike Curwen
Ranch Hand

Joined: Feb 20, 2001
Posts: 3695

Can I raise my hand and ask a late question?

(I'll ask anyways)

Is there a particular reason you want this to work without the mapping? (other than "I think it should.")
JeanLouis Marechaux
Ranch Hand

Joined: Nov 12, 2001
Posts: 906
Originally posted by Mike Curwen:
Can I raise my hand and ask a late question?

(I'll ask anyways)

Is there a particular reason you want this to work without the mapping? (other than "I think it should.")


Excellent question
Here is a short answer.
I don't want a myServletB to be accessible directly.
If there is a mapping for it, then a user can enter the name of the servlet in the URL.
Is there is not, each request has to be processed by myServletA first.
Does that make sense for you ?
mathew Jackson
Greenhorn

Joined: Jun 15, 2001
Posts: 12
OK, I understand why you are doing this the way you are. I too had a similar problem. The way I solved it was to leave the mapping in place but to set an attribute in the first servlet, then check for that attribute in the second. If the attribute wasn't set or was not what I set it to I returned a 403.
So in servlet a add:
req.setAttribute("MY_ATT", "SOME_VALUE");
and in servlet b:
if(req.getAttribute("MY_ATT") == null){
res.sendError(res.SC_FORBIDDEN);
}
Hope that helps some
mathew Jackson
Greenhorn

Joined: Jun 15, 2001
Posts: 12
Also, Filters in the new 2.3 spec may be of some use. I haven't yet worked with them but who knows.
JeanLouis Marechaux
Ranch Hand

Joined: Nov 12, 2001
Posts: 906
Thanks for this workaround Mathew.
I'll try it as soon as I get some "free time".
(using the attribute tip, not the filter 'cause my container is not 2.3 compliant)

So my understanding is that I can't forward to a servlet without having a servlet mapping ...
I'm really surprised
Moreover, I've never seen this mentioned in the Servlet's specifications... I can't believe it.
[ January 23, 2002: Message edited by: Bill Bailey ]
Robert Gagliardo
Greenhorn

Joined: Nov 15, 2001
Posts: 24
By spec you dont need servlet mapping for the forward, include to work.
the limitation you found is from your app server.
i know dispatch.forward|include works as intended with resin and servletexec.
JeanLouis Marechaux
Ranch Hand

Joined: Nov 12, 2001
Posts: 906
Originally posted by Robert Gagliardo:
By spec you dont need servlet mapping for the forward, include to work.
the limitation you found is from your app server.
i know dispatch.forward|include works as intended with resin and servletexec.

Fully agree with you.
And as my App Server is WebSphere, the servlet engine is TomCat.
And frankly, I don't think there is this kind of limitation with Tomcat
I guess there is something special in WebSphere configuration I've missed... but what...
JeanLouis Marechaux
Ranch Hand

Joined: Nov 12, 2001
Posts: 906
OK.
The solution is :
getServletContext.getRequestDispatcher("/servlet/com.myapp.action.myServletB");
BUT in WebSphere 4, you have to modify the file ibm-web-ext.xmi in order to have :
ServeServletsByClassnameEnabled = "true" (sounds like default is false)
This allows to call servlet without mapping

Many thanks to all of you who helped me
And plz forgive me, 'cause it was half a servlet problem, and half a Websphere one
[ January 23, 2002: Message edited by: Bill Bailey ]
Mike Curwen
Ranch Hand

Joined: Feb 20, 2001
Posts: 3695

What about using the security that's built in to the spec?

You can define a few URI patterns (one that matches your B servlet, for example) that prevents users from using them directly.
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16145
    
  21

Without formally checking the spec, I think that the implicit mapping of servlets was a convenience feature of early servers that's been (at least unoficially) deprecated, as it's a potential security hole. The idea being that the only classes that could be invoked as servlets were classes explicitly defined to BE servlets.
Question? Since the only way to run this secondary servlet is by direct invocation from the primary servlet, do you actually GAIN anything by doing a forward() request as opposed to simply calling it as a subroutine-type module?


Customer surveys are for companies who didn't pay proper attention to begin with.
JeanLouis Marechaux
Ranch Hand

Joined: Nov 12, 2001
Posts: 906
Mike, Tim,
Thanks a lot for your ideas.
To give a brief answer and explanation, I'm not responsible for the architecture of the application I have to fix...
According to me, myServletA should not be a servlet but a JavaClassA.
ServletB should be a frontController, using JavaClassA as a filter.
This would solve all my problems.
But it's an old (can a J2EE app really be old ) application with a lot of links to others.....
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Serlet request Dispatcher