The Method request.getRequestURI() returns URI with context path.
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
.
例如,如果应用程序的基本URL是http://localhost:8080/myapp/(即上下文路径是myapp),并且我为http://localhost:8080/myapp/secure/users调用request.getRequestURI(),它将返回/myapp/secure/users。
Is there any way we can get only this part /secure/users
, i.e. the URI without context path?
我们是否有办法只获得这个部分/安全/用户,即没有上下文路径的URI ?
7 个解决方案
#1
130
If you're inside a front contoller servlet which is mapped on a prefix pattern, then you can just use HttpServletRequest#getPathInfo()
.
如果您在前面的contoller servlet中,它映射到前缀模式,那么您可以使用HttpServletRequest#getPathInfo()。
String pathInfo = request.getPathInfo();
// ...
Assuming that the servlet in your example is mapped on /secure
, then this will return /users
which 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 String
method:
然而如果servlet映射在一个后缀模式(你的URL示例然而不表明是这种情况),或者当你在一个过滤器(不一定要调用servlet时确定,所以getPathInfo()将返回null),那么最好的办法就是子串请求URI自己根据上下文路径的长度使用常规字符串方法:
HttpServletRequest request = (HttpServletRequest) req;
String path = request.getRequestURI().substring(request.getContextPath().length());
// ...
#2
62
request.getRequestURI().substring(request.getContextPath().length())
#3
21
With Spring you can do:
有了春天,你可以做:
String path = new UrlPathHelper().getPathWithinApplication(request);
#4
12
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());
...
}
#5
4
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:
这个简短的无效bug +响应暗示了我认为的问题:
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解决方案甚至可以在过滤器中工作。我不喜欢手工操作,但是实现已经中断了。
#6
2
A way to do this is to rest the servelet context path from request URI.
这样做的方法是从请求URI中rest servelet上下文路径。
String p = request.getRequestURI();
String cp = getServletContext().getContextPath();
if (p.startsWith(cp)) {
String.err.println(p.substring(cp.length());
}
Read here .
在这里阅读的。
#7
-1
May be you can just use the split method to eliminate the '/myapp' for example:
也许你可以使用分割方法来消除'/myapp'例如:
string[] uris=request.getRequestURI().split("/");
string uri="/"+uri[1]+"/"+uris[2];
#1
130
If you're inside a front contoller servlet which is mapped on a prefix pattern, then you can just use HttpServletRequest#getPathInfo()
.
如果您在前面的contoller servlet中,它映射到前缀模式,那么您可以使用HttpServletRequest#getPathInfo()。
String pathInfo = request.getPathInfo();
// ...
Assuming that the servlet in your example is mapped on /secure
, then this will return /users
which 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 String
method:
然而如果servlet映射在一个后缀模式(你的URL示例然而不表明是这种情况),或者当你在一个过滤器(不一定要调用servlet时确定,所以getPathInfo()将返回null),那么最好的办法就是子串请求URI自己根据上下文路径的长度使用常规字符串方法:
HttpServletRequest request = (HttpServletRequest) req;
String path = request.getRequestURI().substring(request.getContextPath().length());
// ...
#2
62
request.getRequestURI().substring(request.getContextPath().length())
#3
21
With Spring you can do:
有了春天,你可以做:
String path = new UrlPathHelper().getPathWithinApplication(request);
#4
12
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());
...
}
#5
4
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:
这个简短的无效bug +响应暗示了我认为的问题:
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解决方案甚至可以在过滤器中工作。我不喜欢手工操作,但是实现已经中断了。
#6
2
A way to do this is to rest the servelet context path from request URI.
这样做的方法是从请求URI中rest servelet上下文路径。
String p = request.getRequestURI();
String cp = getServletContext().getContextPath();
if (p.startsWith(cp)) {
String.err.println(p.substring(cp.length());
}
Read here .
在这里阅读的。
#7
-1
May be you can just use the split method to eliminate the '/myapp' for example:
也许你可以使用分割方法来消除'/myapp'例如:
string[] uris=request.getRequestURI().split("/");
string uri="/"+uri[1]+"/"+uris[2];