Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to acheive Send Redirect in Struts

 
Parameswaran Thangavel
Ranch Hand
Posts: 485
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi all
i need to redirect my application to completely different application.
So i tried with response.sendRedirect("URL"); which fails completely.


Since actionForward is expected by framework for every ActionServlet i donno what will be the flow of apllication?

Any idea how can we redirect to different site in struts.

And also what will happen to the Current session objects. will it get retained or get destroyed?
 
Merrill Higginson
Ranch Hand
Posts: 4864
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To forward to a different application, you need to:

1-Specify a forward. In the path, specify the full URL of the site including the "http://". Also specify redirect="true".

2-Simply find the forward using the ActionMapping object and return it from the execute() method of your Action class, just as you would with any other forward.

And also what will happen to the Current session objects. will it get retained or get destroyed?


Naturally, since you're moving to a completely different application, the new application knows nothing about your session or any other objects stored on your server. If after browsing the new application, the user navigates back to your site, the session and all other server-side objects will still be there.
 
Parameswaran Thangavel
Ranch Hand
Posts: 485
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
Is redirect can be acheived using only the action element instead of going to the execute method.

sth like, <action forward="" redirect="true"/>

instead of
<action >
<forward name="out" path="www.google.com" redirect="true" />
</action>

i donno whether it is legal attribte,

Since there is no logic or code done inside execute method other than setting the target say mapping.findforward("out").
 
Eugene Abarquez
Ranch Hand
Posts: 211
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I don't know if there is a policy against the revival of really old posts, but I have a follow-up question on this.

What if the URL is not predetermined and is generated on the fly? You definitely can't specify it in the path attribute of your struts-config.xml file. Is there a way to do this in Struts?

I hope somebody responds,

Eugene
 
Joe Ess
Bartender
Posts: 9279
10
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Eugene Abarquez:
Hello,
I don't know if there is a policy against the revival of really old posts, but I have a follow-up question on this.


Our policy is Dont Wake The Zombies
Actually, in your case it's fine since you are expanding on the original question. I'm not sure about Struts, but HttpServletResponse has a sendRedirect method.
 
Alaa Nassef
Ranch Hand
Posts: 471
Hibernate Mac OS X Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not sure about Struts, but HttpServletResponse has a sendRedirect method.


According to the first question in the post, the sendReirect fails completely. So, is there another option?
 
Eugene Abarquez
Ranch Hand
Posts: 211
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys, sorry if I wake some zombies.

I already found a turnaround for the problem.

First, I saved the "dynamic" url in the session for later use in the Action object. Then, I just did the usual forward to another page called "redirect.jsp" from the Action object, using the predefined path attribute in the struts-config.xml file of course.



Then in the redirect.jsp page, I placed a JSTL redirect tag and gave it the value of the "dynamic" url I saved from the session previously.

Pretty hacky, but that solved the problem. Hope this helps somebody out too.

Eugene
 
Alaa Nassef
Ranch Hand
Posts: 471
Hibernate Mac OS X Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Great solution. I would like to suggest to save the URL in the request rather than the session, so that i won't be there in the memory after the redirect.
 
sai kumar
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

We are using the action mapping in struts config file to do a redirect.

The code snippet is like:

<forward name="changeLanguage" redirect="true" path="/changeLanguage.do" />

The redirect works fine but it is a temporary redirect (302).

We would like to change this to a permanent redirect (301). Do we have an option to configure that in the struts-config.xml.

Your reply will be appreciated.

Thanks
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic