You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Will Spies <sp...@towers.com> on 2002/09/12 16:49:18 UTC
Redirecting to a relative URL from root
I would like a forward to redirect off of the root context. Normally, if my
struts app is in a WAR , let's say MyStrutsApp this forward:
<forward name = "success" path = "/mydir/allben_home.jsp"
redirect = "true"/>
would result in a redirect to
http://myhost:myport/MyStrutsApp/mydir/allben_home.jsp
I would like to do a redirect to http://myhost:myport/mydir/allben_home.jsp
Looking at the processActionForward code below this would work if I removed
the '/' but by definition this doesn't work since redirecting to a relative
link without a slash in front of it will still result in MyStrutsApp
appearing in the new redirect URL.
How can I do this?
protected void processActionForward(ActionForward forward,
ActionMapping mapping,
ActionForm formInstance,
HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException {
if (forward != null) {
String path = forward.getPath();
if (forward.getRedirect()) {
if (path.startsWith("/"))
path = request.getContextPath() + path;
response.sendRedirect(response.encodeRedirectURL(path));
} else {
RequestDispatcher rd =
getServletContext().getRequestDispatcher(path);
if (rd == null) {
response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
internal.getMessage
("requestDispatcher",
path));
return;
}
rd.forward(request, response);
}
}
_______________________
Will Spies
Towers Perrin
Phone: (215)246-7145
e-mail: spiesw@towers.com
--
To unsubscribe, e-mail: <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>
Re: Redirecting to a relative URL from root
Posted by Donald Ball <db...@rhoworld.com>.
On 9/12/2002 at 12:08 PM Michael Lee wrote:
>Cool! Did not know that! I take it when you say that solution doesn't work
>you just mean the one scentence..
I meant the suggested solution, namely that you use absolute URLs in your
struts-config.xml file when redirecting a user to another webapp on the
same server as your webapp isn't a very good one because it forces you to
hard-code things into your struts-config file that you shouldn't have to.
>" If your going to another webapp just do an http://.... (Is this last
>sentence right?)"
>So you just use // and it get's you to domain root? Is that right?
>so if I had
>http://domain.com/webapp1/somepage.jsp
>http://domain.com/webapp2/somepage.jsp
>
>from somepage.jsp all urls from "/anotherpage.jsp" would be
>http://domain.com/webapp1/anotherpage.jsp
>
>and to go to webapp2/somepage.jsp the url would be
>"//webapp2/sompage.jsp" which equates to
>http://domain.com/webapp2/somepage.jsp
>
>Is this correct?
Unfortunately, no, it does _not_ work this way right now. I'm suggesting
that struts be modified so that this syntax will be supported.
(note if you like the proposal, it's only a few lines of code to modify
performActionForward to make it happen, you needn't wait for the struts
developers to approve it if you're comfortable extending struts on your
own)
- donald
--
To unsubscribe, e-mail: <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>
Re: Redirecting to a relative URL from root
Posted by Michael Lee <ml...@hotmail.com>.
Cool! Did not know that! I take it when you say that solution doesn't work
you just mean the one scentence..
" If your going to another webapp just do an http://.... (Is this last
sentence right?)"
So you just use // and it get's you to domain root? Is that right?
so if I had
http://domain.com/webapp1/somepage.jsp
http://domain.com/webapp2/somepage.jsp
from somepage.jsp all urls from "/anotherpage.jsp" would be
http://domain.com/webapp1/anotherpage.jsp
and to go to webapp2/somepage.jsp the url would be
"//webapp2/sompage.jsp" which equates to
http://domain.com/webapp2/somepage.jsp
Is this correct?
----- Original Message -----
From: "Donald Ball" <db...@rhoworld.com>
To: <st...@jakarta.apache.org>
Sent: Thursday, September 12, 2002 11:10 AM
Subject: Re: Redirecting to a relative URL from root
> On 9/12/2002 at 11:01 AM Michael Lee wrote:
>
> >by default the '/' root is from your webapp, as it should be. All URLs
> >should be from '/' that are going to the same webapp. If your going to
> >another webapp just do an http://.... (Is this last sentence right?)
> >Don't use ../.. whatever you do! (not easily mutable)
>
> That solution doesn't work in general. In his struts-config file, he would
> have to hard-code the following assumptions:
>
> * protocol (http v.s. https)
> * hostname
> * port number
>
> A better solution, I think, would be to augment processActionForward with
a
> new syntax for reaching the site root. How about //? E.g.
>
> //OtherWebApp/foo/bar
>
> - donald
>
>
> --
> To unsubscribe, e-mail:
<ma...@jakarta.apache.org>
> For additional commands, e-mail:
<ma...@jakarta.apache.org>
>
--
To unsubscribe, e-mail: <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>
Re: Redirecting to a relative URL from root
Posted by Donald Ball <db...@rhoworld.com>.
On 9/12/2002 at 11:01 AM Michael Lee wrote:
>by default the '/' root is from your webapp, as it should be. All URLs
>should be from '/' that are going to the same webapp. If your going to
>another webapp just do an http://.... (Is this last sentence right?)
>Don't use ../.. whatever you do! (not easily mutable)
That solution doesn't work in general. In his struts-config file, he would
have to hard-code the following assumptions:
* protocol (http v.s. https)
* hostname
* port number
A better solution, I think, would be to augment processActionForward with a
new syntax for reaching the site root. How about //? E.g.
//OtherWebApp/foo/bar
- donald
--
To unsubscribe, e-mail: <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>
Re: Redirecting to a relative URL from root
Posted by Michael Lee <ml...@hotmail.com>.
by default the '/' root is from your webapp, as it should be. All URLs
should be from '/' that are going to the same webapp. If your going to
another webapp just do an http://.... (Is this last sentence right?)
Don't use ../.. whatever you do! (not easily mutable)
Mike
----- Original Message -----
From: "Will Spies" <sp...@towers.com>
To: "Struts Users Mailing List" <st...@jakarta.apache.org>
Sent: Thursday, September 12, 2002 10:49 AM
Subject: Redirecting to a relative URL from root
>
>
> I would like a forward to redirect off of the root context. Normally, if
my
> struts app is in a WAR , let's say MyStrutsApp this forward:
>
> <forward name = "success" path = "/mydir/allben_home.jsp"
> redirect = "true"/>
>
> would result in a redirect to
> http://myhost:myport/MyStrutsApp/mydir/allben_home.jsp
>
> I would like to do a redirect to
http://myhost:myport/mydir/allben_home.jsp
>
> Looking at the processActionForward code below this would work if I
removed
> the '/' but by definition this doesn't work since redirecting to a
relative
> link without a slash in front of it will still result in MyStrutsApp
> appearing in the new redirect URL.
>
> How can I do this?
>
>
>
>
>
>
>
> protected void processActionForward(ActionForward forward,
> ActionMapping mapping,
> ActionForm formInstance,
> HttpServletRequest request,
> HttpServletResponse response)
> throws IOException, ServletException {
>
> if (forward != null) {
> String path = forward.getPath();
> if (forward.getRedirect()) {
> if (path.startsWith("/"))
> path = request.getContextPath() + path;
> response.sendRedirect(response.encodeRedirectURL(path));
> } else {
> RequestDispatcher rd =
> getServletContext().getRequestDispatcher(path);
> if (rd == null) {
>
> response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
> internal.getMessage
> ("requestDispatcher",
> path));
> return;
> }
> rd.forward(request, response);
> }
> }
>
>
> _______________________
> Will Spies
> Towers Perrin
> Phone: (215)246-7145
> e-mail: spiesw@towers.com
>
>
> --
> To unsubscribe, e-mail:
<ma...@jakarta.apache.org>
> For additional commands, e-mail:
<ma...@jakarta.apache.org>
>
--
To unsubscribe, e-mail: <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>