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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15081061/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 23:24:49  来源:igfitidea点击:

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

javascriptjspurluri

提问by lata

Suppose
URL: http:/localhost:9090/project1/url.jsp?id1=one&id2=two&id3=three

假设
网址http://localhost:9090/project1/url.jsp?id1=one&id2=two&id3=three

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

with this i get the output http:/localhost:9090/project1/url.jsp?id1=one

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

but with this i am able to retrieve only 1st parameter(i.e id1=one) not other parameters

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


but if i use javascript i am able to retrieve all parameters


但如果我使用 javascript 我可以检索所有参数

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

html:

html:

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

i want to retrieve current URL value to be used in my next page but i don't want to use sessions

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

using any of above two method how do i retrieve all parameters in jsp?

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

thanks in advance

提前致谢

采纳答案by Bhushan

I think this is what you are looking for..

我想这就是你要找的..

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

回答by Gabriel Glenn

Given URL = http:/localhost:9090/project1/url.jsp?id1=one&id2=two&id3=three

给定 URL = http://localhost:9090/project1/url.jsp?id1=one&id2=two&id3=three

request.getQueryString();

Should indeed return id1=one&id2=two&id3=three

确实应该返回 id1=one&id2=two&id3=three

See HttpServletRequest.getQueryString JavaDoc

参见HttpServletRequest.getQueryString JavaDoc

I once face the same issue, It's probably due to the some testing procedure failure. If it happens, test in a clear environment : new browser window, etc.

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

Bhushan answer is not equivalent to getQueryString, as it decode parameters values !

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