Click to See Complete Forum and Search --> : reading url parameters


jrthor2
01-29-2010, 02:32 PM
I know you can read url parameters using HttpServletRequest, but what I need to do is to read the parameters, and loop through them and put them in the request like this:

request.setAttribute("name", "value");

so that velocity can get access to them. The url could have mulitple parameters, not just one, like:

http://www.site.com/mysite?page=page1&subpage=2

So I would need to put both of these in the request.

Could someone help me with this?

Thanks!!

javawebdog
01-29-2010, 04:10 PM
See the Java API on Enumerations
This is the general idea though.


// Get an enumeration of all parameters
Enumeration enum = req.getParameterNames();
//Loop through the enumeration
for (enum.hasMoreElements(); ) {
// Get the name of the request parameter
name = (String)enum.nextElement();

// Get the value of the request parameter
value = req.getParameter(name);

// If the request parameter can appear more than once in the query string, get all values
String[] values = req.getParameterValues(name);

for (int i=0; i<values.length; i++) {
/* do something */
}
}