HttpServletRequest完成URL

我有一个HttpServletRequest对象。

如何获得导致此调用到达我的servlet的完整且准确的URL?

或者至less尽可能准确,因为有可能是可以重新生成的东西(可能是参数的顺序)。

HttpServletRequest有以下方法:

  • getRequestURL() – 返回查询string分隔符之前的完整URL的部分?
  • getQueryString() – 返回查询string分隔符后的完整URL的部分?

所以,要获得完整的url,只需:

 public static String getFullURL(HttpServletRequest request) { StringBuffer requestURL = request.getRequestURL(); String queryString = request.getQueryString(); if (queryString == null) { return requestURL.toString(); } else { return requestURL.append('?').append(queryString).toString(); } } 

我用这个方法:

 public static String getURL(HttpServletRequest req) { String scheme = req.getScheme(); // http String serverName = req.getServerName(); // hostname.com int serverPort = req.getServerPort(); // 80 String contextPath = req.getContextPath(); // /mywebapp String servletPath = req.getServletPath(); // /servlet/MyServlet String pathInfo = req.getPathInfo(); // /a/b;c=123 String queryString = req.getQueryString(); // d=789 // Reconstruct original requesting URL StringBuilder url = new StringBuilder(); url.append(scheme).append("://").append(serverName); if (serverPort != 80 && serverPort != 443) { url.append(":").append(serverPort); } url.append(contextPath).append(servletPath); if (pathInfo != null) { url.append(pathInfo); } if (queryString != null) { url.append("?").append(queryString); } return url.toString(); } 
 // http://hostname.com/mywebapp/servlet/MyServlet/a/b;c=123?d=789 public static String getUrl(HttpServletRequest req) { String reqUrl = req.getRequestURL().toString(); String queryString = req.getQueryString(); // d=789 if (queryString != null) { reqUrl += "?"+queryString; } return reqUrl; } 

HttpUtil被弃用,这是正确的方法

 StringBuffer url = req.getRequestURL(); String queryString = req.getQueryString(); if (queryString != null) { url.append('?'); url.append(queryString); } String requestURL = url.toString(); 

结合getRequestURL()getQueryString()的结果应该可以得到你想要的结果。

在一个Spring项目中,你可以使用

 UriComponentsBuilder.fromHttpRequest(new ServletServerHttpRequest(request)).build().toUriString() 

你可以使用filter。

 @Override public void doFilter(ServletRequest arg0, ServletResponse arg1, FilterChain arg2) throws IOException, ServletException { HttpServletRequest test1= (HttpServletRequest) arg0; test1.getRequestURL()); it gives http://localhost:8081/applicationName/menu/index.action test1.getRequestURI()); it gives applicationName/menu/index.action String pathname = test1.getServletPath()); it gives //menu/index.action if(pathname.equals("//menu/index.action")){ arg2.doFilter(arg0, arg1); // call to urs servlet or frameowrk managed controller method // in resposne HttpServletResponse httpResp = (HttpServletResponse) arg1; RequestDispatcher rd = arg0.getRequestDispatcher("another.jsp"); rd.forward(arg0, arg1); } 

不要忘记在web.xml中的filter映射中放置<dispatcher>FORWARD</dispatcher>

在HttpServletRequest对象上使用以下方法

java.lang.String getRequestURI()从HTTP请求的第一行中的协议名称到查询string,将此请求URL的一部分返回。

java.lang.StringBuffer getRequestURL() -重build客户端用于发出请求的URL。

java.lang.String getQueryString()返回path后请求URL中包含的查询string。

晚会稍晚,但是我在WebUtils的MarkUtils-Web库中包含了这个 – Checkstyle批准和JUnittesting:

 import javax.servlet.http.HttpServletRequest; public class GetRequestUrl{ /** * <p>A faster replacement for {@link HttpServletRequest#getRequestURL()} * (returns a {@link String} instead of a {@link StringBuffer} - and internally uses a {@link StringBuilder}) * that also includes the {@linkplain HttpServletRequest#getQueryString() query string}.</p> * <p><a href="https://gist.github.com/ziesemer/700376d8da8c60585438" * >https://gist.github.com/ziesemer/700376d8da8c60585438</a></p> * @author Mark A. Ziesemer * <a href="http://www.ziesemer.com.">&lt;www.ziesemer.com&gt;</a> */ public String getRequestUrl(final HttpServletRequest req){ final String scheme = req.getScheme(); final int port = req.getServerPort(); final StringBuilder url = new StringBuilder(256); url.append(scheme); url.append("://"); url.append(req.getServerName()); if(!(("http".equals(scheme) && (port == 0 || port == 80)) || ("https".equals(scheme) && port == 443))){ url.append(':'); url.append(port); } url.append(req.getRequestURI()); final String qs = req.getQueryString(); if(qs != null){ url.append('?'); url.append(qs); } final String result = url.toString(); return result; } } 

可能是Mat Banik的最快速和最稳健的答案 – 但即使是他也没有考虑到使用HTTP / HTTPS的潜在的非标准端口configuration。

也可以看看: