This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Java Interview Guide and have Anthony DePalma on-line!
See this thread for details.
The moose likes JSP and the fly likes Finding current URI? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "Finding current URI?" Watch "Finding current URI?" New topic

Finding current URI?

Rick Beaver
Ranch Hand

Joined: Dec 14, 2004
Posts: 464

I am trying to find out the current URI loaded from a header that is included in all JSP files.

using request.getRequestURI() I can get the URI fine, until I do a forward / redirect.

The basis of what I am trying to do is to test if the current page is a.jsp and if so, dont redirect to a.jsp, if it is anything else, redirect to a.jsp.

The problem of course is that as the request.getRequestURI() does not change when you use <jsp:forward> or response.sendRedirect() I end up in an infinite loop of forwarding to a.jsp.

Has anyone an idea how to find the fact that I am at a.jsp without passing any parameters in the request?


If I have this in b.jsp:

The getRequestURI() is always b.jsp in this test so it keeps redirecting to a.jsp, which breaks the app.

ph34r my 133t j4v4 h4><0r1ng sk177z
Michal Bienek

Joined: Jun 17, 2002
Posts: 29
By having this code in b.jsp, you will never have a requestURI of a.jsp.
By doing a jsp:forward, you are internally changing the location, but the request URI will not change. The browser requested b.jsp and nothing else. Therefore, b.jsp will never be referenced by a requestURI of a.jsp.
I must be missing something in your problem statement.

here's the flow:
browser (request b.jsp) -> webserver -> b.jsp (forwards to) -> a.jsp

say b.jsp told the browser to redirect to a.jsp, then the flow would be:
browser (request b.jsp) -> webserver -> b.jsp (redirect to a.jsp) -> browser (now request a.jsp) -> webserver -> a.jsp

In no way can a webserver, when getting a request for a.jsp, would respond to the request with b.jsp (unless you are aliasing a.jsp to point to b.jsp, but based on your problem statement, that doesn't seem to be the case)
I agree. Here's the link:
subject: Finding current URI?
It's not a secret anymore!