How to get the url of the client

10,254

Solution 1

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.

Solution 2

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 .../>)

Share:
10,254
Cassie
Author by

Cassie

Updated on June 04, 2022

Comments

  • Cassie
    Cassie almost 2 years

    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?