HttpServletRequest获取请求路径
程序员文章站
2022-03-17 12:21:20
...
HttpServletRequest获取请求路径 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 // eg. /ser.do?method=add_pre&type=mad String url = request.getRequestURI(); return /ssm/ser.do 2、 //The returned URL contains a protocol, server name, port number, and server path, but it does not include query string parameters //eg. http://localhost:8080/ssm/ser.do?method=add_pre&type=mad StringBuffer url_buffer = request.getRequestURL(); return http://localhost:8080/ssm/ser.do
HttpServletRequest 的这两种方法都只能得到不包含参数的请求url,区别如下:
1 前者返回相对路径,后者返回完整路径
2 前者返回string ,后者返回stringbuffer
要想得到完整请求url可以通过如下方法,getQueryString()得到的是url后面的参数串,和前者相加就是带参数的请求路径了
String queryString = request.getQueryString(); String fullPath = url + queryString; // 或者是url_buffer.toString()+queryString; 即 /ssm/ser.do + method=add_pre&type=mad