33

I have n number of text fields named in the form "Question.....". How can I get all the parameters which starts with "question" from the JSP page to the Action?

0

6 Answers 6

72
+50
<%@ page import = "java.util.Map" %>
Map<String, String[]> parameters = request.getParameterMap();
for(String parameter : parameters.keySet()) {
    if(parameter.toLowerCase().startsWith("question")) {
        String[] values = parameters.get(parameter);
        //your code here
    }
}
5
  • 3
    good answer. One remark though, you shouldn't iterate a map like this, you should use something like for(Entry<String, String[]> paramEntry : parameters.entrySet()) Commented Apr 4, 2013 at 6:55
  • What advantage does that give over the approach above?
    – Finbarr
    Commented Apr 4, 2013 at 23:11
  • 1
    Performance. In the first approach, you iterate through keys and perform a lookup for each key. In practice, there is not a big difference in performance, especially for request parameters which is a tiny map. Nevertheless, it's recommended to avoid the lookup if you can. Commented Apr 10, 2013 at 9:22
  • Actually the lookup only takes place for keys that start with "question", but as you mention it is theoretically slower than iterating over the entries.
    – Finbarr
    Commented Apr 10, 2013 at 16:43
  • Another advantage is that you have the information you need in a single object. This works well when using streams. Commented Feb 28, 2017 at 16:18
30

Even though this is an old question, I had to do something similar today but I prefer JSTL:

<c:forEach var="par" items="${paramValues}">
    <c:if test="${fn:startsWith(par.key, 'question')}"> 
${par.key} = ${par.value[0]}; //whatever
    </c:if>
</c:forEach>
1
  • Also you can use ${par.key.startsWith('question')} instead of fn:startsWith().
    – Cherry
    Commented Jan 10, 2014 at 6:03
7

This should print out all Parameters that start with "Question".

<html><body>
<%@ page import = "java.util.*" %>
<b>Parameters:</b><br>
<%
  Enumeration parameterList = request.getParameterNames();
  while( parameterList.hasMoreElements() )
  {
    String sName = parameterList.nextElement().toString();
    if(sName.toLowerCase.startsWith("question")){
      String[] sMultiple = request.getParameterValues( sName );
      if( 1 >= sMultiple.length )
        // parameter has a single value. print it.
        out.println( sName + " = " + request.getParameter( sName ) + "<br>" );
      else
        for( int i=0; i<sMultiple.length; i++ )
          // if a paramater contains multiple values, print all of them
          out.println( sName + "[" + i + "] = " + sMultiple[i] + "<br>" );
    }
  }
%>
</body></html>
1
  • toLowerCase is missing parenthesis
    – Matiaan
    Commented Sep 30, 2021 at 11:47
2
localhost:8080/esccapp/tst/submit.jsp?key=datr&key2=datr2&key3=datr3

    <%@page import="java.util.Enumeration"%>

    <%
    Enumeration in = request.getParameterNames();
    while(in.hasMoreElements()) {
     String paramName = in.nextElement().toString();
     out.println(paramName + " = " + request.getParameter(paramName)+"<br>");
    }
    %>

    key = datr
    key2 = datr2
    key3 = datr3
1

The fastest way should be:

<%@ page import="java.util.Map" %>
Map<String, String[]> parameters = request.getParameterMap();
for (Map.Entry<String, String[]> entry : parameters.entrySet()) {
    if (entry.getKey().startsWith("question")) {
        String[] values = entry.getValue();
        // etc.

Note that you can't do:

for (Map.Entry<String, String[]> entry : 
     request.getParameterMap().entrySet()) { // WRONG!

for reasons explained here.

0
HTML or Jsp Page         
<input type="text" name="1UserName">
<input type="text" name="2Password">
<Input type="text" name="3MobileNo">
<input type="text" name="4country">
and so on...
in java Code 

 SortedSet ss = new TreeSet();
 Enumeration<String> enm=request.getParameterNames();
while(enm.hasMoreElements())
{
    String pname = enm.nextElement();
    ss.add(pname);
}
Iterator i=ss.iterator();
while(i.hasNext())
{
    String param=(String)i.next();
    String value=request.getParameter(param);
}
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.