• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Calling another web-app using RequestDispatcher

 
Giriraj Bhojak
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 197
Java Oracle Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
RequestDispatcher can forward request to the resources running within the webapplication.
It cannot help in forwarding request to another web application.

 
Harpreet Singh janda
Ranch Hand
Posts: 317
Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 197
Java Oracle Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Prabhakar Reddy Bokka
Ranch Hand
Posts: 197
Java Oracle Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How about showing what you tried.
 
Prabhakar Reddy Bokka
Ranch Hand
Posts: 197
Java Oracle Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 197
Java Oracle Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[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
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 197
Java Oracle Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic