How to filter table elements in jsp page based on user selection

Viewed 170

index.jsp page

!DOCTYPE html>
<html>
<head>

</head>
<body>

<div align="center">

<form action ="demo.jsp" method="post">
    <table id="formTable">
        <tbody>
        
        <tr>
            <th> <label>START Date: </label> </th>
            <th> <input type = "date" name="Date1" id="date"></th>
         
        </tr>
        <tr>
            <th> <label>END Date: </label> </th>
            <th> <input type = "date" name="Date2" id="date"></th>
        </tr>
        
        <tr>
    
        <td> <label>Author </label> </td>
        <td> <input type ="text" name ="Author" id="author"> </td>
        </tr>
        <tr>
    
        <td> <label>Book Name </label> </td>
        <td> <input type ="text" name ="book_Name" id="bookName"> </td>
        </tr>
        <tr>

        <td> <label>Domain </label> </td>
        <td>  <select id="domain" name="Domain" id="domain">
                  <option value ="" selected Disabled>Select Domain</option>
                  <option>Biology</option>
                  <option>chemistry</option>
                  <option>maths</option>
                  
                  </select> </td>
                  </tr>
                  
        
        
        
        </tbody>
        </table>
        <br>
        <br>
        <input type = "submit"  value = "Submit" name = "submit"/>  
</form>
</div>
</body>
</html>

demo.jsp page

 <!DOCTYPE html>
    <html>
    
    
    <body>
    
    
    <div align="center">
    <table id="customers" >
        <thead>
            <tr>
                <td> <label> Id </label> </td>
                <td> <label>book Name </label> </td>
                <td> <label>author </label> </td>
                <td> <label>domain</label></td>
                
                
            </tr>  
        
        
        
            <%
    
        
                      try{
                         
                          String query ="select * from book"
                          Connection conn = SqlConnection.getConnection();
                          Statement stmt = conn.createStatement();
                          ResultSet rs = stmt.executeQuery(query);
                          while(rs.next()){
                              %>
                              <tr>
                              <td><input type = text readonly value ="<%=rs.getString("Id")%>"> </td>
                              <td><input type = text readonly value ="<%=rs.getString("bookName")%>"> </td>
                              <td><input type = text readonly value ="<%=rs.getString("author")%>"></td>
                              <td><input class ="btn"type = text readonly value ="<%=rs.getString("domain")%>"title="<%=rs.getString("domain")%>"> </td>
                             
                              
                              </tr>
                              <%
                              }
                        }
                        catch(Exception ex){
                            System.out.println(ex);
                        }
               %>
        </thead>
    </table>
    
    
    
    </div>
    
    </body>
    </html>

These are my jsp pages - in the index page if a user selects a start date and an end date only reports within that daterange should be selected. If a user fills out any other text fields along with this start and end date it should filter the results according to this field. For example if I select the start date 01/01/2020 and the end date 28/02/2020 and also the domain 'biology', then the reports with the domain 'biology' should be displayed.

Thanks in advance.

Updated demo.jsp

<html>


<body>


<div align="center">
<table id="customers" >
    <thead>
        <tr>
            <td> <label> Id </label> </td>
            <td> <label>book Name </label> </td>
            <td> <label>author </label> </td>
            <td> <label>domain</label></td>
            
            
        </tr>  
    
    
    
        <%
                 string sdate =request.getParameter("date1");
                 string edate =request.getParameter("date2");
                 string author =request.getParameter("Author");
                 string book =request.getParameter("book_name");
                 string domain =request.getParameter("Domain");

    
                  try{
                     
                      String query ="select * from book where date 
                      between"'+sdate'" and "'+end+'" or author = 
                      "'+author+'" or bookName = "'+book_name+'" or "'+domain'"
                      Connection conn = SqlConnection.getConnection();
                      preparedStatement psmt = conn.createStatement();
                      psmt.setString(1,request.getParameter("date");
                      psmt.setString(2,request.getParameter("date");
                      psmt.setString(3,request.getParameter("author");
                     psmt.setString(4,request.getParameter("bookName");
                     psmt.setString(5,request.getParameter("domain");

                      ResultSet rs = stmt.executeQuery(query);
                      while(rs.next()){
                          %>
                          <tr>
                          <td><input type = text readonly value ="<%=rs.getString("Id")%>"> </td>
                          <td><input type = text readonly value ="<%=rs.getString("bookName")%>"> </td>
                          <td><input type = text readonly value ="<%=rs.getString("author")%>"></td>
                          <td><input class ="btn"type = text readonly value ="<%=rs.getString("domain")%>"title="<%=rs.getString("domain")%>"> </td>
                         
                          
                          </tr>
                          <%
                          }
                    }
                    catch(Exception ex){
                        System.out.println(ex);
                    }
           %>
    </thead>
</table>



</div>

</body>
</html>
0 Answers
Related