繁体   English   中英

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

[英]how to get full path of URL including multiple parameters in jsp

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

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

有了这个,我得到输出http:/ localhost:9090 / project1 / url.jsp?id1 = 1

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


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

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

HTML:

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

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

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

提前致谢

给定URL = http:/ localhost:9090 / project1 / url.jsp?id1 = 1&id2 = 2&id3 = 3

request.getQueryString();

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

请参阅HttpServletRequest.getQueryString JavaDoc

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

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

我想这就是你要找的......

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM