• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Redirect to a Different context

 
Ranch Hand
Posts: 485
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.....
 
Ranch Hand
Posts: 4864
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Parameswaran Thangavel
Ranch Hand
Posts: 485
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 4864
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 485
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
If somebody says you look familiar, tell them you are in porn. Or in these tiny ads:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic