0

I have an index file and a servlet class file. I need to create a table after i submit information from the index to the servlet class. I submit the form.

        <form name="form" method="post" action="servlet">
        Number: <input type="number" name="table"/>
        <input type="submit" value="Submit"/>
        </form> 

This information is passed to the servlet as a number. I need to make tables with the number. If it is 1, it is 1 row, if iti s 5 it is 5 rows. I need to use a for loop on the servlet page, but i am stuck. I have tried something like below, but it does not work.

<table>
      <% for(int row=1; row <= 5; row++) { %>
      <tr>
      </tr>
      <% } %>
 </table>
Renuz
  • 1,587
  • 7
  • 21
  • 34

1 Answers1

2

Try to avoid Scriplets. You can use forEach JSTL tag for looping in jsp file itself.

set the count as request attribute in the servlet and then access it in jsp as shown below:

<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
...
<c:forEach begin="0" end="${count}" varStatus="loop">
    Index: ${loop.index}<br/>
</c:forEach>

Read How to loop over something a specified number of times in JSTL?

Complete example:

HTML:

<form name="form" method="post" action="servlet">
    Number: <input type="number" name="table"/>
    <input type="submit" value="Submit"/>
 </form> 

Servlet

//inside doPost method

    request.setAttribute("count", request.getParameter("table");

    // redirect to jsp 

    String nextJSP = "/table.jsp";
    RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(nextJSP);
    dispatcher.forward(request,response);

JSP:

<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
...
<c:forEach begin="0" end="${count}" varStatus="loop">
    Index: ${loop.index}<br/>
</c:forEach>
Community
  • 1
  • 1
Braj
  • 46,415
  • 5
  • 60
  • 76
  • I was trying that, but how do i implement it? Do i just put it in the servlet.java file as html code? Example: out.print("forEach code"); – Renuz Oct 23 '15 at 05:02
  • you need servlet and jsp both the files. Servlet is for server side logic and jsp for html rending. – Braj Oct 23 '15 at 05:04
  • Where would implement, <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> I am using netbeans premade servlet. – Renuz Oct 23 '15 at 05:04
  • It doesnt say to use anything but the index and servlet file in the assignment, that's the only way?. – Renuz Oct 23 '15 at 05:04
  • you need to read about JSTL core tag library that provides some handy methods to use in jsp instead of Scriptlet (java code) – Braj Oct 23 '15 at 05:05
  • you need one html (form submission), servlet (to process the form data) and one jsp to show the result. HTML -> servlet -> JSP – Braj Oct 23 '15 at 05:06
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/93126/discussion-between-renuz-and-braj). – Renuz Oct 23 '15 at 05:06
  • posted complete example – Braj Oct 23 '15 at 05:20
  • it is giving me an error for RequestDispatcher in the servlet file. – Renuz Oct 23 '15 at 06:02