1

I written the code for array list and its display the database values in JSP page. But it's display only table heading values from JSP page. I didn't know where i done the mistake. anyone help me

This is my Servlet code

protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
        PrintWriter out=response.getWriter();
            try
            {
                String date1=request.getParameter("startdte");
                String date2=request.getParameter("enddte"); 
                SimpleDateFormat df = new SimpleDateFormat("MM/dd/yyyy");
                Date date3 = df.parse(date1);
                SimpleDateFormat format1 = new SimpleDateFormat("MM/dd/yyyy");
                Date date4 = format1.parse(date2);
                java.sql.Date sqdate1 = new java.sql.Date(date3.getTime());
                java.sql.Date sqdate2 = new java.sql.Date(date4.getTime());
                Connection conn=null;
                PreparedStatement pst=null;
                ResultSet rst=null;
                ArrayList<Product> listProducts = new ArrayList<Product>();
                conn=mysqlconnect.ConnecrDb();
                String sql="select bill_master.bill_no,DATE_FORMAT(bill_master.bill_date, '%Y/%m/%d') AS 'formatted_date',transaction.product_id,transaction.tax_amount,transaction.amount,transaction.amount-transaction.tax_amount as 'without_tax ',product_master.Product_name,product_master.vat from bill_master inner join transaction on bill_master.bill_no=transaction.bill_no inner join product_master on transaction.product_id=product_master.product_id where vat='14.50' and DATE_FORMAT(bill_master.bill_date, '%Y/%m/%d')between ? and ?";
                pst= conn.prepareStatement(sql);
                pst.setDate(1, sqdate1);
                pst.setDate(2, sqdate2);
                rst= pst.executeQuery();          
                while(rst.next())
                {
                    Product product=new Product();
                    product.Bill_no= rst.getInt("bill_no");
                    product.Formatted_date=rst.getDate("formatted_date");
                    product.Product_id=rst.getInt("product_id");
                    product.Tax_amount=rst.getDouble("tax_amount");
                    product.Amount=rst.getDouble("amount");
                    product.Without_tax=rst.getDouble("without_tax"); 
                    product.Product_name=rst.getString("product_name");
                    product.Vat=rst.getDouble("vat");
                    listProducts.add(product);

                }
                    request.setAttribute("listProducts",listProducts);
                    conn.close();
                    RequestDispatcher rd=request.getRequestDispatcher("/report1.jsp");
                    rd.forward(request,response);
            }
           catch(Exception e)
           {
              e.printStackTrace();
           }
    }

This is my Product class code

package report;
import java.sql.Date;
public class Product {
 public int Bill_no;
   public  Date Formatted_date;
   public int Product_id;
   public double Tax_amount;
   public double Amount;
   public double Without_tax;
   public String Product_name;
   public double Vat;   
}

This is my report1 JSP Page code

<%@ page import="java.util.*"%>
<%@ page import="report.Product"%>
<%@ page import="javax.servlet.http.*,javax.servlet.*" %>
<%@page language="java" contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

    </head>
    <body>          

          <div align="center">
        <table border="1" cellpadding="5">/
            <caption><h2>List of products</h2></caption>
            <tr>
                <th>Bill NO</th>
                <th>Product Id</th>
                <th>Date</th>
                <th>Tax</th>
                <th>Without TAX</th>
                <th>Including TAX</th>
                <th>Product Name</th>
                <th>VAT</th>
            </tr>
             <%
                 ArrayList<Product> dproducts=(ArrayList)request.getAttribute("listProducts");
                   Iterator it=dproducts.iterator();
                  while(it.hasNext())
                 {
                  Product product=(Product)it.next();
           %>

             <tr>
                  <td><%=product.Bill_no %></td>
                 <td><%=product.Formatted_date %></td> 
                 <td><%=product.Product_id %></td>
                 <td><%=product.Tax_amount %> </td>
                 <td><%=product.Without_tax %> </td>
                 <td><%=product.Amount %> </td>
                 <td><%=product.Product_name %> </td>
                 <td><%=product.Vat %> </td>
            </tr>              
            <%
                 }
             %>
        </table>           
    </div>  
  </body>
</html>
Ram Eswar
  • 19
  • 2
  • 2
    Are you sure that your list is actually getting populated, which would only happen if the query returns a non-empty result set? – Tim Biegeleisen Jul 28 '16 at 07:06
  • try learning to use JSTL as well. – Scary Wombat Jul 28 '16 at 07:08
  • I see that you catch all kinds of exceptions with a simple stackstrace print. What I think is happening, is that probably there happens some exception during the query, but that does not break the application since everything is written to debug. If I was you, I would check the console. Probably you have a some stack trace printed there which helps you determine the cause of the problem. – ThomasMX Jul 28 '16 at 07:14
  • To sum up above comments: look into your log files (or console), if there are any error messages (exception stack traces). – Jozef Chocholacek Jul 28 '16 at 07:59
  • Not sure but i was wondering if => conn.close(); <= is defined at the appropriate location. – AchillesVan Jul 28 '16 at 09:03
  • @Georgesvanhoutte Surely one could put it to a better place (e.g. `finally` block), but I am 99% sure that's not the cause of the problem. – Jozef Chocholacek Jul 28 '16 at 09:15
  • when i run log file shows 29-Jul-2016 10:25:53.948 INFO [localhost-startStop-1] org.apache.catalina.core.ApplicationContext.log No Spring WebApplicationInitializer types detected on classpath 29-Jul-2016 10:25:54.004 – Ram Eswar Jul 29 '16 at 05:37

0 Answers0