how to get full path of URL including multiple par

2019-06-28 02:08发布

问题:

Suppose
URL: 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

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


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

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

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

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

thanks in advance

回答1:

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


回答2:

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

request.getQueryString();

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

See 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 !