Java获取uri and ?username=tom&age=22

    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

getQueryString

java.lang.String getQueryString()

Returns the query string that is contained in the request URL after the path.This method returns null if the URL does not have a query string. Same as the value of the CGI variable QUERY_STRING.Returns:a String containing the query string or null if the URL contains no query string. The value is not decoded by the container. For example, if the client’s browser was directed at http://servername/webname/pagename.csp?paramName=paramValue, the query string would be paramName=paramValue.

注意一点:

API第一句:Returns the query string that is contained in the request URL after the path,这就说明它只对get方法抛的数据有效。post方法传的参数getQueryString将什么都得不到。

阳光总在风雨后。只有坚强的忍耐顽强的奋斗,

Java获取uri and ?username=tom&age=22

相关文章:

你感兴趣的文章:

标签云: