Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 320
lata Avatar asked Feb 26 '13 03:02

lata


2 Answers

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 !

like image 177
Gabriel Glenn Avatar answered Nov 14 '22 22:11

Gabriel Glenn


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
like image 34
Bhushan Avatar answered Nov 14 '22 23:11

Bhushan