I am not able to get the records from my table to jsp page . Also i am able to save , but having problem while retrieving.
My UserDao for list
protected List<User> userList(){
List<User> listAllUser = new ArrayList<User>();
try {
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection(url,user,pass);
String query = "Select * from user";
PreparedStatement ps = con.prepareStatement(query);
ResultSet rs = ps.executeQuery();
while (rs.next()){
String name = rs.getString("name");
String email= rs.getString("email");
User u = new User();
u.setName(name);
u.setEmail(email);
}
}
catch (SQLException | ClassNotFoundException e){
e.printStackTrace();
}
return listAllUser;
}
my Servlet class
@WebServlet("/ListServlet")
public class ListServlet extends HttpServlet {
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
super.doPost(req, resp);
String name = req.getParameter("name");
UserDao ud = new UserDao();
List<User> userList = ud.userList();
req.setAttribute("userList",userList);
RequestDispatcher rd = req.getRequestDispatcher("UserList.jsp");
rd.forward(req,resp);
}
and this is my jsp page
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<html>
<head>
<title>List User</title>
</head>
<body>
<table>
<thead>
<tr>
<th>name</th>
<th>email</th>
</tr>
</thead>
<c:forEach var="user" items="${userList}">
<tr>
<td>
<c:out value="${user.name}"></c:out>
</td>
<td>
<c:out value="${user.name}"></c:out>
</td>
</tr>
</c:forEach>
</table>
</body>
</html>
also my user Class has fields and G/Ts and jars are placed properly. please help me up.