Use request.getParameter in multiple row

Hi…guys! I have a 10 rows table,those cells in each column share the same name,how could I retrieve them in backend using request.getParameter ?
All the data is going to be inserted into db,I am using Struts+Spring+iBatis architecture.

For instance,I have 10 rows of such data in the design.How could I achieve it?Thanks for your pleasure…:slight_smile:


<tr>
<td><input name="date" type="text" size="12" /></td>
<td><input name="billno" type="text" size="8" /></td>
<td><input name="desc" type="text" size="11" /></td>
<td><input name="purpose" type="text" size="20" /></td>
<td><input name="proName" type="text" size="6" /></td>
<td><input name="trainingCost" type="text" size="5" /></td>
<td><input name="travelCost" type="text" size="7" /></td>
<td><input name="entCost"  type="text" size="5" /></td>
<td><input name="miscCost" type="text" size="5" /></td>
<td><input name="subTotalRM" type="text" size="5" disabled="disabled" /></td>
<td><input name="subTotalFC" type="text" size="10"  disabled="disabled" /></td>
</tr>

In your Action that you submit your form to…

This solution is for Struts I. A Struts II solution isn’t too different…

You can either use the request object that is handed to your Action’s execute method:


public ActionForward execute(ActionMapping mapping, ActionForm form, javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response)
{
    String date = request.getParameter("date");
    String billno= request.getParameter("billno");
    String desc= request.getParameter("desc");
    String purpose= request.getParameter("purpose");
    String proName= request.getParameter("proName");
    String trainingCost= request.getParameter("trainingCost");
    ...
    // Build a business object from your data.
    // Submit that business object to a service.
    // The business object will then call your data access object.
}

Or, you can do it the Struts way which is to use their ActionForm. I personally never liked the ActionForm though.

Best of Luck.

Thanks for reply…:slight_smile:
Yup…This is what I did exactly,but my concern is about dealing with data from multiple rows since I have 10 rows in the design.So,user is able to submit multiple records.I just want to know that whether it might cause any confuse since the field name of each column’s element is same.

You might run into that problem. I’ve never had to deal with this, but I think this is what you could do…

You could use the request.getParameterValues(“…”) method instead. It should return you an String array of all the values for a particular parameter. But, there most likely will not be any guaranteed order to those returned values.

A suggestion would be that you number all your parameters in your html.


<tr>
<td><input name="date0" type="text" size="12" /></td>
<td><input name="billno0" type="text" size="8" /></td>
...
</tr>
<tr>
<td><input name="date1" type="text" size="12" /></td>
<td><input name="billno1" type="text" size="8" /></td>
...
</tr>
<tr>
<td><input name="date2" type="text" size="12" /></td>
<td><input name="billno2" type="text" size="8" /></td>
...
</tr>
<tr>
<td><input name="date3" type="text" size="12" /></td>
<td><input name="billno3" type="text" size="8" /></td>
...
</tr>
...

That should work just fine for what you’re trying to do.

Well…thanks for suggestion…:slight_smile:
I may try to use request.getParameterValues(“…”) to see how effective it is.

For the parameter numbering,I wonder it could end up a difficulty for me to detect which column element actually hold the data.If not I might need to use request.getParameter as a loop to retrieve the entire 10 rows elements and checking each of them whether is empty element or not.Just kindly correct me if my logic is wrong.

Well, you’re going to have to check to see if each element is empty or not anyways. If all the form elements are there, they are going to be sent to the server.

Doing a loop of some sort that works with numbered parameters would most likely be better. Because I really do not think you can guarantee the ordering of parameters coming out of the getParameterValues() method call. My guess is that you will get the values in order, but you can’t trust they will be sent the same way from every browser and that every web container you deploy in will read those values the same way.

Well…After seeing the example in RoseIndia,I decided to use request.getParameterValues(…) to deal with multiple records
http://www.roseindia.net/jsp/GetParameterValuesMethod.jsp.txt

