0

I am making a page where you are able to update,edit,delete,insert data in a database. The insert page works well, and the data is inserted successfully, but when i click the "Add" button i am not redirected to the AllGrocery page but i am given this error by Eclipse.Can't figure out where the problem is.Can anyone help me out? Database : GroceryStore, Table : grocerys(id,name,price)

DataAccess.java

package dao;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.LinkedList;
import java.util.List;

import com.sun.istack.internal.logging.Logger;

import db.DBUtils;
import model.Grocery;

public class DataAccess {

    public void addNew(Grocery g) throws ClassNotFoundException, SQLException{
        try {
        PreparedStatement ps = DBUtils.getPreparedStatement("insert into grocerys values(?,?,?)");
        ps.setInt(1, g.getID());
        ps.setString(2, g.getName());
        ps.setDouble(3, g.getPrice());

        ps.executeUpdate();

        } catch(ClassNotFoundException | SQLException ex){

        }
    }

    public static List<Grocery> getAll(){
        List<Grocery> gs = new LinkedList<>();

        try {
            ResultSet rs = DBUtils.getPreparedStatement("select * from grocerys").executeQuery();

            while (rs.next()){
                Grocery g = new Grocery();
                g.setID(rs.getInt(1));
                g.setName(rs.getString(2));
                g.setPrice(rs.getDouble(3));

                gs.add(g);
            }

        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return gs;
    }
}

DBUtils.java

package db;

import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.Connection;
import java.sql.SQLException;

public class DBUtils {

    public static PreparedStatement getPreparedStatement(String sql) throws ClassNotFoundException, SQLException{

        PreparedStatement ps = null;

        Class.forName("com.mysql.jdbc.Driver");

        String url = "jdbc:mysql://localhost:3306/GroceryStore";
        String user = "root";
        String pass = "adil";

        Connection conn = DriverManager.getConnection(url,user,pass);

        ps = conn.prepareStatement(sql);


        return ps;

    }

    public static void main(String[] args)throws ClassNotFoundException, SQLException {

        getPreparedStatement("select * from grocerys");

    }

}

Grocery.java

package model;

public class Grocery {

    private int id;
    private String name;
    private Double price;

    public void setID(int id_){
        id = id_;
    }

    public void setName(String name_){
        name = name_;
    }

    public void setPrice(Double price_){
        price = price_;
    }

    public int getID(){
        return id;
    }

    public String getName(){
        return name;
    }

    public Double getPrice(){
        return price;
    }

}

AllGrocery.java

package servlet;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import dao.DataAccess;

/**
 * Servlet implementation class AllGrocery
 */
@WebServlet("/AllGrocery")
public class AllGrocery extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public AllGrocery() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        request.setAttribute("AllGrocery", DataAccess.getAll());

        RequestDispatcher rd = request.getRequestDispatcher("AllGrocery.jsp");

        rd.forward(request, response);



    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
    }

}


        public Double getPrice(){
            return price;
        }

    }

AddNew.html

<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Add Grocery</title>
</head>
<body>

<h1>Add New Grocery</h1>
<form action = "ManagerAddNew.jsp" method = "post">
<table>

<tr>
<th>ID</th>
<td><input type = "number" name = "id" style = "width : 200px;"/></td>
</tr>

<tr>
<th>NAME</th>
<td><input type = "text" name = "name" style = "width : 200px;"/></td>
</tr>


<tr>
<th>PRICE</th>
<td><input type = "number" name = "price" style = "width : 200px;"/></td>
</tr>


<tr>
<th></th>
<td><input type = "submit" name = "submit" value = "ADD"/></td>
</tr>

</table>
</form>

</body>
</html>

AllGrocery.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>

    <%@ taglib prefix = "c" uri = "http://java.sun.com/jsp/jstl/core" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Grocerys Available</title>
</head>
<body>

<div syle = "width : 1200px; margin-left:auto;margin-right:auto;">

<table cellpadding = "10">

<tr>

<th>ID</th>
<th>Name</th>
<th>Price</th>

</tr>

<c:forEach items = "${AllGrocery}" var = "p">

<tr>
<td>${p.id}</td>
<td>${p.name}</td>
<td>${p.price}</td>
<td>

<a href = "edit?id = ${p.id}">Edit</a>
<a href = "delete?id = ${p.id}">Delete</a>

</td>
</tr>


</c:forEach>

</table>

</div>

</body>
</html>

ManagerAddNew.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>

    <%@ page import = "model.Grocery" %>
    <%@ page import = "dao.DataAccess" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>

<%

String name = request.getParameter("name");
int id = Integer.parseInt(request.getParameter("id"));
Double price = Double.parseDouble(request.getParameter("price"));

Grocery g = new Grocery();
g.setID(id);
g.setName(name);
g.setPrice(price);

DataAccess da = new DataAccess();

da.addNew(g);

response.sendRedirect("/SimpleServletProject/AllGrocery");

%>

</body>
</html>

Error :

org.apache.jasper.JasperException: An exception occurred processing JSP page /AllGrocery.jsp at line 29

26: <c:forEach items = "${AllGrocery}" var = "p">
27: 
28: <tr>
29: <td>${p.id}</td>
30: <td>${p.name}</td>
31: <td>${p.price}</td>
32: <td>


Stacktrace:
    org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:575)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:477)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:395)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:339)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    servlet.AllGrocery.doGet(AllGrocery.java:38)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:624)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)


root cause 

javax.el.PropertyNotFoundException: Property 'id' not found on type model.Grocery
    javax.el.BeanELResolver$BeanProperties.get(BeanELResolver.java:290)
    javax.el.BeanELResolver$BeanProperties.access$300(BeanELResolver.java:243)
    javax.el.BeanELResolver.property(BeanELResolver.java:377)
    javax.el.BeanELResolver.getValue(BeanELResolver.java:97)
    org.apache.jasper.el.JasperELResolver.getValue(JasperELResolver.java:104)
    org.apache.el.parser.AstValue.getValue(AstValue.java:184)
    org.apache.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:184)
    org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate(PageContextImpl.java:950)
    org.apache.jsp.AllGrocery_jsp._jspx_meth_c_005fout_005f0(AllGrocery_jsp.java:215)
    org.apache.jsp.AllGrocery_jsp._jspx_meth_c_005fforEach_005f0(AllGrocery_jsp.java:163)
    org.apache.jsp.AllGrocery_jsp._jspService(AllGrocery_jsp.java:113)
    org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:439)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:395)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:339)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    servlet.AllGrocery.doGet(AllGrocery.java:38)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:624)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
BalusC
  • 992,635
  • 352
  • 3,478
  • 3,452
Adil.R
  • 180
  • 1
  • 4
  • 17

2 Answers2

3

If you see the exception it is unable to find the property id in the Grocery object. Your getter method name for the id should be changed to getId() and it is not getID(). It should be getId() since when you camelCase you just change the first character of the variable to capital not the entire variable.

Aditya
  • 815
  • 2
  • 6
  • 17
1

Your java Bean Grocery is not as per Bean standards. JavaBeans Conventions

If you are using some IDE, you can always right click and Generate Getters and Setters. Most of the IDEs do provide the facility to generate.

Then few extra suggestions for the code.

<c:forEach items = "${AllGrocery}" var = "p">

should be changed to

<c:forEach items = "${requestScope.AllGrocery}" var = "p">

and

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

            request.setAttribute("AllGrocery", DataAccess.getAll());

            RequestDispatcher rd = getServletContext().getRequestDispatcher("/AllGrocery.jsp");

            rd.forward(request, response);
    }
java8.being
  • 464
  • 3
  • 10