File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes JSP and the fly likes Changing the URL when using forward or redirect... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "Changing the URL when using forward or redirect..." Watch "Changing the URL when using forward or redirect..." New topic

Changing the URL when using forward or redirect...

George Larry
Ranch Hand

Joined: Nov 07, 2001
Posts: 52
I just wrote a long explanation of my problem, but I had placed a <jsp:forward in my subject line, and was told that <'s aren't allowed... so I backed up and everything was lost... I think someone should fix that.
Anyway, Here's my problem in a nutshell. I have a login.jsp and menu.jsp in my home folder, and everything else in a subdirectory. If a user tries to bypass the login screen by typing in a URL beyond home (home/reports/blah.jsp) I redirect them to the login page. The problem is that the URL the browser uses doesn't change... The login.jsp page will be displayed but the browser still things they're at 'home/reports/blah.jsp'... This messes up all my relative URL's.
After they login I try to send them to menu.jsp, but the browser tries to send them to home/reports/menu.jsp.
Is there a way to change the URL when forwarding or redirecting?
Jason Brawner
Ranch Hand

Joined: Dec 19, 2001
Posts: 66
I don't think there is an easy way to change what appears in the browsers address bar. You'd have to do something dumb like calling a blank html page that redirects to your login page...messy.
To solve the relative page problem, I use all relative paths from the current context base directory. This way it should always work, regardless of whats in the broswers address bar (I could be wrong).
Hope this helps,

Jason E. Brawner<br />Sun Certified Java Programmer<br />Sun Certified Web Component Developer
David O'Meara

Joined: Mar 06, 2001
Posts: 13459

Use response.sendRedirect("/login.jsp") instead. Note that the bahaviour of this is fundamentally different to forward and include.
Forward and include work completelyy on the server side. The client asks for a page, you decide they should get another page and send them that instead. As far as the client knows, they got the page they asked for and hence the URL is unchanged.
sendRedirect is similar to the 'Jedi mind-trick'. The client asks for page A, you send a message telling them they want page B (this isn't the page you are looking for) and the client changes the URL then re-sends a request for the new page.
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 17417

I think that use of the [ CODE ] tags will protect any '<' - type stuff (such as sample HTML/XML). For quickie inline stuff, people often just put in a gratuitous space like so: < tag >. Notice I did that on "CODE" up above.
The reason for this is that otherwise the ubb sofware is likely to present your stuff as though it were html rather than passing it on unchanged.
You can also use the html/xml escapes: &lt; and so forth.

An IDE is no substitute for an Intelligent Developer.
Sunil K Bansal
Ranch Hand

Joined: Jan 04, 2001
Posts: 62
by using
parameters are not getting passed from abc.jsp to xyz.jsp
i need parameters from abc.jsp in xyz.jsp
Joe Gilvary
Ranch Hand

Joined: May 11, 2001
Posts: 152
Could you put the parameters in the session
before you redirect? The page that accepts the
redirect could use session attributes if the
request parameters of interest were null.
Sunil K Bansal
Ranch Hand

Joined: Jan 04, 2001
Posts: 62
I hv three JSP pages
a.jsp, b.jsp, c.jsp
first a.jsp is loaded,
from a.jsp i move to b.jsp,
from b.jsp to c.jsp
and then c.jsp to a.jsp like this at any point user can come to main menu
when a.jsp is loaded and submitted I have to do some processing(data saving) and then forward to b.jsp
same on b.jsp first data saving and then move to c.jsp
same for c.jsp data saving and then move to a.jsp
when i move from a.jsp to b.jsp i need some parameters to be passed from a.jsp to b.jsp
and like wise from b.jsp to c.jsp
i dont need parameters from c.jsp to a.jsp
(but right now these r getting passed from c.jsp to a.jsp)

for this I m using forward action of JSP
<jsp:forward page="b.jsp" />
everything(data saving) is working good
but URL shown in address bar is not right
after first step it shows URL of 1 page past.
In action attribute of form I hv given the same JSP name
e.g. on a.jsp form action=a.jsp
after saving data I make a variable true
if variable is true(that means data is saved) I forward to next JSP
if data is not saved due to some error in isert statement it remains on the same JSP(and now URL changes to same page, means correct JSP page, as I m giving same JSP page in action attribute of form)
as for as functionality is concerned I think there is no problem I m getting what I want.
I have not fully tested.
i just tested on my standalone m/c.
i think functionality wise i shud not get any problem
but if URL shown is proper of the page which is displayed it will be more nice.
is there any other way around to get the same functionality and showing same URL.
I hope u got what I want to say......
hope for a better solution
Sunil K Bansal
Ranch Hand

Joined: Jan 04, 2001
Posts: 62
I hv not received any reply to my previous explanation,
that means there is no better solution.
or people just dont want to waste time on thinking, if above is funtioning without any problem.
I agree. Here's the link:
subject: Changing the URL when using forward or redirect...
It's not a secret anymore!