-2

I'm working on a piece of code where I have an ArrayList of items. I'm putting this on a webpage using HTML, and I want to print all the items in a list with an input field next to each item, where a user can type in a quantity - kind of like a shopping cart.

How would I go about doing this purely in an HTML document? I am currently doing this within the Java code itself, but I understand this is bad practice, and I don't know how to retrieve the number the type in - how would I do that?

Would I be better off doing this with JSP? If so, can you give me some advice as to how to go about doing that?

Thanks in advance. Here is the code that I have been working with so far.

int counter = 1;
out.println("<html><body><br><br>");
for (Gear g : gear) {
    out.println(counter + ". " + g.toString() + "&nbsp; &nbsp; &nbsp; &nbsp; <input type=\"text\" name=\"" + counter + "\">" + "<br>");
    counter++;
}
out.println("<br><br><br> <input type=\"submit\" value=\"Enter\" name=\"en\" </html></body>");

I've been using servlets and Tomcat, by the way.

EDIT: I see that someone has said this is a duplicate of a previous question. I would personally argue that my question is different, especially since my focus here is on HTML and not JSP - but whatever, fine. If someone has any thoughts on the HTML here, I would really appreciate them.

EDIT 2: Alternatively, how might I dynamically name each input box in the loop?

<table>
    <c:forEach items="${gear}" var="g">
        <tr>
            <td>${g.category}</td> 
            <td>${g.desc1}</td>
            <td>${g.quant}</td>
            <td><input type="text"></td>
        </tr>
    </c:forEach>
</table>
jpabene
  • 129
  • 2
  • 13

2 Answers2

0

Alternatively, how might I dynamically name each input box in the loop?

PFB the answer

Add a varStatus in your foreach and name your input box as below varStatus="vStatus" and input type="text" id="${pStatus}"

<table>
    <c:forEach items="${gear}" var="g" varStatus="vStatus">
        <tr>
            <td>${g.category}</td> 
            <td>${g.desc1}</td>
            <td>${g.quant}</td>
            <td><input type="text" id="${pStatus}" name="${pStatus}"></td>
        </tr>
    </c:forEach>
</table>
Thomas
  • 498
  • 4
  • 16
0

Use varStatus:

<c:forEach items="${gear}" var="g" varStatus='i'>
    ...
    <td><input type="text" name='${i.count}'></td>
    ...
</c:forEach>