I initially wrote a .jsp connected by Struts 1.1 to pull some data from a database using scriplets:
<% Map<String, myObject> map = MyClass.returnMap();
for(Map.Entry<String, myObject> entry : map.entrySet()) {
myObject element = entry.getValue();
out.println("<tr>");
out.println("<td><input type=\"checkbox\" name=\"objects[]\" value=\"" + entry.getKey() + "\"/>" + entry.getKey() + "</td>");
out.println("<td>" + element.property1() + "</td>");
out.println("<td>" + element.property2() + "</td>");
}
%>
So I end up with a table with a checkbox, object name, and two of the element's properties as the columns, with a row for each entry in the Map returned by returnMap()
.
Now, I want to be able to check however many checkboxes inside the generated table, and then click a button to send a list of all the checked checkboxes to a servlet to perform some server-side calculations depending on the checkboxes selected.
One problem is that the said "submit" button is outside the table, in a separate div (used for a fixed position header). Could I just wrap a form around the entirety of the div containing the button, and the table?
I am starting out in web development, and have done some tutorials on Servlets and I understand the basic concepts. I have seen that it's generally bad practice to use scriptlets for business logic in the jsp, so I am considering generating the table through my Servlet instead. However, I also want to be able to use the elements generated by the Servlet in another Servlet method (if that makes sense).
My thought process was:
1) .JSP loads through Struts
2) .JSP receives table from Servlet
3) When submit button is clicked, sends list of checked checkboxes back to the Servlet
4) Servlet uses list of checkboxes and performs some business logic
5) .JSP refreshes with updated table
Is this a viable process? Or is there a better way to do this?
I have to access the .jsp (through Struts), not the servlet url most of the tutorials use