当调用 RequestDispatcher.forward(javax.servlet.ServletRequest, javax.servlet.ServletResponse)后,再调用javax.servlet.ServletRequest的getRequestUr方法得到的是forward后的uri,之前的uri得不到了。servlet规范明确规定了这种情况:getRequestURI
java.lang.String getRequestURI()
Returns the part of this request's URL from the protocol name up to the query string in the first line of the HTTP request. The web container does not decode this String. For example:
First line of HTTP request Returned Value
POST /some/path.html HTTP/1.1 /some/path.html
GET http://foo.bar/a.html HTTP/1.0 /a.html
HEAD /xyz?a=b HTTP/1.1 /xyz
To reconstruct an URL with a scheme and host, use HttpUtils#getRequestURL.
Returns:
a String containing the part of the URL from the protocol name up to the query string
See Also:
HttpUtils#getRequestURL
getRequestURL
java.lang.StringBuffer getRequestURL()
Reconstructs the URL the client used to make the request. The returned URL contains a protocol, server name, port number, and server path, but it does not include query string parameters.
If this request has been forwarded using RequestDispatcher.forward(javax.servlet.ServletRequest, javax.servlet.ServletResponse), the server path in the reconstructed URL must reflect the path used to obtain the RequestDispatcher, and not the server path specified by the client.
Because this method returns a StringBuffer, not a string, you can modify the URL easily, for example, to append query parameters.
This method is useful for creating redirect messages and for reporting errors.
Returns:
a StringBuffer object containing the reconstructed URL
要想使用forward前的uri就需要费一翻脑筋了,好在spring UrlPathHelper提供了解决方案,public String getOriginatingRequestUri(HttpServletRequest request) 可以获取到之前的uri,UrlPathHelper是个好东西,还可以做很多事情。仔细研究一下这个方法:
/**
* Return the request URI for the given request. If this is a forwarded request,
* correctly resolves to the request URI of the original request.
*/
public String getOriginatingRequestUri(HttpServletRequest request) {
String uri = (String) request.getAttribute(WEBSPHERE_URI_ATTRIBUTE);
if (uri == null) {
uri = (String) request.getAttribute(WebUtils.FORWARD_REQUEST_URI_ATTRIBUTE);
if (uri == null) {
uri = request.getRequestURI();
}
}
return decodeAndCleanUriString(request, uri);
}
针对websphere和其他的web容器有不同的方式,但是基础方式都是从request的属性中获取forward之前的uri,自从servlet2.4版本之后,request内部其实有一个属性存储了这个值,这个属性的名字是: /**
* Special WebSphere request attribute, indicating the original request URI.
* Preferable over the standard Servlet 2.4 forward attribute on WebSphere,
* simply because we need the very first URI in the request forwarding chain.
*/
private static final String WEBSPHERE_URI_ATTRIBUTE = "com.ibm.websphere.servlet.uri_non_decoded";
/**
57 * Standard Servlet 2.4+ spec request attributes for forward URI and paths.
58 * <p>If forwarded to via a RequestDispatcher, the current resource will see its
59 * own URI and paths. The originating URI and paths are exposed as request attributes.
60 */
61 public static final String FORWARD_REQUEST_URI_ATTRIBUTE = "javax.servlet.forward.request_uri";
参照代码:
http://www.docjar.com/html/api/org/springframework/web/util/WebUtils.java.html
http://www.docjar.com/html/api/org/springframework/web/util/WebUtils.java.html
分享到:
相关推荐
HttpServletRequest 中 getRequestURL和getRequestURI的区别文档
从HttpServletRequest获取各种路径总结.docx从HttpServletRequest获取各种路径总结.docx
入参的HttpServletRequest必须为:import jakarta.servlet.http.HttpServletRequest; 运行cmd,再该目录下执行: 执行步骤:java -jar jakartaee-migration-1.0.1.jar commons-fileupload-1.4.jar commons-...
同样的,另外一个问题:String HttpServletRequest.getRequestURI();和StringBuffer HttpServletRequest.getRequestURL();返回的内容有何不同?为什么会如此?带着这些问题到网上去搜了下,没发现让自己看了明白的...
Java Web程序设计入门
JavaWeb开发技术-HttpServletRequest对象.pptx
Java中,引入javax.servlet.http.HttpServletRequest和javax.servlet.http.HttpServletResponse包的必备jar包:org.apache.commons.httpclient.jar
HttpServletRequest 详解 HttpServletRequest对象代表客户端的请求,当客户端通过HTTP协议访问服务器时,HTTP请求头中的所有信息都封装在这个对象中,通过这个对象提供的方法,可以获得客户端请求的所有信息。
这是很好的HttpServletResponse HttpServletRequest.加强.ppt,实际例子,值得收藏!
String uri = request.getRequestURI(); String path = uri.substring(uri.lastIndexOf("/"), uri.lastIndexOf(".")); response.setContentType("text/html;charset=utf-8"); PrintWriter out = response.getWriter...
javax.servlet JAR包,解决找不到 import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; jar包问题
传智博客PPT HttpServletRequest的应用.ppt
* String url = (String) request.getAttribute(WebUtils.FORWARD_REQUEST_URI_ATTRIBUTE); */ 三、删除了配置文件中的cookie写入域的问题。 在配置文件里(spring-shiro.xml )中的配置有所修改。 <!-- ...
org.springframework.mock.web.MockHttpServletRequest.class org.springframework.mock.web.MockHttpServletResponse.class org.springframework.mock.web.MockHttpSession.class org.springframework.mock.web....
2,转向处理地址:通过第一个链接微信会把code传过来,之前参数获取就行 @RequestMapping(value = "/paydispatcher", method = { RequestMethod.GET }) public void payDispatcher(HttpServletRequest request, ...
分析HttpServletRequest 内容 解析出设备来源 手机 电脑 什么种类浏览器 什么系统
httpservletrequest、httpsession的jar包,导入资源包。
【项目实战案例】java校园订餐系统项目(web端) /* * Generated by MyEclipse Struts ...import org.apache.struts.action.ActionForward; import org.apache.struts.action.ActionMapping; import com.bean.HzpBean;
文章目录项目环境部分实现代码遇到的问题解决方案说明 项目环境 SpringBoot 1.5 Shiro 权限管理 vue / axios 部分实现代码 public Result admLogin(HttpServletRequest request, HttpServletResponse response, ...
request.getRequestDispatcher("/admin/category/addCategory.jsp").forward(request, response); }else if(sortNo==null||sortNo.equals("")){ request.setAttribute("msg", "请输入分区序号"); request...