-2

this things is giving me headache for the past 3 days already. I tried to store the resultset that i get from the database and store inside an arraylist of object then pass it to the jsp for display, but after running the program, it gave me this error: enter image description here

anyone have any idea how to solve this problem? Thanks in advance! below is my code: menu.java

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse     response)
        throws ServletException, IOException {
    PrintWriter out = response.getWriter();
    try {
        Class.forName("com.mysql.jdbc.Driver");

        //Create connection object
        conn = DriverManager.getConnection(request.getServletContext().getInitParameter("url"), request.getServletContext().getInitParameter("username"), request.getServletContext().getInitParameter("password"));
        String sql = "select name,price,imageurl from food";
        stmt = conn.createStatement();
        rs = stmt.executeQuery(sql);
        class food {

            String name;
            double price;
            String imageurl;

        }
        ArrayList<food> foodDetail = new ArrayList<food>();

        while (rs.next()) {
            food temp = new food();

            temp.name = rs.getString("name");
            temp.price = rs.getDouble("price");
            temp.imageurl = rs.getString("imageurl");

            foodDetail.add(temp);

        }
        request.setAttribute("menu", foodDetail);
        RequestDispatcher req = request.getRequestDispatcher("menu.jsp");
        req.forward(request, response);

    } catch (ClassNotFoundException ex) {
        Logger.getLogger(Menu.class.getName()).log(Level.SEVERE, null, ex);
    } catch (SQLException ex) {
        Logger.getLogger(Menu.class.getName()).log(Level.SEVERE, null, ex);
    } finally {

        try {
            rs.close();
            stmt.close();
            conn.close();
        } catch (SQLException ex) {
        }

    }

menu.jsp

<%
        class food {

            String name;
            double price;
            String imageurl;
        }


        ArrayList<food> foodDetail = (ArrayList<food>) request.getAttribute("menu");
        food temp = new food();
java.util.Iterator it = foodDetail.iterator();
        while (it.hasNext()) {

            food z = (food) it.next();

            out.println(z.getName());


        }


    %>

2 Answers2

0

Try to remove this class food from jsp page:

  class food {

        String name;
        double price;
        String imageurl;
    }

And instead try to import class food like this <%@ page import="package.food " %> in your page jsp. And add a backslash to path:

   RequestDispatcher req = request.getRequestDispatcher("/menu.jsp");
Abdelhak
  • 8,299
  • 4
  • 22
  • 36
0

Your food class in servlet used in list ArrayList<food> foodDetail = new ArrayList<food>(); which is you trying to pass is trying to cast in org.apache.jsp_menu$1food which is defining in jsp itself. remove the declaration in jsp of class food and use <%@ page import="your.package.food" %> it should be same as what you used in servlet as I have commented.

Shekhar Khairnar
  • 2,643
  • 3
  • 26
  • 44