wood burning stoves 2.0*
The moose likes Struts and the fly likes Redirect to a Different context Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Redirect to a Different context" Watch "Redirect to a Different context" New topic
Author

Redirect to a Different context

Parameswaran Thangavel
Ranch Hand

Joined: Mar 01, 2005
Posts: 485
hi all
i need to refer to some external application from my struts application.

Though i am able to acheive it by configuring the below lines in struts-config.xml ,

<action
path="/par"
type="com.FinServ"
parameter="method"
>
<forward name="showFinPage" path="http://10.22.234.45:4531/FinanceServlet" redirect="true" />
</action>

i dont want to hardcode the path http://10.22.234.45:4531/FinanceServlet, which will change in different environment say Int, Producton, Testing....

So i need to redirect to a external site that too i need it to configure the path dynamically.

After surfed the net i tried the below snippet inside my dispatch action method but not able to redirect it. www.google.com is just get appended with the the URL in IE address bar.

ActionForward fwd = new ActionForward("www.google.com",true);
return fwd;

Any help wil be realy appreciated.....
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
It is a hard and fast rule that whenever you redirect to a page outside the current web application, you must provide a full URL, including the "http://". There is no way around it.

That's the bad news. The good news is that you can look at the URL that was sent to the server to get to the page or Action that you're currently in by using the HttpServletRequest object's getRequestURL() method. This can save you from having to hard-code the server name or IP address.

Here's the process I'd recommend:

1-Create a forward with only "/financeServlet" as the path.

2-Create a utility method that will extract only the server portion of a URL

3- In any action class that uses this forward, get the server portion of the current URL using your method.

4- Create a new ActionForward by appending the server value with the URI provided by the forward you defined in step 1. Then return this ActionForward from your execute method.

If you really want to get fancy, you could extend the ActionForward object to indicate when a forward is to go to another application on the same server, and then take the appropriate action.


Merrill
Consultant, Sima Solutions
Parameswaran Thangavel
Ranch Hand

Joined: Mar 01, 2005
Posts: 485
Hi Merill,

i am really sorry to say that i confused a bit.
Let see my understanding is correct or not.

It is a hard and fast rule that whenever you redirect to a page outside the current web application, you must provide a full URL, including the "http://". There is no way around it.

so in my code if instead of www.google.com, if i gave http://www.google.com will it work.



That's the bad news. The good news is that you can look at the URL that was sent to the server to get to the page or Action that you're currently in by using the HttpServletRequest object's getRequestURL() method. This can save you from having to hard-code the server name or IP address.

In this case we are redirecting to the resource resides in the same server.
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
so in my code if instead of www.google.com, if i gave http://www.google.com will it work.


Yes, that's right. Try it out.

Let me see if I can explain it a little better. If the user of your application typed "http://10.22.234.45:4531/MyApp/MyAction.do" to get to your action, within the execute method of MyAction you can do this:

String url = request.getRequestURL();

The value of url will be "http://10.22.234.45:4531/MyApp/MyAction.do". That means that you always have a way to tell which server you're currently on (development, acceptance, production) by looking at the first part of the String returned by this method. By doing a little String manipulation, you could extract "http://10.22.234.45:4531" as the URL of the current server. Once you have this, you could append this to whatever URI you want. This way you can forward to another application on the same server without having to hard-code the server name.

Here's an example:

Parameswaran Thangavel
Ranch Hand

Joined: Mar 01, 2005
Posts: 485
Hi merill
Thanks for your work.
Since the application i am referring to resides in another server, i went with the first option and it works fine.
Ravi Prakash Shahi
Greenhorn

Joined: Mar 24, 2013
Posts: 1
Hi,

I have two web application A and B running on the same Tomcat Server.
Web Application A is a Struts2 based Application,

I want to Embed Application B in Application A,

Redirection is not an Option because i have to hide the URLs of Application B.

When we navigate the URLs of the Application B, the URL in the address bar should be displayed in context to Application A only.

So i'l have to forward.

Is it Possible to do so.

If Yes where should i initiate, as i have tried URL Connection to read the page but the CSS and JS of the Application B didn't work,

forward in the struts.xml didn't work 404-error

Please help
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Redirect to a Different context