简体   繁体   中英

How to get elements of arrayList from servlet to jsp and display on the same jsp without using AJAX

This is my servlet code

 con.getCliWithProp(fromdate, todate, packageid, quantity,pro);
        for(int i=0;i<Integer.parseInt(quantity);i++){
            results.add(con.resultCli.toArray()[i].toString());

        }System.out.println(results);
        request.setAttribute("cli",results);
    }
    request.getRequestDispatcher("/npt.jsp").forward(request, response);
    }

I am getting this arralyList in a jsp, and my JSP code is

<form action="getDetails" method="post">
<label>From date</label>
<input type="text" name="fromDate" placeholder="From Date" id="datepicker">
<label>To date</label>
<input type="text" name="toDate" placeholder="To Date" id="datepicker1">
<br>
<button class="btn btn-success"  id='submit' >Generate CLI</button>

 </form>
 <br><br>
 <div id="display">
 <pre>
 <font size="4">${requestScope.cli}</font>
 </pre>
 </div>

when I give from date and to date, it fetches me the data in an arrayList but displays it in the format like

['122131232','123232312']

I want to display them element wise on that same jsp page without using AJAX.

To display it element wise, use JSTL <c:forEach/> tag,

<div id="display">
    <pre>
     <c:forEach items=${requestScope.cli} var="item">
     <font size="4">${item}</font> <br />
     </c:forEach>
    </pre>
</div>

To use ajax, use JQuery ajax with Servlet and JSON,

<script>
$(document).ready(function() {
  $('#button').click(function() {
         $.get('JsonStrListServlet', function(responseJson) {   
         // Parse the json result on somediv
          var $ul = $('<ul>').appendTo($('#somediv')); 
          $.each(responseJson, function(index, item) {
          $('<li>').text(item).appendTo($ul);  
          });
      });

  });
</script>

if you only want to get length of a Collection use fn:length(tableEntity.rows))

and for the purpose of iterating use JSTL <c:forEach>

<c:forEach items='${List}' var='varObj'>
<c:out value="${varObj}"/>
</c:forEach>

Hope this will help.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM