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());
}
%>