However,my page is having NumberFormatException when submitted which mostly because of the type conversion that I applied using Double.parseDouble in Action file.


public class InsertClaimAction extends SpringAction {
	
	public ActionForward execute(ActionMapping mapping,
			                     ActionForm form,
			                     HttpServletRequest request,
			                     HttpServletResponse response) throws Exception {
		
    // string arrays that store multiple values
    String [] date = new String[10];
    String [] billno = new String[10];
    String [] description = new String[10];
    String [] purpose = new String[10];
    String [] proName = new String[10];
    String [] trainingCost = new String[10];
    String [] travelCost = new String[10];
    String [] entCost = new String[10];
    String [] miscCost = new String[10];

   if(request.getParameter("date") != null) 
       date = request.getParameterValues("date");
		
   if(request.getParameter("billno") != null)
	billno = request.getParameterValues("billno");
		
   if(request.getParameter("description") != null) 
	description = request.getParameterValues("description");
		
   if(request.getParameter("purpose") != null) 
	purpose = request.getParameterValues("purpose");
		
   if(request.getParameter("proName") != null) 
	proName = request.getParameterValues("proName");
		
   if(request.getParameter("trainingCost") != null) 
	trainingCost = request.getParameterValues("trainingCost");
		
   if(request.getParameter("travelCost") != null) 
	travelCost = request.getParameterValues("travelCost");
	
   if(request.getParameter("entCost") != null) 
	entCost = request.getParameterValues("entCost");
		
   if(request.getParameter("miscCost") != null) 
	miscCost = request.getParameterValues("miscCost");	

   ClaimDTO claim_dtldto = new ClaimDTO();	
   for(int i = 0;i < date.length;i++) {
          if(date[i] != null)
	       claim_dtldto.setDate(date[i]);
  }

   for(int i = 0;i < billno.length;i++) {
	  if(billno[i] != null)
		claim_dtldto.setBillno(billno[i]);
  }

  for(int i = 0;i < description.length;i++) {
	 if(description[i] != null)
		claim_dtldto.setDescription(description[i]);
 }

 for(int i = 0;i < purpose.length;i++) {
	if(purpose[i] != null)
		claim_dtldto.setPurpose(purpose[i]);
}

for(int i = 0;i < proName.length;i++) {
	if(proName[i] != null)
		claim_dtldto.setProName(proName[i]);
}

for(int i = 0;i < trainingCost.length;i++) {
	if(trainingCost[i] != null)
             claim_dtldto.setTrainingCost(Double.parseDouble(trainingCost[i]));
}
		
for(int i = 0;i < travelCost.length;i++) {
	if(travelCost[i] != null)
                claim_dtldto.setTravelCost(Double.parseDouble(travelCost[i]));
}
		
for(int i = 0;i < entCost.length;i++) {
        if(entCost[i] != null)
              claim_dtldto.setEntCost(Double.parseDouble(entCost[i]));
}

for(int i = 0;i < miscCost.length;i++) {
	if(miscCost[i] != null)
                claim_dtldto.setMiscCost(Double.parseDouble(miscCost[i]));
}

// service
.....

//return forward
.....
  }
}

This is the error indicated by tomcat

HTTP Status 500 -


type Exception report

message

description The server encountered an internal error () that prevented it from fulfilling this request.

exception

javax.servlet.ServletException: java.lang.NumberFormatException: empty String
org.apache.struts.action.RequestProcessor.processException(RequestProcessor.java:523)
org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:421)
org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:224)
org.apache.struts.action.ActionServlet.process(ActionServlet.java:1196)
org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:432)
javax.servlet.http.HttpServlet.service(HttpServlet.java:710)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)

root cause

