Java 如何在没有上下文路径的情况下获取请求 URI?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4278083/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 15:17:22  来源:igfitidea点击:

How to get request URI without context path?

javaservlets

提问by craftsman

The Method request.getRequestURI()returns URI with context path.

方法request.getRequestURI()返回带有上下文路径的 URI。

For example, if the base URL of an application is http://localhost:8080/myapp/(i.e. the context path is myapp), and I call request.getRequestURI()for http://localhost:8080/myapp/secure/users, it will return /myapp/secure/users.

例如,如果一个应用程序的基本网址http://localhost:8080/myapp/(即上下文路径是MYAPP),和我打电话request.getRequestURI()http://localhost:8080/myapp/secure/users,它将返回/myapp/secure/users

Is there any way we can get only this part /secure/users, i.e. the URI without context path?

有什么办法可以只得到这部分/secure/users,即没有上下文路径的 URI?

采纳答案by BalusC

If you're inside a front contoller servlet which is mapped on a prefix pattern, then you can just use HttpServletRequest#getPathInfo().

如果您在映射到前缀模式的前端控制器 servlet 中,那么您只需使用HttpServletRequest#getPathInfo().

String pathInfo = request.getPathInfo();
// ...

Assuming that the servlet in your example is mapped on /secure, then this will return /userswhich would be the information of sole interest inside a typical front controller servlet.

假设您的示例中的 servlet 被映射到/secure,那么这将返回/users这将是典型前端控制器 servlet 内唯一感兴趣的信息。

If the servlet is however mapped on a suffix pattern (your URL examples however does not indicate that this is the case), or when you're actually inside a filter (when the to-be-invoked servlet is not necessarily determined yet, so getPathInfo()could return null), then your best bet is to substring the request URI yourself based on the context path's length using the usual Stringmethod:

但是,如果 servlet 映射到后缀模式(但是,您的 URL 示例并未表明是这种情况),或者当您实际上位于过滤器中时(当尚未确定要调用的 servlet,因此getPathInfo()可能返回null),那么最好的办法是使用常用String方法根据上下文路径的长度自己对请求 URI 进行子字符串化:

HttpServletRequest request = (HttpServletRequest) req;
String path = request.getRequestURI().substring(request.getContextPath().length());
// ...

回答by PeterMmm

A way to do this is to rest the servelet context path from request URI.

一种方法是从请求 URI 中保留小服务上下文路径。

String p = request.getRequestURI();
String cp = getServletContext().getContextPath();

if (p.startsWith(cp)) {
  String.err.println(p.substring(cp.length());
}

Read here.

在这里阅读。

回答by Colin

May be you can just use the split method to eliminate the '/myapp' for example:

可能您可以使用 split 方法来消除“/myapp”,例如:

string[] uris=request.getRequestURI().split("/");
string uri="/"+uri[1]+"/"+uris[2];

回答by fforw

request.getRequestURI().substring(request.getContextPath().length())

回答by lukastymo

getPathInfo() sometimes return null. In documentation HttpServletRequest

getPathInfo() 有时返回 null。在文档HttpServletRequest 中

This method returns null if there was no extra path information.

如果没有额外的路径信息,则此方法返回 null。

I need get path to file without context path in Filter and getPathInfo() return me null. So I use another method: httpRequest.getServletPath()

我需要在过滤器中获取没有上下文路径的文件路径,并且 getPathInfo() 返回 null。所以我使用另一种方法:httpRequest.getServletPath()

public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException
{
    HttpServletRequest httpRequest = (HttpServletRequest) request;
    HttpServletResponse httpResponse = (HttpServletResponse) response;

    String newPath = parsePathToFile(httpRequest.getServletPath());
    ...

}

回答by thetoolman

If you use request.getPathInfo() inside a Filter, you always seem to get null (at least with jetty).

如果您在过滤器中使用 request.getPathInfo() ,您似乎总是会得到 null(至少在 jetty 中)。

This terse invalid bug + response alludes to the issue I think:

这个简洁的无效错误 + 响应暗示了我认为的问题:

https://issues.apache.org/bugzilla/show_bug.cgi?id=28323

https://issues.apache.org/bugzilla/show_bug.cgi?id=28323

I suspect it is related to the fact that filters run before the servlet gets the request. It may be a container bug, or expected behaviour that I haven't been able to identify.

我怀疑这与过滤器在 servlet 获取请求之前运行的事实有关。这可能是一个容器错误,或者我无法识别的预期行为。

The contextPath is available though, so fforws solution works even in filters. I don't like having to do it by hand, but the implementation is broken or

但是 contextPath 是可用的,所以 fforws 解决方案甚至在过滤器中也能工作。我不喜欢必须手动完成,但实现已损坏或

回答by James

With Spring you can do:

使用 Spring,您可以:

String path = new UrlPathHelper().getPathWithinApplication(request);