目录
1、先在数据库中 添加数据
2、servlet层
2.1 在servlet中 找到上一模块“LoginServlet.java”,在里面添加代码
2.2 在servlet层 new SelectUserServlet类
2.3 在web.xml 中,新增配置
3、service 层
4、 dao层
4.1 UserDao
4.2 UserDaoImpl
5、 注意: 代码同“登录模块”原理:顺着写 倒着抛异常
1、先在数据库中 添加数据
2、servlet层
2.1 在servlet中 找到上一模块“LoginServlet.java”,在里面添加代码
2.2 在servlet层 new SelectUserServlet类
package com.by.servlet;import com.by.pojo.User;
import com.by.service.UserService;
import com.by.service.impl.UserServiceImpl;import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;public class SelectUserServlet extends HttpServlet {private UserService userService = new UserServiceImpl();@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {doPost(req, resp);}@Overrideprotected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {try {List<User> userList = userService.selectUser();req.setAttribute("userList",userList); // "userList",userList : key 和 valuereq.getRequestDispatcher("select_user.jsp").forward(req,resp);} catch (Exception e) {e.printStackTrace();}}
}
2.3 在web.xml 中,新增配置
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"version="4.0"><!--欢迎页面--><welcome-file-list><welcome-file>login.jsp</welcome-file></welcome-file-list><servlet><servlet-name>login</servlet-name><servlet-class>com.by.servlet.LoginServlet</servlet-class></servlet><servlet-mapping><servlet-name>login</servlet-name><url-pattern>/login</url-pattern></servlet-mapping><servlet><servlet-name>selectUser</servlet-name><servlet-class>com.by.servlet.SelectUserServlet</servlet-class></servlet><servlet-mapping><servlet-name>selectUser</servlet-name><url-pattern>/selectUser</url-pattern></servlet-mapping>
</web-app>
3、service 层
4、 dao层
4.1 UserDao
4.2 UserDaoImpl
package com.by.dao.impl;import com.by.dao.UserDao;
import com.by.pojo.User;
import com.by.util.JdbcUtils;import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;public class UserDaoImpl implements UserDao {@Overridepublic User login(String username, String password) throws Exception {String sql = "select * from user where username=? and password=?";ResultSet rs = JdbcUtils.query(sql, username, password);if(rs.next()){return new User(rs.getInt("id"),rs.getString("username"),rs.getString("password"),rs.getString("sex"),rs.getInt("age"),rs.getString("address"));}return null;}@Overridepublic List<User> selectUser() throws Exception {String sql = "select * from user";ResultSet rs = JdbcUtils.query(sql);List<User> userList = new ArrayList<>();while (rs.next()){User user = new User(rs.getInt("id"),rs.getString("username"),rs.getString("password"),rs.getString("sex"),rs.getInt("age"),rs.getString("address"));userList.add(user);}return userList;}
}