java.lang.NumberFormatException: empty String
sun.misc.FloatingDecimal.readJavaFormatString(Unknown Source)
java.lang.Double.parseDouble(Unknown Source)
com.sgo.action.InsertClaimAction.execute(InsertClaimAction.java:110)
org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:419)
org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:224)
org.apache.struts.action.ActionServlet.process(ActionServlet.java:1196)
org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:432)
javax.servlet.http.HttpServlet.service(HttpServlet.java:710)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)

note The full stack trace of the root cause is available in the Apache Tomcat/6.0.13 logs.


Apache Tomcat/6.0.13

Anyone please help me to figure it out?Thanks for your pleasure…:slight_smile:

You have one ClaimDTO, which you are loading with data from as many as 10 Claims?

I would think you’d need an array of 10 ClaimDTOs to hold all that data…

Anyway, assuming all fields are required, I would boil this down to one for loop that retrieves one full Claim per loop. This way you can stop the loop and deal with the exception if the current record is incomplete or has a problem.

Also, the arrays you get from the request should never be null, however, the same cannot be said of their contents.

eg:

String nullStrings = {null,null} != String nullArray = null; // true

Thanks for reply…Can you attach some example code for the looping?

Rather than

for(this)
for(that)
for(the other thing)

do

for( i = 0; i < claimArray.length; ++i ) {

claimArray[i].setParamA( paramA[i] );
claimArray[i].setParamB( paramB[i] );
//etc
}

Hmm…I not sure what went wrong in the code.The data is not inserted into db.

I am using three DTO objects to hold the data.claimArray is the ClaimDTO array that hold the multiple record insert to table B.claimdto is the object that hold the single field record to table A.claim_dtldto is the object that hold the single field record to table B.

PK of table A and table B are claimNo and claimDtlNo respectively.claimNo is also FK of table B. HTMLUtil is just an API that I used it for type conversion.There will have two insert statements in iBatis xml.insertClaim & insertClaimDtl to insert data to table A and table B respectively.


public class InsertClaimAction extends SpringAction {
	
