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

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

本文关键字:参数 URL 路径 包含多 jsp 获取      更新时间:2023-09-26

假设
URLhttp://localhost:9090/project1/URL.jsp?id1=one&id2=2&id3=三

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

用这个我得到输出http://localhost:9090/project1/url.jsp?id1=一个

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


但如果我使用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=one&id2=2&id3=三个

request.getQueryString();

确实应该返回id1=one&id2=2&id3=三个

请参阅HttpServlet请求.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