We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
主要是这几个方法的返回值的使用方式问题:
request.getServletPath() request.getPathInfo() request.getRequestURI()
其中 request.getRequestURI() 的返回值包含了 request.getContextPath(),所以是相对于网站的根目录的。
request.getRequestURI()
request.getContextPath()
下面我们分析 request.getServletPath() 和 request.getPathInfo()
request.getServletPath()
request.getPathInfo()
如果我们的 servlet-mapping 如下配置:
<servlet-mapping> <servlet-name>jetbrick-template</servlet-name> <url-pattern>*.jetx</url-pattern> </servlet-mapping>
那么访问: /context/templates/index.jetx
request.getServletPath() == "/templates/index.jetx" request.getPathInfo() == <null>
<servlet-mapping> <servlet-name>jetbrick-template</servlet-name> <url-pattern>/*</url-pattern> </servlet-mapping>
request.getServletPath() == "" request.getPathInfo() == "/templates/index.jetx"
<servlet-mapping> <servlet-name>jetbrick-template</servlet-name> <url-pattern>/template/*</url-pattern> </servlet-mapping>
request.getServletPath() == "/templates" request.getPathInfo() == "/index.jetx"
总结:
所以,我们要获取相对于 request.getContextPath() 的路径,我们可以使用如下的代码:
String uri = request.getServletPath(); String pathInfo = request.getPathInfo(); if (pathInfo != null && pathInfo.length() > 0) { uri = uri + pathInfo; }
或者:
String uri = request.getRequestURI(); String contextPath = request.getContextPath(); if (contextPath != null && contextPath.length() > 0) { uri = uri.substring(contextPath.length()); }
The text was updated successfully, but these errors were encountered:
51e54be
Regression issue by #23
Sorry, something went wrong.
subchen
No branches or pull requests
主要是这几个方法的返回值的使用方式问题:
其中
request.getRequestURI()
的返回值包含了request.getContextPath()
,所以是相对于网站的根目录的。下面我们分析
request.getServletPath()
和request.getPathInfo()
如果我们的 servlet-mapping 如下配置:
那么访问: /context/templates/index.jetx
如果我们的 servlet-mapping 如下配置:
那么访问: /context/templates/index.jetx
如果我们的 servlet-mapping 如下配置:
那么访问: /context/templates/index.jetx
总结:
所以,我们要获取相对于
request.getContextPath()
的路径,我们可以使用如下的代码:或者:
The text was updated successfully, but these errors were encountered: