如何在jsp中获取包含多个参数的URL的完整路径

时间:2013-02-26 03:54:09

标签: javascript jsp url uri

假设
网址 http:/ localhost:9090 / project1 / url.jsp?id1 = 1& id2 = 2& id3 = 3

<%
String str=request.getRequestURL()+"?"+request.getQueryString();
System.out.println(str);
%>

用这个我得到输出 的 HTTP:/本地主机:9090 / PROJECT1 / url.jsp ID1 =一个

但是有了这个,我只能检索第一个参数(即id1 = 1)而不是其他参数


但如果我使用javascript我能够检索所有参数

function a()
     {
        $('.result').html('current url is : '+window.location.href );
    }

HTML:

<div class="result"></div>

我想检索要在我的下一页中使用的当前网址值,但我不想使用会话

使用以上两种方法中的任何一种,如何在jsp中检索所有参数?

提前致谢

2 个答案:

答案 0 :(得分:6)

鉴于URL = http:/ localhost:9090 / project1 / url.jsp?id1 = 1&amp; id2 = 2&amp; id3 = 3

request.getQueryString();

确实应该返回id1 = 1&amp; id2 = 2&amp; id3 = 3

请参阅HttpServletRequest.getQueryString JavaDoc

我曾经面临同样的问题,可能是由于某些测试程序失败了。 如果发生这种情况,请在清晰的环境中进行测试:新的浏览器窗口等。

Bhushan答案不等同于getQueryString,因为它解码参数值!

答案 1 :(得分:4)

我认为这就是你要找的......

String str=request.getRequestURL()+"?";
Enumeration<String> paramNames = request.getParameterNames();
while (paramNames.hasMoreElements())
{
    String paramName = paramNames.nextElement();
    String[] paramValues = request.getParameterValues(paramName);
    for (int i = 0; i < paramValues.length; i++) 
    {
        String paramValue = paramValues[i];
        str=str + paramName + "=" + paramValue;
    }
    str=str+"&";
}
System.out.println(str.substring(0,str.length()-1));    //remove the last character from String
相关问题