As we observe during online ticket reservation we will enter passenger details in textfields in a table. how values will be fetched from that Jsp page to action class?

Dani AI

Generated

As described, the usual pattern for a ticket-reservation table is: collect a row of inputs for each passenger in the JSP, then map those rows to an ArrayList<Passenger> (a POJO) in your server-side action/servlet. pointed toward servlets and hinted at parameter access — below are two practical, accurate ways to do that and key pitfalls to watch for.

Plain JSP + Servlet (manual mapping)

  • Give each column the same field name across rows (e.g., multiple inputs named name and age). In the servlet use request.getParameterValues(...) to get parallel arrays, iterate by index and build POJOs.

Example JSP fragment:

<tr>
  <td><input type="text" name="name" /></td>
  <td><input type="text" name="age" /></td>
</tr>

Example servlet mapping loop:

String[] names = request.getParameterValues("name");
String[] ages  = request.getParameterValues("age");
List<Passenger> list = new ArrayList<>();
if (names != null) {
  for (int i = 0; i < names.length; i++) {
    String n = names[i].trim();
    if (n.isEmpty()) continue; // skip empty rows
    Passenger p = new Passenger();
    p.setName(n);
    try { p.setAge(Integer.parseInt(ages[i])); } catch (Exception e) { p.setAge(null); }
    list.add(p);
  }
}
request.setAttribute("passengers", list);

Framework binding (Struts2 / Spring MVC)

  • If using a framework that supports indexed properties, name inputs like passengers[0].name, passengers[1].age etc. A List<Passenger> passengers property on the action/controller will be populated automatically.

Practical notes and troubleshooting

  • Always check arrays for null and unequal lengths to avoid IndexOutOfBounds.
  • Trim and validate each field; handle parse errors (age, seat numbers).
  • Ignore completely empty rows.
  • Protect against malicious input (escape output, validate server-side).
  • For dynamic rows, keep indexes contiguous or submit JSON and parse it server-side (modern and robust).
  • If results are not as expected, dump Arrays.toString(request.getParameterValues("name")) to log to verify what the browser actually sent.

This covers a reliable, production-ready way to convert table textfields into an ArrayList of POJOs whether you do it manually in a servlet or let your framework bind indexed inputs automatically.

Recommended Answers

All 2 Replies

learn using servlets.
check the two sticky posts in this forum.

You can access using request.getParameter

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.