Get request URL in JSP which is forwarded by Servlet

后端 未结 8 1300
忘掉有多难
忘掉有多难 2020-11-29 22:27

How can I get request URL in JSP which is forwarded by Servlet?

If I run following code in JSP,

System.out.println(\"servlet path= \" + request.getSe         


        
相关标签:
8条回答
  • 2020-11-29 22:45

    Also you could use

    ${pageContext.request.requestURI}
    
    0 讨论(0)
  • 2020-11-29 22:46

    To avoid using scriplets in the jsp, follow the advice of "divideByZero", and use ${pageContext.request.requestURI} This is a better way to go.

    0 讨论(0)
  • 2020-11-29 22:55

    Try this,

    <c:set var="pageUrl" scope="request">
        <c:out value="${pageContext.request.scheme}://${pageContext.request.serverName}"/>
        <c:if test="${pageContext.request.serverPort != '80'}">
            <c:out value=":${pageContext.request.serverPort}"/>
        </c:if>
        <c:out value="${requestScope['javax.servlet.forward.request_uri']}"/>
    </c:set>
    

    I would like to put it in my base template and use in whole app whenever i need to.

    0 讨论(0)
  • 2020-11-29 22:56

    Try this instead:

    String scheme = req.getScheme();             
    String serverName = req.getServerName(); 
    int serverPort = req.getServerPort();    
    String uri = (String) req.getAttribute("javax.servlet.forward.request_uri");
    String prmstr = (String) req.getAttribute("javax.servlet.forward.query_string");
    String url = scheme + "://" +serverName + ":" + serverPort + uri + "?" + prmstr;
    

    Note: You can't get HREF anchor from your url. Example, if you have url "toc.html#top" then you can get only "toc.html"

    Note: req.getAttribute("javax.servlet.forward.request_uri") work only in JSP. if you run this in controller before JSP then result is null

    You can use code for both variant:

    public static String getCurrentUrl(HttpServletRequest req) {
        String url = getCurrentUrlWithoutParams(req);
        String prmstr = getCurrentUrlParams(req);
        url += "?" + prmstr;
        return url;
    }
    
    public static String getCurrentUrlParams(HttpServletRequest request) {
        return StringUtil.safeString(request.getQueryString());
    }
    
    public static String getCurrentUrlWithoutParams(HttpServletRequest request) {
        String uri = (String) request.getAttribute("javax.servlet.forward.request_uri");
        if (uri == null) {
            return request.getRequestURL().toString();
        }
        String scheme = request.getScheme();
        String serverName = request.getServerName();
        int serverPort = request.getServerPort();
        String url = scheme + "://" + serverName + ":" + serverPort + uri;
        return url;
    }
    
    0 讨论(0)
  • 2020-11-29 22:59

    None of these attributes are reliable because per the servlet spec (2.4, 2.5 and 3.0), these attributes are overridden if you include/forward a second time (or if someone calls getNamedDispatcher). I think the only reliable way to get the original request URI/query string is to stick a filter at the beginning of your filter chain in web.xml that sets your own custom request attributes based on request.getRequestURI()/getQueryString() before any forwards/includes take place.

    http://www.caucho.com/resin-3.0/webapp/faq.xtp contains an excellent summary of how this works (minus the technical note that a second forward/include messes up your ability to use these attributes).

    0 讨论(0)
  • 2020-11-29 23:06

    If you use RequestDispatcher.forward() to route the request from controller to the view, then request URI is exposed as a request attribute named javax.servlet.forward.request_uri. So, you can use

    request.getAttribute("javax.servlet.forward.request_uri")
    

    or

    ${requestScope['javax.servlet.forward.request_uri']}
    
    0 讨论(0)
提交回复
热议问题