HttpServletRequest获取请求路径

 

Java代码   收藏代码
  1. HttpServletRequest获取请求路径  
  2. 1、        //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  
  3.         //  eg.  /ser.do?method=add_pre&type=mad  
  4.         String url = request.getRequestURI();    
  5.     return /ssm/ser.do  
  6. 2、        //The returned URL contains a protocol, server name, port number, and server path, but it does not include query string parameters  
  7.           //eg.      http://localhost:8080/ssm/ser.do?method=add_pre&type=mad  
  8.         StringBuffer url_buffer = request.getRequestURL();  
  9.     return http://localhost:8080/ssm/ser.do  


HttpServletRequest 的这两种方法都只能得到不包含参数的请求url,区别如下: 

1 前者返回相对路径,后者返回完整路径 

2 前者返回string ,后者返回stringbuffer 

要想得到完整请求url可以通过如下方法,getQueryString()得到的是url后面的参数串,和前者相加就是带参数的请求路径了 
Java代码   收藏代码
  1.     String queryString = request.getQueryString();  
  2. String fullPath = url + queryString;     
  3. // 或者是url_buffer.toString()+queryString;  
  4. 即 /ssm/ser.do + method=add_pre&type=mad  

再分享一下我老师大神的人工智能教程吧。零基础!通俗易懂!风趣幽默!还带黄段子!希望你也加入到我们人工智能的队伍中来!https://blog.csdn.net/jiangjunshow

猜你喜欢

转载自www.cnblogs.com/sjwudhwhhw/p/10298227.html