aspose file tools*
The moose likes Servlets and the fly likes Calling another web-app using RequestDispatcher Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Calling another web-app using RequestDispatcher" Watch "Calling another web-app using RequestDispatcher" New topic
Author

Calling another web-app using RequestDispatcher

Giriraj Bhojak
Ranch Hand

Joined: Apr 03, 2008
Posts: 71
My web application calls response.sendRedirect("/newapp/home.jsp").
It works fine.
But i am also passing some request parameters which are exposed through request-header.

Is there a way i can use RequestDispatcher to invoke a jsp in another web-app.
I tried::
getServletContext().getRequestDispatcher(newURL).forward(request, response)
But it didn't work.
I am doing this in a Struts action class.
Any ideas?

Regards,
Giriraj.
Prabhakar Reddy Bokka
Ranch Hand

Joined: Jul 26, 2005
Posts: 189

RequestDispatcher can forward request to the resources running within the webapplication.
It cannot help in forwarding request to another web application.


SCJP 5, SCWCD 5
Harpreet Singh janda
Ranch Hand

Joined: Jan 14, 2010
Posts: 317

If both the web applications are running on the same server than you can user the forward method to forward the request to other web app.
Prabhakar Reddy Bokka
Ranch Hand

Joined: Jul 26, 2005
Posts: 189

Harpreet Singh janda wrote:If both the web applications are running on the same server than you can user the forward method to forward the request to other web app.


Can you post the code to get request dispatcher to forward the call to another application in the same server???
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

Have a look at the getRequestDispatcher method, it comes in two forms. One allows you to access resources from another context in the same web container.
Rajkishore Pujari
Ranch Hand

Joined: Sep 03, 2005
Posts: 46
http://download-llnw.oracle.com/javaee/5/api/javax/servlet/ServletContext.html#getContext%28java.lang.String%29
Prabhakar Reddy Bokka
Ranch Hand

Joined: Jul 26, 2005
Posts: 189

David O'Meara wrote:Have a look at the getRequestDispatcher method, it comes in two forms. One allows you to access resources from another context in the same web container.


Did you guys tried to access??

I am not able to access. Please provide the code if you are able to access.
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

How about showing what you tried.
Prabhakar Reddy Bokka
Ranch Hand

Joined: Jul 26, 2005
Posts: 189

Rajkishore Pujari wrote:http://download-llnw.oracle.com/javaee/5/api/javax/servlet/ServletContext.html#getContext%28java.lang.String%29


This never works for accessing resources of other web application.
ServletContext.getRequestDispatcher(java.lang.String) the path must start with '/' (no scope of keeping a URL). So, the resource should always be local to that context root. No options to access different context even if other web application is deployed in same server/ web container.

ServletRequest.getRequestDispatcher(java.lang.String)
The pathname specified may be relative, although it cannot extend outside the current servlet context.
Again the resource should be within the context.

David O'Meara wrote:How about showing what you tried.


Hope that makes sence.
Amarnath Ramasamy
Greenhorn

Joined: Feb 05, 2010
Posts: 7
If you are using weblogic, you can call another web app using weblogic.servlet.proxy.HttpProxyServlet class., or even you can use HttpClient. This is another way where in which you can avoid redirect.
Prabhakar Reddy Bokka
Ranch Hand

Joined: Jul 26, 2005
Posts: 189

[quote=Amarnath Ramasamy]If you are using weblogic, you can call another web app using weblogic.servlet.proxy.HttpProxyServlet class., or even you can use HttpClient. This is another way where in which you can avoid redirect.[/quote]

Can you post the code(If you are sure!..?)
Amarnath Ramasamy
Greenhorn

Joined: Feb 05, 2010
Posts: 7
Try this..you can also find several examples on web for httpclient / httpproxy

web.xml
======
<servlet>
<servlet-name>HttpProxy</servlet-name>
<servlet-class>weblogic.servlet.proxy.HttpProxyServlet</servlet-class>
<init-param>
<param-name>redirectURL</param-name>
<param-value>http://AnotherAppHostName:yourPort</param-value>
</init-param>
<init-param>
<param-name>WebLogicHost</param-name>
<param-value>AnotherAppHostName</param-value>
</init-param>

<init-param>
<param-name>WebLogicPort</param-name>
<param-value>yourPort</param-value>
</init-param>

</servlet>

<servlet-mapping>
<servlet-name>HttpProxy</servlet-name>
<url-pattern>/anotherapp/*</url-pattern>
</servlet-mapping>


In your action or servlet
=================
request.getRequestDispatcher("/anotherapp/yoursevletOrJsp").include(request,response);
Rajkishore Pujari
Ranch Hand

Joined: Sep 03, 2005
Posts: 46
Prabhakar Reddy Bokka wrote:
Rajkishore Pujari wrote:http://download-llnw.oracle.com/javaee/5/api/javax/servlet/ServletContext.html#getContext%28java.lang.String%29


This never works for accessing resources of other web application.
ServletContext.getRequestDispatcher(java.lang.String) the path must start with '/' (no scope of keeping a URL). So, the resource should always be local to that context root. No options to access different context even if other web application is deployed in same server/ web container.

ServletRequest.getRequestDispatcher(java.lang.String)
The pathname specified may be relative, although it cannot extend outside the current servlet context.
Again the resource should be within the context.

David O'Meara wrote:How about showing what you tried.


Hope that makes sence.


Please read this statement under link I have given before. Have you tried doing this.

"This method allows servlets to gain access to the context for various parts of the server, and as needed obtain RequestDispatcher objects from the context. The given path must be begin with "/", is interpreted relative to the server's document root and is matched against the context roots of other web applications hosted on this container".

servletContext.getContext("/someOtherWebappContext").getRequestDispatcher("/resourceInOtherWebAppContext");

Prabhakar Reddy Bokka
Ranch Hand

Joined: Jul 26, 2005
Posts: 189

Yeah.. I got it.

I was trying to configure context root in Tomcat 6. But not able to understand how to do it.
Please can you help me to configure.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Calling another web-app using RequestDispatcher