0

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.

  • 1
    Add `listAllUser.add(u);` at the end of the `while` cycle in the `UserDao.userList()` method - you are creating the User objects, but you forgot to add them into the list! ;-) – Jozef Chocholacek Jan 22 '20 at 12:26
  • @jozef thanks for you helpful answer , but again i am not able to list records..its giving me empty jsp page – Ajay Kumar Sinha Jan 22 '20 at 12:30
  • Interesting... how do you access the page? Do you submit a form in a previous page? Because your servlet implements `doPost` method, which is used to serve the POST request, i.e. a form submission usually. So if you call the servlet by simply typing something like `http://localhost:8080/yourAppName/ListServlet` into the browser address field, you are using the GET request. Try to change `doPost` to `doGet` then. – Jozef Chocholacek Jan 22 '20 at 12:38
  • @Jozef thanks for the quick reply .. but problem remains :( – Ajay Kumar Sinha Jan 22 '20 at 12:41
  • Do you see at least the header of the table? Is there any error in the log / console file? – Jozef Chocholacek Jan 22 '20 at 12:45
  • Ya only headers but not records..also no error in log – Ajay Kumar Sinha Jan 22 '20 at 12:47
  • Well, then the only way is to debug it - either using your IDE, or add few logging outputs, e.g. `System.out.println("List size: " + userList.size());` before the `req.setAttribute("userList",userList);` line (to see, if anything was loaded). – Jozef Chocholacek Jan 22 '20 at 12:53
  • @Jozef thanks for reply , i am using IntelliJ IDEA , ok i will use debugger to check – Ajay Kumar Sinha Jan 22 '20 at 12:56

0 Answers0