tags:

views:

181

answers:

2

I'm sending the request from Display.jsp to TrialShow.jsp page, but whenever I call ${pageContext.request.requestURL} in TrialShow JSP page, I'm getting http://localhost:8081/newjsp1/TrialShow.jsp as an output. How can I display http://localhost:8081/newjsp1/Display.jsp in TrialShow JSP page?

+5  A: 

So you want the URL of the page which called the current page by a fullworthy HTTP request? I assume that there's no means of a forward, because you usually don't use JSP for this (as it would potentially produce IllegalStateException: Response already committed) and also, with a forward your requirement would have just worked the way you want.

The easiest way and your best bet is then getting the HTTP Referer header (yes, including the legendary typo). You can get it in EL as follows:

${header.referer}

I said "best bet", because clients are not required to fill the referrer header. Most browsers will send them along, but keep in mind that this value is fully controllable by the client, thus the client (or any of the client-installed software, with some specific Norton software as known example) can spoof or even fully remove the header value.

A bit more reliable way is to let the original page pass its URL as a (hidden) request parameter. E.g.

<input type="hidden" name="from" value="${pageContext.request.requestURI}">

This way it's accessible in next page by

${param.from}

I said, "bit", because the client can still change it, but now you're not dependent on the client specific environment anymore. You still need to keep in mind that you shouldn't use this value for business purposes. Use it at highest for statistics or debugging.

BalusC
Thank you Sir very much for your help.
Dusk
You're welcome.
BalusC
+3  A: 

Use ${requestScope['javax.servlet.forward.request_uri']}

EDIT: corrected syntax

EDIT: This works if you forwarded request to the second jsp (for example, using <jsp:forward .../>)

axtavt
I don't think there's any means of a forward here. It's JSP --> JSP, not JSP --> Servlet --> JSP. If there was actually means of a forward, you would have seen the URL of the `Display.jsp` instead in the request URL!
BalusC