• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Serlet request Dispatcher

 
JeanLouis Marechaux
Ranch Hand
Posts: 906
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ??
 
Matthew Phillips
Ranch Hand
Posts: 2676
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try this:
getServletContext().getRequestDispatcher("/servlet/<servlet2>").forward(request, response);
where <servlet2> is the name of your servlet.
 
JeanLouis Marechaux
Ranch Hand
Posts: 906
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 732
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 906
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 906
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 906
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2676
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try this:
getServletContext.getRequestDispatcher("/servlet/com.myapp.action.myServletB");
The fully qualified name uses periods instead of slashes.
 
JeanLouis Marechaux
Ranch Hand
Posts: 906
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3695
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 906
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 906
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 906
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 906
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3695
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18302
56
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
JeanLouis Marechaux
Ranch Hand
Posts: 906
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.....
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic