HttpServletRequest request = ServletActionContext. getRequest(); String uri = request.getRequestURI(); StringBuffer sb = null; if(uri != null) sb = new StringBuffer(uri); else sb = new StringBuffer(); String s = request.getQueryString(); if(s!=null) sb.append("?").append(s); String url = sb.toString();
注意到上面几处,第一处,通过request获取到uri,
第二处,通过request.getQueryString()获取到查询的字符串等,如username=tom&age=22,
第三处,对uri进行处理之后,用?将两部分拼接起来,最后得到url。
还有一点,可以在此基础上延伸,如果两次访问的url的hashCode不同(判断条件),可以清除cache。
补充:request.getQueryString()的API
From: http://docs.oracle.com/javaee/6/api/javax/servlet/http/HttpServletRequest.html#getQueryString%28%29
java.lang.String getQueryString()
null
if the URL does not have a query string. Same as the value of the CGI variable QUERY_STRING.
String
containing the query string or
null
if the URL contains no query string. The value is not decoded by the container.
注意一点:
API第一句:Returns the query string that is contained in the request URL after the path,这就说明它只对get方法抛的数据有效。post方法传的参数getQueryString将什么都得不到。