jQuery in Action, 2nd edition*
The moose likes Servlets and the fly likes Doubt in forwarding to Servlet/JSP in another Application 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 "Doubt in forwarding to Servlet/JSP in another Application " Watch "Doubt in forwarding to Servlet/JSP in another Application " New topic
Author

Doubt in forwarding to Servlet/JSP in another Application

Senthil Kumar
Ranch Hand

Joined: Oct 14, 2007
Posts: 33
Hi all,
Can anyone say how can we forward to a servlet/JSP available in another application(Context).

Thanks in advance.
Regards
Senthil
Tarun Yadav
Ranch Hand

Joined: Sep 20, 2007
Posts: 134
By default, you can't; it would be a security loophole.

But you can set it up depending on your server; for Tomcat, you need to first set the attribute 'crossContext' equal to true for the webapp's <Context> element.

You can then get the other context with ServletContext.getContext("/otherWebApp"). On this, you can then use the usual getRequestDispatcher() and RequestDispatcher.forward() methods.
Senthil Kumar
Ranch Hand

Joined: Oct 14, 2007
Posts: 33
Thanks a lot for your reply
Senthil Kumar
Ranch Hand

Joined: Oct 14, 2007
Posts: 33
But i have a question.
Sometimes we just forward to homepage of another site
example:
response.sendRedirect("www.xx.com");

In this case we are trying to access JSP/HTML of another application which is a resource of another application.
Is that true.In this case i have not made any settings as you said before.
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

With redirects, you're actually asking the browser to make a brand new request to the other URL. There is no server side interaction between the two applications.


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Tarun Yadav
Ranch Hand

Joined: Sep 20, 2007
Posts: 134
Also, I don't think this:



will work. You'll need to add the protocol information:



The first one will simply append "www.xx.com" to the current request path!
[ October 25, 2007: Message edited by: Tarun Yadav ]
Senthil Kumar
Ranch Hand

Joined: Oct 14, 2007
Posts: 33
Oh OK... i got it...
so it means that we can send a request to another resource available in another application only by 2 ways.
1.Context.getContext("/xx");
2.response.sendRedirect("http://www.xx.com");
Is that right?
Tarun Yadav
Ranch Hand

Joined: Sep 20, 2007
Posts: 134
That's correct but keep in mind that with an include(), you're sending the original request object (via server side call)but with a redirect you're sending a new request (by sending the location to the browser and asking it to ask for that resource).
[ October 26, 2007: Message edited by: Tarun Yadav ]
Senthil Kumar
Ranch Hand

Joined: Oct 14, 2007
Posts: 33
Thanks Tarun Thanks a lot for your reply.
am clear now.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Doubt in forwarding to Servlet/JSP in another Application
 
Similar Threads
Scope of implicit response object
calling a servlets
Basic Doubt
can one servlet extend another servlet?
Accessing servlet