   public ActionForward execute(ActionMapping mapping,
			                     ActionForm form,
			                     HttpServletRequest request,
			                     HttpServletResponse response) throws Exception {
		
	// string arrays that store multiple values
	String [] date = new String[10];
	String [] billno = new String[10];
	String [] description = new String[10];
	String [] purpose = new String[10];
	String [] proName = new String[10];
	String [] trainingCost = new String[10];
	String [] travelCost = new String[10];
	String [] entCost = new String[10];
	String [] miscCost = new String[10];

        String staffName = request.getParameter("name");
        String claimMy = request.getParameter("monthyear");
	String department = request.getParameter("department");
	String designation = request.getParameter("designation");
	Double excrate =  HTMLUtil.str2double(request.getParameter("excrate"),0.0);
	Double grandTotal = HTMLUtil.str2double(request.getParameter("grandTotal"),0.0);
	Double totalRM = HTMLUtil.str2double(request.getParameter("totalRM"),0.0);
	Double totalFC =    HTMLUtil.str2double(request.getParameter("totalFC"),0.0);
	String fcName = request.getParameter("fcAbbr");

       if(request.getParameter("date") != null)
		date = request.getParameterValues("date");
		
	if(request.getParameter("billno") != null)
		billno = request.getParameterValues("billno");
		
	if(request.getParameter("description") != null)
		description = request.getParameterValues("description");
		
	if(request.getParameter("purpose") != null)
		purpose = request.getParameterValues("purpose");
		
	if(request.getParameter("proName") != null)
		proName = request.getParameterValues("proName");
		
	if(request.getParameter("trainingCost") != null)
		trainingCost = request.getParameterValues("trainingCost");
		
	if(request.getParameter("travelCost") != null)
		travelCost = request.getParameterValues("travelCost");
	
	if(request.getParameter("entCost") != null)
		entCost = request.getParameterValues("entCost");
		
	if(request.getParameter("miscCost") != null)
		miscCost = request.getParameterValues("miscCost");


      //initialize dto
	ClaimDTO claimdto = new ClaimDTO();
	ClaimDTO claim_dtldto = new ClaimDTO();
	ClaimDTO[] claimArray = new ClaimDTO[10];
	claimdto.setClaimNo(claimNo);
	claimdto.setStaffName(staffName);
	claimdto.setClaimMy(claimMy);
	claimdto.setDepartment(department);
	claimdto.setDesignation(designation);
	claimdto.setExcrate(excrate);
	claimdto.setGrandTotal(grandTotal);
	claim_dtldto.setClaimDtlNo(claimDtlNo);
	claim_dtldto.setClaimNo(claimNo);
	claim_dtldto.setTotalRM(totalRM);
	claim_dtldto.setTotalFC(totalFC);
	claim_dtldto.setFcName(fcName);

       for(int i = 0;i < claimArray.length;i++) {
		if(claimArray[i] != null) {
			claimArray[i].setDate(date[i]);
			claimArray[i].setBillno(billno[i]);
			claimArray[i].setDescription(description[i]);
			claimArray[i].setPurpose(purpose[i]);
			claimArray[i].setProName(proName[i]);
	                claimArray[i].setTrainingCost(
                                         Double.parseDouble(trainingCost[i]));
                       claimArray[i].setTravelCost(
                                         Double.parseDouble(travelCost[i]));
                      claimArray[i].setEntCost(Double.parseDouble(entCost[i]));
                      claimArray[i].setMiscCost(Double.parseDouble(miscCost[i]));
		 }	
    }

     //service
     ClaimService springservice = (ClaimService)getBean("ClaimService");
     springservice.insertClaim(claimdto);
      springservice.insertClaimDtl(claim_dtldto,claimArray);
				


Is there any faulty part in the logic,please help me to figure it out.Thanks…:slight_smile:

Have you tested your ClaimService object on its own to see if its actually writing to the database?

Yup…When I checked the log file of tomcat.The insert statement only carry null values.It is not written to db at all.But that is no any exception is being thrown.

Which statement is failing?

springservice.insertClaim(claimdto);

or

springservice.insertClaimDtl(claim_dtldto,claimArray);

Both statements are failing.The data is not inserted to both tables.

A word of warning:

Don’t rely on the data being sent ordered.

For example, date[1] may actually hold the 10:th value from the form array!
If you think there is a relation between say date[2] and billno[2] you are in for a surprise - there is no such relation. Some browsers send the values in the same order as in the html code but some don’t, which means you’ll have bogus data in some cases without knowing or being able to detect such cases. There is nothing in the specs saying how the values should be sent, IIRC.

So, a better way would be to rename the fields to for instance “date_1”, “date_2” and “billno_1”, “billno_2” etc. Then you could loop through the request parameter map and associate the fields from there. Either that or you continue on the current road and hope you don’t get caught cheating. :wink:

Its already been covered. He’ll learn on his own, eventually. :slight_smile:

So for the first statement not working. My guess is that the data you’re trying to pull from the request is actually null.

For the second request, I don’t even think you’re getting your DTO objects into your Array. You initialize the Array to be a size of 10, but I never see where you set the values of your Array with your DTOs.

Best of Luck.

Map tempMap = request.getParameterMap();
String data=(String)tempMap.get(“data”);

I know this thread is old, but I was searching for “getParameterValues and ordering”, and want to vent my spleen.

First of all, section 17.13.4 of the HTML 4.01 W3C recommendation dictates that in the data encoded as part of a form submission (content type application/x-www-form-urlencoded), names and values of controls must appear in the same order as they appear in the document.

Assuming browsers follow it (which I have not checked), that would be really useful to the original poster - and to me, dammit - but the Servlet 2.4 spec - downloadable from the JCP site - doesn’t say anything about preserving the order in getParameterValues().

That sucks.

I’d put the links for the HTML recommendation and the Servlet spec, but I’m new on this board, and it won’t let me.

That sucks too.

So tonight, the internet kinda blows.