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


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Forward Question" Watch "Forward Question" New topic
Author

Forward Question

Isaias C. Barroso
Ranch Hand

Joined: Jan 22, 2002
Posts: 165
Hi All,
How to forward a servlet on a different context?
Context - > test1 -> ServletTest1
Context - > test2 -> ServletTest2
On ServletTest1 i want forward a ServletTest2.
getServletContext().getRequestDispatcher("/test2/servlet/ServletTest2").forward(req, res);
Don't work.
Best Regards,


Isaias
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

Depending on how well it is implemented on the server you are using (it isn't always useful), this is possible but not a good idea.
The reason it's a bad idea is that it creates dependencies between your applications. A context should be a stand-alone application. If you need the servlet, it may be better to import it into the context and give it its own mapping.
That said, this may work:
getServletContext().getContext( "test2" ).getRequestDispatcher("/servlet/ServletTest2").forward(req, res);
The call to 'getRequetDispatcher' is always specific to a context, which is why your call didn't work. This reaches from one context to the next and makes method calls from that context.
Depending on the sever you are using, you might need "/test2" rather than "test2".
Dave
Isaias C. Barroso
Ranch Hand

Joined: Jan 22, 2002
Posts: 165
Hi David,
Thanks by response.
Best Regards.
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

Sent by chandm
I quoted your reply to a previous post and I wanted to ask you what you meant. I have been creating Web application which send control over to a servlet, the servlet does some work and uses RequestDispatcher.forward to send a JavaBean and the request to a JSP page. The thing is it fails a lot after using a stress tool to test it out. Is this attributed to what your stating below and the better way you offer is that using RequestDispatcher.include. Does that make it stay in the same context?
Thanks.

If you aren't explicitly calling a different context then you always remain in the same one. If you are crossing contexts then you are likely to have a range of problems depending on the server you are using.
One possible problem I forgot to mention is that of thread safety. When you cross between contexts, it may be that you get assigned a different thread in the other context. If this is the case then code may not execute in the order you expect, and Objects that you though were safe may be altered by different threads at the same time. This could certainly cause problems during load testing.
I can't really say more without specific information of whats going on.
Dave
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

Originally posted by Isaias C. Barroso:
Hi David,
Thanks by response.
Best Regards.

No problem, I hope it helped!
Dave
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Forward Question
 
Similar Threads
help moderator or site admin
Configure Tomcat 6 to serve applications in other than default directory
HFSJ Mock Exam question 45
a simple question
JSP Forward