0

I am able to display a table when a user clicks a search button. But now I want to split the table into chunks of 20 rows where the user can click next or previous to go forward and backward through all of the data. I am not allowed to use JavaScript for this assignment. Only JSP, Java, HTML.

The two debugging out.print() calls are not showing up. A different page is being loaded after one of the buttons is clicked, but the two debugging out.print calls are not displaying any HTML. I have checked out How to know which button is clicked on jsp this post but had no luck.

    <form method="GET">
      <center>
        <input type="submit" name="previous_table" value="Previous" />
        <input type="submit" name="next_table" value="Next" />
      </center>
    </form>
    </br>

    <%
          String val1 = request.getParameter("previous_table");
          String val2 = request.getParameter("next_table");
          try {
            if ("Previous".equals(val1)) { // Get previous results
              out.println("<h1>HELLO 1</h1>");
              buildTable(rs, out, false);
            }
            else if ("Next".equals(val2)) { // Get next results
              out.println("<h1>HELLO 2</h1>");
              buildTable(rs, out, true);
            }
          } catch(Exception e) {
            out.print("<center><h1>"+e.toString()+"</h1></center>");
          }
    %>

I also have a follow up question. If the user clicks next or previous button, will my current table on the page be overwritten by the new one? That is my intent but I don't know if it will work that way.

I WAS ABLE TO FIX IT BY DOING THIS:

    <form method="POST">
      <center>
        <input type="submit" name="previous_table" value="Previous" />
        <input type="submit" name="next_table" value="Next" />
      </center>
    </form>
    </br>

    <%
          String val1 = request.getParameter("previous_table");
          String val2 = request.getParameter("next_table");

3 Answers3

1

you should add name with value for button after that you can get by parameter click value.

`<input type="hidden" name="myprevious" value="previous"/>
<input type="hidden" name="mynext" value="next" />
<%
   String val1 = request.getParameter("myprevious");
   String val2 = request.getParameter("mynext");
   try {
     if (val1 == "previous") { // Get previous results
       out.println("<h1>HELLO 1</h1>");
       buildTable(rs, out, false);
     }
     else if (val2 == "next") { // Go next results
       out.println("<h1>HELLO 2</h1>");
       buildTable(rs, out, true);
     }
   } catch(Exception e) {
     out.print("<center><h1>"+e.toString()+"</h1></center>");
   } 
%>

`

I hope it will help you.

Thanks.

1

Try to use the subList() method of a List<>(). As explained here.

HOW TO IMPLEMENT IT ##

you can put an hidden input in your form to give you the last index for your list like :

<input type="hiden" value="${last_index_of_your_list + 1}" name="index">

Then in your servlet part you put like this :

int index = Interger.ParseInt(request.getParameter("index"));

if(index <= 0){
   datalist = datalist(0, 19>datalist.size()? datalist.size() : 19);
}else{

   if(clicked_on_next){

      datalist = datalist(index, index+19>datalist.size()? datalist.size() : index+19 );

   }else{

      datalist = datalist(index - 40, index-20>datalist.size()? datalist.size() : index-20 );

   }

}
Big Zed
  • 417
  • 5
  • 11
0

You are using hidden fields but you need to use submit button for next and previous.

<input type="submit" name="myprevious" value="previous"/>
<input type="submit" name="mynext" value="next" />

Make sure both are define in form. when we submit form after that you will get button value in parameter.same my previous answer. because we can not get parameter value without submit form.

Thanks.