如何从HttpServletRequest中获取URL的部分内容?

25
从以下URL中,我需要单独获取(http://localhost:9090/dts)
也就是说我需要删除(documents/savedoc),或者只需要获取- (http://localhost:9090/dts)
http://localhost:9090/dts/documents/savedoc  
有没有办法在请求中获得上述内容?我尝试过以下方法并得到了结果,但仍在继续尝试。
System.out.println("URL****************"+request.getRequestURL().toString());  
System.out.println("URI****************"+request.getRequestURI().toString());
System.out.println("ContextPath****************"+request.getContextPath().toString());

URL****************http://localhost:9090/dts/documents/savedoc  
URI****************/dts/documents/savedoc  
ContextPath****************/dts

有人能帮我修复这个问题吗?

6个回答

40
你说你想要得到的是:
http://localhost:9090/dts

在您的情况下,上述字符串包含以下内容:
  1. scheme: http
  2. 服务器主机名: localhost
  3. 服务器端口: 9090
  4. 上下文路径: dts

有关请求路径元素的更多信息,请参阅官方Oracle Java EE教程从请求中获取信息

第一个变体:

String scheme = request.getScheme();
String serverName = request.getServerName();
int serverPort = request.getServerPort();
String contextPath = request.getContextPath();  // includes leading forward slash

String resultPath = scheme + "://" + serverName + ":" + serverPort + contextPath;
System.out.println("Result path: " + resultPath);

第二个变体:
String scheme = request.getScheme();
String host = request.getHeader("Host");        // includes server name and server port
String contextPath = request.getContextPath();  // includes leading forward slash

String resultPath = scheme + "://" + host + contextPath;
System.out.println("Result path: " + resultPath);

两种变体都会给你想要的结果:http://localhost:9090/dts 当然,还有其他的变体,就像其他人已经写过的一样。只是在你的原始问题中,你问的是如何获得http://localhost:9090/dts,也就是说你希望你的路径包括方案
但是如果你不需要方案,快速的方法是:
String resultPath = request.getHeader("Host") + request.getContextPath();

你将得到(在你的情况下):localhost:9090/dts

18

据我所知,目前没有提供相应的API方法,需要进行定制。

String serverName = request.getServerName();
int portNumber = request.getServerPort();
String contextPath = request.getContextPath();

// 试试这个

System.out.println(serverName + ":" +portNumber + contextPath );

18
只需从URL中删除URI,然后将上下文路径附加到URL即可。当您处理无需出现在URL中的默认端口80时,摆弄松散的方案和端口只会更加繁琐。
StringBuffer url = request.getRequestURL();
String uri = request.getRequestURI();
String ctx = request.getContextPath();
String base = url.substring(0, url.length() - uri.length() + ctx.length());
// ...

另请参阅:


2
5个类似的问题。最后一个简洁明了的答案。 - Nathaniel Johnson

3

据我理解,您只需要域名和上下文路径。基于这个理解,您可以使用以下方法来获取所需的字符串。

String domain = request.getRequestURL().toString();
String cpath = request.getContextPath().toString();

String tString = domain.subString(0, domain.indexOf(cpath));

tString = tString + cpath;

1

对于那些想要在它们的端点中获取所定位的端点的前台URL的人,可以使用以下方法:

request.getHeader("referer")

0
通常我有这样一个方法:
public String getAbsoluteContextPath() throws MalformedURLException {
    ExternalContext context = FacesContext.getCurrentInstance().getExternalContext();
    HttpServletRequest request = (HttpServletRequest) context.getRequest();
    URL url = new URL(request.getRequestURL().toString());
    return url.getProtocol() + "://" + url.getAuthority() + context.getRequestContextPath();
}

该方法将返回您想要的内容,仅当当前请求中存在端口号时才返回端口号。在您的情况下,它将返回:http://localhost:9090/dts


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接