10

I would grab the URL of the current JSP web page with its settings example: index.jsp? param = 12

Have you any idea? Thank you

Anass
  • 6,132
  • 6
  • 27
  • 35

2 Answers2

24

You can get it from the HttpServletRequest object which is in EL available by ${pageContext.request}. The part before the ? is available by getRequestURL() method and the part after the ? is available by getQueryString() method. So, in a nutshell:

<p>Request URL: ${pageContext.request.requestURL}</p>
<p>Query string: ${pageContext.request.queryString}</p>
<p>Full URL: ${pageContext.request.requestURL}?${pageContext.request.queryString}</p>

If you want to do this using normal Java code, you'd better use a Servlet for this.

String requestURL = request.getRequestURL().toString();
String queryString = request.getQueryString();
if (queryString != null) requestURL += "?" + queryString;
// ...
Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • 6
    Warning: If you've forwarded to the jsp page using a RequestDispatcher, the abovementioned jsp code may not produce the URL you expected. It will produce the URL of the forwarded jsp page and not the URL that the user actually requested. I have [mentioned this to @BalusC before](http://stackoverflow.com/questions/5624911/include-jsp-file-with-java/5624928#comment-6409455) and I'm still waiting for the a satisfactory workaround. For now, I'm including jsps instead of forwarding. – Asaph May 23 '11 at 13:50
  • 1
    @Asaph: Wow, what an attitude. I must have overlooked this comment reply. It's set as a request attribute. For the name, check the value of constant `RequestDispatcher#FORWARD_REQUEST_URI`. In the future, just press `Ask Question` if you have a question which you're looking an answer for. – BalusC May 23 '11 at 13:59
  • 1
    Sorry. I didn't mean to have an attitude about it. No hard feelings? So do you want to update your answer to use the request attribute instead of `${pageContext.request.requestURL}`?. It seems to me that this would better. No? Also, what if the request was forwarded multiple times? Will the request attribute still contain the URL that the user requested? – Asaph May 23 '11 at 14:04
  • 1
    @Asaph: There is nothing which proves that the OP is doing a forward. If no forward is done, that attribute is not been set. I recall some related answers, you may find it useful: http://stackoverflow.com/questions/3663329/how-to-get-path-at-browser-in-jsp/3663408#3663408 and http://stackoverflow.com/questions/5622461/how-to-know-when-the-request-is-forwarded-in-a-requestwrapper-object/5623974#5623974 – BalusC May 23 '11 at 14:18
  • Thanks. Those links were helpful. Again, I didn't mean to come off rude in my initial comment. Sorry. – Asaph May 25 '11 at 04:38
1

Look at the HttpServletRequest Object, which you can access from your JSP in a scriplet (although that's not pretty). It has many methods for getting the URL of the page, including the parameters. Methods of interest will be:

 - getQueryString 
 - getRequestURI
 - getRequestURL

Have a play with them.

planetjones
  • 12,469
  • 5
  • 50
  • 51