Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
396 views
in Technique[技术] by (71.8m points)

java - How to get the url of the client

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...