【问题标题】:how to get full path of URL including multiple parameters in jsp如何在jsp中获取包含多个参数的URL的完整路径
【发布时间】:2013-02-11 10:16:59
【问题描述】:

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

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

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

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


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

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

html:

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

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

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

提前致谢

【问题讨论】:

    标签: javascript jsp url uri


    【解决方案1】:

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

    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
    

    【讨论】:

    • 一切都是对的,只是在 str 打印的最后添加了 & 符号
    • @lata 我已经更新了我的代码,我已经测试过了,它对我有用
    • 如果最后一行设置为删除最后出现的 '&' 会更正确,因为它也适用于没有参数的 URL str.substring(0, str.lastIndexOf("&amp;"))
    【解决方案2】:

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

    request.getQueryString();
    

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

    见HttpServletRequest.getQueryString JavaDoc

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-03-11
      • 2014-05-10
      • 2020-11-03
      • 2017-04-17
      • 2013-11-24
      • 2014-09-05
      • 2016-03-27
      • 1970-01-01
      相关资源
      最近更新 更多