how can i determine the current URL in a jsp page?

Discussions

Web tier: servlets, JSP, Web frameworks: how can i determine the current URL in a jsp page?

  1. i know in PHP, there's a static constant called PHP_SELF which stores the current URL. is there an equivalent of this for JSP?

    thank you
  2. Hi! Shawn:

    JSP on compilation generates a servlet thus the request, response objects are readily available.
    The following scriptlet (Java code) should give you what you need

    <%= request.getRequestURI() %>

    This will print the URI to the browser. If you want to store it in a variable use the following

    <% String myURI = request.getRequestURI(); %>

    If you need the value as a URL you will need to use the following

    <%@ page import="javax.servlet.http.HttpUtils.*" %>
    <%= javax.servlet.http.HttpUtils.getRequestURL(request) %>

    in the JSP.

    Pradeep.


  3. you can getRequestURI() and getRequestURL() functions. See Servlet request for more details about the request methods