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?
            Asked
            
        
        
            Active
            
        
            Viewed 1.2e+01k times
        
    6 Answers
72
            <%@ 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
    }
}
- 
                    3good answer. One remark though, you shouldn't iterate a map like this, you should use something like for(EntryparamEntry : parameters.entrySet()) – Victor Ionescu Apr 04 '13 at 06:55
- 
                    What advantage does that give over the approach above? – Finbarr Apr 04 '13 at 23:11
- 
                    1Performance. 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. – Victor Ionescu Apr 10 '13 at 09: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 Apr 10 '13 at 16:43
- 
                    Another advantage is that you have the information you need in a single object. This works well when using streams. – Thorbjørn Ravn Andersen Feb 28 '17 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>
 
    
    
        Filipe Pina
        
- 2,201
- 23
- 35
- 
                    Also you can use ${par.key.startsWith('question')} instead of fn:startsWith(). – Cherry Jan 10 '14 at 06: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>
 
    
    
        lajuette
        
- 997
- 1
- 7
- 18
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
 
    
    
        Pax Exterminatus
        
- 504
- 4
- 9
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);
}
 
    
    
        Nilesh
        
- 16
- 1
 
     
     
     
     
    