LXJ0906
LXJshu
2016-04-19 03:11

通过Servlet显示数据库中的数据,报404

  • servlet
 public class userListServlet {
    public void doGet(HttpServletRequest request,HttpServletResponse response)
    throws ServletException,IOException{
        Model model=new Model();
        List<User>list=model.userSelect();
        request.getSession().setAttribute("list", list);
        response.sendRedirect("listUsers1.jsp");
    }
}


Model中的userSelect()方法如下:
public List<User> userSelect(){
        List users=new ArrayList();
        try{
            conn=s.getConncetion();
            String sql="select * from user";
            stat=conn.createStatement();
            rs=stat.executeQuery(sql);
            User user;
            while(rs.next()){
                user=new User();
                user.setId(rs.getInt("id"));
                user.setName(rs.getString("name"));
                user.setPassword(rs.getString("password"));
                users.add(user);
            }
            s.closeAll(conn, stat, rs);
        }catch(SQLException e){
            e.printStackTrace();
        }
        return users;
    }

listUsers1.jsp页面如下:
<body>
    数据库中所有用户:
    <table border="1">
        <c:forEach items="${sessionScope.list }" var="user" varStatus="num">
        <tr>
            <td>${user.id }</td>
            <td>${user.name }</td>
            <td>${user.password }</td>
        </tr>
        </c:forEach>
    </table>
</body>


web.xml配置如下:

<servlet>
        <servlet-name>userListServlet</servlet-name>
        <servlet-class>control.userListServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>userListServlet</servlet-name>
        <url-pattern>/userListServlet</url-pattern>
    </servlet-mapping>
  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

5条回答

为你推荐