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将什么都得不到。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值