user3649899
user3649899

Reputation: 157

Display variable result from servlet to jsp arraylist

i am trying to display my data from mysql into the jsp through servlet problem i am facing are im not sure if the data is passing, i am getting the NullPointerException error. it doesnt even display any data taken from the mysql

what i am trying to do is to display all my data from my mysql id,name,price into the jsp in a format table

result.jsp

<%                           
        ArrayList<ProductBean> list=(ArrayList<ProductBean>)request.getAttribute("list");
        for (int i = 0; i < list.size(); i++) {
                list.get(i).getItemID();
                list.get(i).getName();
                list.get(i).getPrice();
            }
        %>

product.java

  connection = DriverManager.getConnection(connectionUrl + dbName, userId, password);
            statement = connection.createStatement();
            String sql = "SELECT id,name,price FROM item";

            ArrayList < ProductBean > list = new ArrayList < ProductBean > ();

            resultSet = statement.executeQuery(sql);
            while (resultSet.next()) {
                String itemID = resultSet.getString("id"); //fetch the values present in database
                String name = resultSet.getString("name");
                String price = resultSet.getString("price");
                list.add(new ProductBean(itemID, name, price));

            }
            request.setAttribute("list", list); 
            request.getRequestDispatcher("/result.jsp").forward(request, response);

ProductBean.java

public class ProductBean {

    private String itemID;
    private String name;
    private String price;

    public String getItemID() {
        return itemID;
    }
    public void setItemID(String itemID) {
        this.itemID = itemID;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getPrice() {
        return price;
    }
    public void setPrice(String price) {
        this.price = price; 
    }

    public ProductBean(){
    }

    public ProductBean(String itemID, String name, String price){
        this.itemID = itemID;
        this.name = name;
        this.price = price;
    }
}

Upvotes: 0

Views: 520

Answers (1)

Neha Shettar
Neha Shettar

Reputation: 723

To display use out.println() if you are not getting NULL pointer Exception

<%                           
        ArrayList<ProductBean> list=(ArrayList<ProductBean>)request.getAttribute("list");
        for (int i = 0; i < list.size(); i++) {
                 out.println(list.get(i).getItemID());
                 out.println(list.get(i).getName());
                 out.println(list.get(i).getPrice());
            }
        %>

Use can also use JSTL

<c:forEach var="i" items=${list}>
  <c:out value="${i.itemID}"/> 
  <c:out value="${i.name}"/> 
  <c:out value="${i.price}"/> 

</c:forEach>

Upvotes: 1

Related Questions