Servlet+jdbc完成CRUD操作
这个小练习是为了更好的掌握servlet与jdbc访问数据库,对用户信息进行CRUD操作,功能有:用户登录、退出、查询用户列表,新增用户信息、修改用户信息、根据id删除用户信息、根据用户名模糊查询用户信息,过滤没有登录的用户,使其转到登录页面,监听器对用户的行为进行监听(登录和退出),将其结果存入数据库中,并将其显示在前端页面中。其中用户的servlet进行了合并,将对用户进行操作的所有servlet都整合到了一个servlet中,这就要求路径必须正确!!!
/**@文件名: DbUtil.java
* @类功能说明: 数据库连接工具类
* @作者: LiYuHui
* @Email: 1327711913@qq.com
* @日期: 2020年6月14日下午2:33:03
* @修改说明:<br>
* <pre>
* <li>作者: LiYuHui</li>
* <li>日期: 2020年6月14日下午2:33:03</li>
* <li>内容: </li>
* </pre>
*/
public class DbUtil {
private static String jdbcDriver = "com.mysql.jdbc.Driver";
private static String jdbcUrl = "jdbc:mysql://localhost:3306/db_servlet?useUnicode=true&characterEncoding=utf-8";
private static String userName = "root";
private static String pwd = "123456";
/**
* @方法名: getConn
* @方法说明: 获取连接
* @作者: LiYuHui
* @邮箱:1327711913@qq.com
* @日期: 2020年6月14日下午2:41:34
* @return
* @return: Connection
*/
public static Connection getConn() {
Connection conn = null;
try {
Class.forName(jdbcDriver);
conn = DriverManager.getConnection(jdbcUrl,userName,pwd);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return conn;
}
public static void closed(PreparedStatement pstm,Connection conn) {
try {
if(pstm!=null) {
pstm.close();
if(conn!=null) {
conn.close();
}
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
/**@文件名: UserInfo.java
* @类功能说明: 用户信息的实体类
* @作者: LiYuHui
* @Email: 1327711913@qq.com
* @日期: 2020年6月14日下午2:45:00
* @修改说明:<br>
* <pre>
* <li>作者: LiYuHui</li>
* <li>日期: 2020年6月14日下午2:45:00</li>
* <li>内容: </li>
* </pre>
*/
public class UserInfo {
private Integer userId; //用户编号
private String userName; //用户名
private String userPwd; //用户密码
private String userEmail; //用户邮箱
private int userPhone; //用户手机号
public UserInfo() {
}
/**
* 用于登录的构造器
* @param userName
* @param userPwd
*/
public UserInfo(String userName, String userPwd) {
this.userName = userName;
this.userPwd = userPwd;
}
/**
* 用于新增的构造器
* @param userName
* @param userPwd
* @param userEmail
* @param userPhone
*/
public UserInfo(String userName, String userPwd, String userEmail, int userPhone) {
super();
this.userName = userName;
this.userPwd = userPwd;
this.userEmail = userEmail;
this.userPhone = userPhone;
}
/**
* 用于修改、查询的构造器
* @param userId
* @param userName
* @param userPwd
* @param userEmail
* @param userPhone
*/
public UserInfo(Integer userId, String userName, String userPwd, String userEmail, int userPhone) {
super();
this.userId = userId;
this.userName = userName;
this.userPwd = userPwd;
this.userEmail = userEmail;
this.userPhone = userPhone;
}
public Integer getUserId() {
return userId;
}
public void setUserId(Integer userId) {
this.userId = userId;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getUserPwd() {
return userPwd;
}
public void setUserPwd(String userPwd) {
this.userPwd = userPwd;
}
public String getUserEmail() {
return userEmail;
}
public void setUserEmail(String userEmail) {
this.userEmail = userEmail;
}
public int getUserPhone() {
return userPhone;
}
public void setUserPhone(int userPhone) {
this.userPhone = userPhone;
}
}
/**@文件名: LoggInfo.java
* @类功能说明: 监听登录信息
* @作者: LiYuHui
* @Email: 1327711913@qq.com
* @日期: 2020年6月12日下午5:39:14
* @修改说明:<br>
* <pre>
* <li>作者: LiYuHui</li>
* <li>日期: 2020年6月12日下午5:39:14</li>
* <li>内容: </li>
* </pre>
*/
public class LoggInfo {
private Integer logg_id; //日志id
private String logg_name; //用户名
private String logg_createTime; //创建时间
private String logg_content; //日志内容
public LoggInfo(Integer logg_id, String logg_name, String logg_createTime, String logg_content) {
this.logg_id = logg_id;
this.logg_name = logg_name;
this.logg_createTime = logg_createTime;
this.logg_content = logg_content;
}
public LoggInfo(String logg_name, String logg_createTime, String logg_content) {
this.logg_name = logg_name;
this.logg_createTime = logg_createTime;
this.logg_content = logg_content;
}
public LoggInfo() {
// TODO Auto-generated constructor stub
}
public Integer getLogg_id() {
return logg_id;
}
public void setLogg_id(Integer logg_id) {
this.logg_id = logg_id;
}
public String getLogg_name() {
return logg_name;
}
public void setLogg_name(String logg_name) {
this.logg_name = logg_name;
}
public String getLogg_createTime() {
return logg_createTime;
}
public void setLogg_createTime(String logg_createTime) {
this.logg_createTime = logg_createTime;
}
public String getLogg_content() {
return logg_content;
}
public void setLogg_content(String logg_content) {
this.logg_content = logg_content;
}
}
/**@文件名: UserInfoDao.java
* @类功能说明: 用户信息的Dao接口
* @作者: LiYuHui
* @Email: 1327711913@qq.com
* @日期: 2020年6月14日下午2:51:11
* @修改说明:<br>
* <pre>
* <li>作者: LiYuHui</li>
* <li>日期: 2020年6月14日下午2:51:11</li>
* <li>内容: </li>
* </pre>
*/
public interface UserInfoDao {
/**
* @方法名: login
* @方法说明: 用户登录的接口
* @作者: LiYuHui
* @邮箱:1327711913@qq.com
* @日期: 2020年6月14日下午2:53:16
* @param userInfo
* @return
* @return: UserInfo
*/
public UserInfo login(UserInfo userInfo);
/**
* @方法名: findAllUserInfo
* @方法说明: 查询用户列表的接口
* @作者: LiYuHui
* @邮箱:1327711913@qq.com
* @日期: 2020年6月14日下午2:55:01
* @return
* @return: List<UserInfo>
*/
public List<UserInfo> findAllUserInfo();
/**
* @方法名: addUserInfo
* @方法说明: 新增用户信息的接口
* @作者: LiYuHui
* @邮箱:1327711913@qq.com
* @日期: 2020年6月14日下午2:59:05
* @param userInfo
* @return
* @return: int
*/
public int addUserInfo(UserInfo userInfo);
/**
* @方法名: updateUserInfo
* @方法说明: 修改用户信息的接口
* @作者: LiYuHui
* @邮箱:1327711913@qq.com
* @日期: 2020年6月14日下午3:00:25
* @param userInfo
* @return
* @return: int
*/
public int updateUserInfo(UserInfo userInfo);
/**
* @方法名: deleteUserInfo
* @方法说明: 根据id删除用户信息的接口
* @作者: LiYuHui
* @邮箱:1327711913@qq.com
* @日期: 2020年6月14日下午3:01:35
* @param userId
* @return
* @return: int
*/
public int deleteUserInfo(int userId);
/**
* @方法名: findUserInfoByName
* @方法说明: 根据用户名模糊查询用户信息的接口
* @作者: LiYuHui
* @邮箱:1327711913@qq.com
* @日期: 2020年6月14日下午2:57:31
* @return
* @return: List<UserInfo>
*/
public List<UserInfo> findUserInfoByName(String userName);
}
========================================================================================
/**@文件名: UserInfoDaoImpl.java
* @类功能说明: 用户信息的Dao接口的实现类(处理用户信息)
* @作者: LiYuHui
* @Email: 1327711913@qq.com
* @日期: 2020年6月14日下午3:03:33
* @修改说明:<br>
* <pre>
* <li>作者: LiYuHui</li>
* <li>日期: 2020年6月14日下午3:03:33</li>
* <li>内容: </li>
* </pre>
*/
public class UserInfoDaoImpl implements UserInfoDao {
/**
* 用户登录
*/
@Override
public UserInfo login(UserInfo userInfo) {
String sql = "select user_id,user_name,user_pwd,user_email,user_phone from t_userInfo where user_name=? and user_pwd=?";
Connection conn = DbUtil.getConn();
PreparedStatement pstm = null;
ResultSet rs = null;
UserInfo resultUserInfo = null;
try {
//使用PreparedStatement操作类对数据库进行操作
pstm = conn.prepareStatement(sql);
//处理Sql中占位符(?)所占的内容
pstm.setString(1, userInfo.getUserName());
pstm.setString(2, userInfo.getUserPwd());
//用ResultSet接收查询的结果集
rs = pstm.executeQuery();
//判断结果是否存在
while(rs.next()) {
//将结果存入返回值
resultUserInfo = new UserInfo(rs.getInt("user_id"),rs.getString("user_name"),rs.getString("user_pwd"),rs.getString("user_email"),rs.getInt("user_phone"));
}
} catch (SQLException e) {
e.printStackTrace();
}finally {
DbUtil.closed(pstm, conn);
}
return resultUserInfo;
}
/**
* 查询用户列表
*/
@Override
public List<UserInfo> findAllUserInfo() {
String sql = "select user_id,user_name,user_pwd,user_email,user_phone from t_userInfo";
Connection conn = DbUtil.getConn();
PreparedStatement pstm = null;
ResultSet rs = null;
List<UserInfo> userList = new ArrayList<UserInfo>();
try {
//使用PreparedStatement操作类对数据库进行操作
pstm = conn.prepareStatement(sql);
//用ResultSet接收查询的结果集
rs = pstm.executeQuery();
//判断结果是否存在
while(rs.next()) {
//将结果存入对象
UserInfo userInfo = new UserInfo(rs.getInt("user_id"),rs.getString("user_name"),rs.getString("user_pwd"),rs.getString("user_email"),rs.getInt("user_phone"));
userList.add(userInfo);
}
} catch (SQLException e) {
e.printStackTrace();
}finally {
DbUtil.closed(pstm, conn);
}
return userList;
}
/**
* 新增用户信息
*/
@Override
public int addUserInfo(UserInfo userInfo) {
String sql = "insert into t_userInfo(user_name,user_pwd,user_email,user_phone) values(?,?,?,?)";
Connection conn = DbUtil.getConn();
PreparedStatement pstm = null;
int result = 0;
try {
//使用PreparedStatement操作类对数据库进行操作
pstm = conn.prepareStatement(sql);
//处理Sql中占位符(?)所占的内容
pstm.setString(1, userInfo.getUserName());
pstm.setString(2, userInfo.getUserPwd());
pstm.setString(3, userInfo.getUserEmail());
pstm.setInt(4, userInfo.getUserPhone());
//用result接收返回的结果
result = pstm.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}finally {
DbUtil.closed(pstm, conn);
}
return result;
}
/**
* 修改用户信息
*/
@Override
public int updateUserInfo(UserInfo userInfo) {
String sql = "update t_userInfo set user_name=?,user_pwd=?,user_email=?,user_phone=? where user_id=?";
Connection conn = DbUtil.getConn();
PreparedStatement pstm = null;
int result = 0;
try {
//使用PreparedStatement操作类对数据库进行操作
pstm = conn.prepareStatement(sql);
//处理Sql中占位符(?)所占的内容
pstm.setString(1, userInfo.getUserName());
pstm.setString(2, userInfo.getUserPwd());
pstm.setString(3, userInfo.getUserEmail());
pstm.setInt(4, userInfo.getUserPhone());
pstm.setInt(5, userInfo.getUserId());
//用result接收返回的结果
result = pstm.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}finally {
DbUtil.closed(pstm, conn);
}
return result;
}
/**
* 删除用户信息
*/
@Override
public int deleteUserInfo(int userId) {
String sql = "delete from t_userInfo where user_id=?";
Connection conn = DbUtil.getConn();
PreparedStatement pstm = null;
int result = 0;
try {
//使用PreparedStatement操作类对数据库进行操作
pstm = conn.prepareStatement(sql);
//处理Sql中占位符(?)所占的内容
pstm.setInt(1, userId);
//用result接收返回的结果
result = pstm.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}finally {
DbUtil.closed(pstm, conn);
}
return result;
}
/**
* 根据用户名模糊查询用户信息
*/
@Override
public List<UserInfo> findUserInfoByName(String userName) {
String sql = "select user_id,user_name,user_pwd,user_email,user_phone from t_userInfo where user_name like concat('%',?,'%')";
Connection conn = DbUtil.getConn();
PreparedStatement pstm = null;
ResultSet rs = null;
List<UserInfo> userList = new ArrayList<UserInfo>();
try {
//使用PreparedStatement操作类对数据库进行操作
pstm = conn.prepareStatement(sql);
//处理Sql中占位符(?)所占的内容
pstm.setString(1, userName);
//用ResultSet接收查询的结果集
rs = pstm.executeQuery();
//判断结果是否存在
while(rs.next()) {
//将结果存入对象
UserInfo userInfo = new UserInfo(rs.getInt("user_id"),rs.getString("user_name"),rs.getString("user_pwd"),rs.getString("user_email"),rs.getInt("user_phone"));
userList.add(userInfo);
}
} catch (SQLException e) {
e.printStackTrace();
}finally {
DbUtil.closed(pstm, conn);
}
return userList;
}
}
/**@文件名: LoggInfoDao.java
* @类功能说明:
* @作者: LiYuHui
* @Email: 1327711913@qq.com
* @日期: 2020年6月12日下午5:47:30
* @修改说明:<br>
* <pre>
* <li>作者: LiYuHui</li>
* <li>日期: 2020年6月12日下午5:47:30</li>
* <li>内容: </li>
* </pre>
*/
public interface LoggInfoDao {
/**
* @方法名: addLoggInfo
* @方法说明: 新增日志数据
* @作者: LiYuHui
* @邮箱:1327711913@qq.com
* @日期: 2020年6月12日下午5:48:48
* @param loggInfo
* @return
* @return: int
*/
public int addLoggInfo(LoggInfo loggInfo);
/**
* @方法名: findAllLoggInfo
* @方法说明: 查询日志列表
* @作者: LiYuHui
* @邮箱:1327711913@qq.com
* @日期: 2020年6月15日上午2:21:32
* @return
* @return: List<LoggInfo>
*/
public List<LoggInfo> findAllLoggInfo();
}
=======================================================================================
/**@文件名: LoggInfoDaoImpl.java
* @类功能说明:
* @作者: LiYuHui
* @Email: 1327711913@qq.com
* @日期: 2020年6月12日下午5:51:00
* @修改说明:<br>
* <pre>
* <li>作者: LiYuHui</li>
* <li>日期: 2020年6月12日下午5:51:00</li>
* <li>内容: </li>
* </pre>
*/
public class LoggInfoDaoImpl implements LoggInfoDao {
/**
* 新增日志数据
*/
@Override
public int addLoggInfo(LoggInfo loggInfo) {
String sql = "insert into t_logg(logg_name,logg_createTime,logg_content) values(?,?,?)";
Connection conn = DbUtil.getConn();
PreparedStatement ps = null;
int result = 0;
try {
ps=conn.prepareStatement(sql);
ps.setString(1, loggInfo.getLogg_name());
ps.setString(2, loggInfo.getLogg_createTime());
ps.setString(3, loggInfo.getLogg_content());
result=ps.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
return result;
}
/**
* 查询日志列表
*/
@Override
public List<LoggInfo> findAllLoggInfo() {
String sql = "select logg_id,logg_name,logg_createTime,logg_content from t_logg";
Connection conn = DbUtil.getConn();
PreparedStatement pstm = null;
ResultSet rs = null;
List<LoggInfo> loggList = new ArrayList<LoggInfo>();
try {
//使用PreparedStatement操作类对数据库进行操作
pstm = conn.prepareStatement(sql);
//用ResultSet接收查询的结果集
rs = pstm.executeQuery();
//判断结果是否存在
while(rs.next()) {
//将结果存入对象
LoggInfo loggInfo = new LoggInfo(rs.getInt("logg_id"),rs.getString("logg_name"),rs.getString("logg_createTime"),rs.getString("logg_content"));
loggList.add(loggInfo);
}
} catch (SQLException e) {
e.printStackTrace();
}finally {
DbUtil.closed(pstm, conn);
}
return loggList;
}
}
/**@文件名: UserInfoServlet.java
* @类功能说明: 用户信息的处理类
* @作者: LiYuHui
* @Email: 1327711913@qq.com
* @日期: 2020年6月14日下午3:20:27
* @修改说明:<br>
* <pre>
* <li>作者: LiYuHui</li>
* <li>日期: 2020年6月14日下午3:20:27</li>
* <li>内容: </li>
* </pre>
*/
public class UserInfoServlet extends HttpServlet{
/**
*
*/
private static final long serialVersionUID = 1L;
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request, response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//解决中文乱码问题
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
response.setContentType("text/html; charset=UTF-8");
//获取前端页面传递的值
String action = request.getParameter("action");
if("login".equals(action)) {
this.login(request, response);
}else if ("logout".equals(action)) {
this.logout(request, response);
}else if ("findAll".equals(action)) {
this.findAll(request, response);
}else if("add".equals(action)) {
this.addUserInfo(request, response);
}else if("toUpdateJsp".equals(action)) {
this.toUpdateJsp(request, response);
}else if("update".equals(action)) {
this.updateUserInfo(request, response);
}else if("delete".equals(action)) {
this.deleteUserInfo(request, response);
}else if("searchUserInfoByName".equals(action)) {
this.searchUserInfoByName(request, response);
}
}
/**
* @方法名: login
* @方法说明: 用户登录
* @作者: LiYuHui
* @邮箱:1327711913@qq.com
* @日期: 2020年6月14日下午3:27:21
* @param request
* @param response
* @throws ServletException
* @throws IOException
* @return: void
*/
protected void login(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//获取前端页面传递的值
String userName = request.getParameter("userName");
String pwd = request.getParameter("pwd");
String remember = request.getParameter("remember");
//将获取到的值存入对象中
UserInfo userInfo = new UserInfo(userName,pwd);
//调用dao的方法
UserInfoDao userInfoDao = new UserInfoDaoImpl();
UserInfo resultUserInfo = userInfoDao.login(userInfo);
//返回值是否为空
if(resultUserInfo != null) {
//存入session
request.getSession().setAttribute("currentUserInfo", resultUserInfo);
//跳转至主页面
response.sendRedirect("layout.jsp");
}else {
//返回错误信息
request.setAttribute("msg", "*用户名或密码输入有误!");
request.setAttribute("userName", userName);
request.setAttribute("pwd", pwd);
//返回至登录页面(带错误信息)
request.getRequestDispatcher("login.jsp").forward(request, response);
}
}
/**
* @方法名: logout
* @方法说明: 退出系统
* @作者: LiYuHui
* @邮箱:1327711913@qq.com
* @日期: 2020年6月14日下午9:03:28
* @param request
* @param response
* @throws ServletException
* @throws IOException
* @return: void
*/
protected void logout(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.getSession().removeAttribute("currentUserInfo");
response.sendRedirect("login.jsp");
}
/**
* @方法名: findAll
* @方法说明: 查询用户列表
* @作者: LiYuHui
* @邮箱:1327711913@qq.com
* @日期: 2020年6月14日下午10:25:47
* @param request
* @param response
* @throws ServletException
* @throws IOException
* @return: void
*/
protected void findAll(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//调用dao方法
UserInfoDao userInfoDao = new UserInfoDaoImpl();
List<UserInfo> userList = userInfoDao.findAllUserInfo();
//将集合存入作用域中
request.setAttribute("userList", userList);
//带着结果跳转到主页面
request.getRequestDispatcher("userInfo/userList.jsp").forward(request, response);
}
/**
* @方法名: addUserInfo
* @方法说明: 添加用户信息
* @作者: LiYuHui
* @邮箱:1327711913@qq.com
* @日期: 2020年6月14日下午11:49:18
* @param request
* @param response
* @throws ServletException
* @throws IOException
* @return: void
*/
protected void addUserInfo(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//获取前端页面传递的值
String userName = request.getParameter("userName");
String userPwd = request.getParameter("userPwd");
String userEmail = request.getParameter("userEmail");
int userPhone = Integer.parseInt(request.getParameter("userPhone"));
//将值存入UserInfo中
UserInfo userInfo = new UserInfo(userName,userPwd,userEmail,userPhone);
//调用dao方法
UserInfoDao userInfoDao = new UserInfoDaoImpl();
int result = userInfoDao.addUserInfo(userInfo);
//判断结果是否有内容
if(result>0) {
//带结果跳转到列表页面
request.getRequestDispatcher("userInfo?action=findAll").forward(request, response);
}
}
/**
* @方法名: toUpdateJsp
* @方法说明: 将数据回显至修改页面(仅显示用户信息,不进行修改)
* @作者: LiYuHui
* @邮箱:1327711913@qq.com
* @日期: 2020年6月15日上午12:37:36
* @param request
* @param response
* @throws ServletException
* @throws IOException
* @return: void
*/
protected void toUpdateJsp(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//获取前端页面传递的值
String userName = request.getParameter("userName");
String userPwd = request.getParameter("userPwd");
String userEmail = request.getParameter("userEmail");
int userPhone = Integer.parseInt(request.getParameter("userPhone"));
int userId = Integer.parseInt(request.getParameter("userId"));
//将值存入UserInfo中
UserInfo userInfo = new UserInfo(userId,userName,userPwd,userEmail,userPhone);
//将userInfo存入作用域中
request.setAttribute("userInfo", userInfo);
//带着存入的值跳转至修改页面
request.getRequestDispatcher("userInfo/updateUserInfo.jsp").forward(request, response);
}
/**
* @方法名: updateUserInfo
* @方法说明: 修改用户信息(真实的修改)
* @作者: LiYuHui
* @邮箱:1327711913@qq.com
* @日期: 2020年6月15日上午12:46:23
* @param request
* @param response
* @throws ServletException
* @throws IOException
* @return: void
*/
protected void updateUserInfo(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//获取前端页面传递的值
String userName = request.getParameter("userName");
String userPwd = request.getParameter("userPwd");
String userEmail = request.getParameter("userEmail");
int userPhone = Integer.parseInt(request.getParameter("userPhone"));
int userId = Integer.parseInt(request.getParameter("userId"));
//将值存入UserInfo中
UserInfo userInfo = new UserInfo(userId,userName,userPwd,userEmail,userPhone);
//调用dao方法
UserInfoDao userInfoDao = new UserInfoDaoImpl();
int result = userInfoDao.updateUserInfo(userInfo);
//判断结果是否有内容
if(result>0) {
//带结果跳转到列表页面
request.getRequestDispatcher("userInfo?action=findAll").forward(request, response);
}
}
/**
* @方法名: deleteUserInfo
* @方法说明: 根据id删除用户信息
* @作者: LiYuHui
* @邮箱:1327711913@qq.com
* @日期: 2020年6月15日上午1:11:14
* @param request
* @param response
* @throws ServletException
* @throws IOException
* @return: void
*/
protected void deleteUserInfo(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//获取前端页面传递的值
int userId = Integer.parseInt(request.getParameter("userId"));
//调用dao方法
UserInfoDao userInfoDao = new UserInfoDaoImpl();
int result = userInfoDao.deleteUserInfo(userId);
//判断结果是否有内容
if(result>0) {
//带结果跳转到列表页面
request.getRequestDispatcher("userInfo?action=findAll").forward(request, response);
}
}
/**
* @方法名: searchUserInfoByName
* @方法说明: 根据用户名进行模糊查询
* @作者: LiYuHui
* @邮箱:1327711913@qq.com
* @日期: 2020年6月15日上午2:12:42
* @param request
* @param response
* @throws ServletException
* @throws IOException
* @return: void
*/
protected void searchUserInfoByName(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//获取前端页面传递的值
String userName = request.getParameter("userName");
//调用dao方法
UserInfoDao userInfoDao = new UserInfoDaoImpl();
List<UserInfo> userList = userInfoDao.findUserInfoByName(userName);
//将集合存入作用域中
request.setAttribute("userList", userList);
//带着结果跳转到主页面
request.getRequestDispatcher("userInfo/userList.jsp").forward(request, response);
}
}
/**@文件名: LoggInfoServlet.java
* @类功能说明: 日志信息的处理类
* @作者: LiYuHui
* @Email: 1327711913@qq.com
* @日期: 2020年6月15日上午2:28:40
* @修改说明:<br>
* <pre>
* <li>作者: LiYuHui</li>
* <li>日期: 2020年6月15日上午2:28:40</li>
* <li>内容: </li>
* </pre>
*/
public class LoggInfoServlet extends HttpServlet{
/**
*
*/
private static final long serialVersionUID = 1L;
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request, response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//解决中文乱码问题
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
response.setContentType("text/html; charset=UTF-8");
//调用dao方法
LoggInfoDao loggInfoDao = new LoggInfoDaoImpl();
List<LoggInfo> loggList = loggInfoDao.findAllLoggInfo();
//将集合存入作用域中
request.setAttribute("loggList", loggList);
//带着结果跳转到主页面
request.getRequestDispatcher("logInfo/logList.jsp").forward(request, response);
}
}
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>登录界面</title>
<link rel="stylesheet" href="css/login.css" />
<script type="text/javascript" src="js/jquery-3.4.1.min.js" ></script>
<script type="text/javascript" src="js/login.js" ></script>
</head>
<body>
<div class="content">
<form action="userInfo?action=login" method="post">
<table>
<tr>
<td>
<span class="contentSpan">快来登录吧!</span>
</td>
</tr>
<tr>
<td>
<input type="text" id="userName" class="userName" onchange="onchangeEvent1()" onm ouseover="onmouseoverEvent1()" onm ouseout="onmouseoutEvent1()" placeholder="用户名" name="userName" value="${userName }"/>
</td>
</tr>
<tr>
<td>
<label id="userNameLable" class="lable"></label>
</td>
</tr>
<tr>
<td>
<input type="password" class="password" id="password" onchange="onchangeEvent2()" onm ouseover="onmouseoverEvent2()" onm ouseout="onmouseoutEvent2()" placeholder="密码" name="pwd" value="${pwd }"/>
</td>
</tr>
<tr>
<td>
<label id="passwordLable" class="lable"></label>
</td>
</tr>
<tr>
<td>
<input class="checkbox" name="remember" value="rememberMe" type="checkbox" checked="checked" />记住密码
</td>
</tr>
<tr>
<td>
<span style="color:red;">${msg }</span>
</td>
</tr>
<tr>
<td>
<input class="tdButton" type="submit" id="submit" value="立即登录"/>
<a href="register.jsp"><input class="tdButton" type="button" id="submit" value="新用户注册"/></a>
</td>
</tr>
</table>
</form>
</div>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>页面布局</title>
</head>
<frameset rows="20%,*">
<frame src="${pageContext.request.contextPath }/userInfo/top.jsp"/>
<frameset cols="20%,*">
<frame src="${pageContext.request.contextPath }/userInfo/left.jsp"/>
<frame name="main"/>
</frameset>
</frameset>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>top.jsp</title>
</head>
<body>
<h1 style="text-align: center;">欢迎【<span style="color:red;">${currentUserInfo.userName }</span>】登录本系统</h1>
<span style="margin-left: 44%;">==><a href="${pageContext.request.contextPath}/userInfo?action=logout" target="main.jsp"><button style="width: 100px;background-color: yellow;border-radius: 10px;">退出系统</button></a><==</span>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>left.jsp</title>
</head>
<body>
<ul>
<li>用户管理
<ul>
<li><a href="${pageContext.request.contextPath }/userInfo?action=findAll" style="text-decoration: none" target="main">用户列表</a></li>
</ul>
</li>
</ul>
<ul>
<li>日志管理
<ul>
<li><a href="${pageContext.request.contextPath }/findAllLoggInfo" style="text-decoration: none" target="main">日志列表</a></li>
</ul>
</li>
</ul>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>用户列表</title>
</head>
<body>
<table border="1" collspacing="0" collpadding="0" align="center">
<tr>
<th colspan="6">用户信息</th>
</tr>
<tr>
<td colspan="6">
<a href="userInfo/addUserInfo.jsp" style="text-decoration: none" target="main">新增用户</a>
<input type="text" name="userName" id="userName"/>
<!-- <a href="userInfo?action=searchUserInfoByName" target="main"><button>查询</button></a> -->
<a href="javascript:searchUserInfoByName();"><button>查询</button></a>
</td>
</tr>
<tr>
<th>编号</th>
<th>用户名</th>
<th>密码</th>
<th>邮箱</th>
<th>手机号</th>
<th>操作</th>
</tr>
<c:forEach items="${userList }" var="userInfo">
<tr align="center">
<td>${userInfo.userId }</td>
<td>${userInfo.userName }</td>
<td>${userInfo.userPwd }</td>
<td>${userInfo.userEmail }</td>
<td>${userInfo.userPhone }</td>
<td>
<!-- 跳转至修改用户的jsp页面(只显示信息,不修改) -->
<a href="userInfo?action=toUpdateJsp&userId=${userInfo.userId }&userName=${userInfo.userName }&userPwd=${userInfo.userPwd }&userEmail=${userInfo.userEmail }&userPhone=${userInfo.userPhone }" target="main"><button>修改</button></a>
<a href="userInfo?action=delete&userId=${userInfo.userId }" target="main"><button>删除</button></a>
</td>
</tr>
</c:forEach>
</table>
<script type="text/javascript">
function searchUserInfoByName(){
var userName = document.getElementById("userName").value;
window.location.href="${pageContext.request.contextPath }/userInfo?action=searchUserInfoByName&userName="+userName;
/* alert(123);
alert("${pageContext.request.contextPath }/userInfo?action=searchUserInfoByName&userName="+userName); */
}
</script>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>添加用户</title>
</head>
<body>
<form action="${pageContext.request.contextPath }/userInfo?action=add" method="post">
<table style="text-align: center;margin: auto;">
<tr>
<td>
<input type="text" id="userName" name="userName" placeholder="用户名" style="border-radius: 4px"/>
</td>
</tr>
<tr>
<td>
<input type="password" name="userPwd" placeholder="密码" style="border-radius: 4px"/>
</td>
</tr>
<tr>
<td>
<input type="text" name="userEmail" id="email" placeholder="邮箱" style="border-radius: 4px"/>
</td>
</tr>
<tr>
<td>
<input type="text" name="userPhone" id="phone" placeholder="手机号" style="border-radius: 4px"/>
</td>
</tr>
<tr>
<td>
<input type="submit" id="submit" value="添加" style="border-radius: 10px"/>
<input class="tdButton" type="reset" id="reset" value="取消" style="border-radius: 10px"/>
</td>
</tr>
</table>
</form>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>修改用户</title>
</head>
<body>
<!-- 表单提交,修改信息 -->
<form action="${pageContext.request.contextPath }/userInfo?action=update" method="post">
<table style="text-align: center;margin: auto;">
<input type="hidden" name="userId" value="${userInfo.userId }"/>
<tr>
<td>
<input type="text" id="userName" name="userName" placeholder="用户名" value="${userInfo.userName }" style="border-radius: 4px"/>
</td>
</tr>
<tr>
<td>
<input type="password" name="userPwd" placeholder="密码" value="${userInfo.userPwd }" style="border-radius: 4px"/>
</td>
</tr>
<tr>
<td>
<input type="text" name="userEmail" id="email" placeholder="邮箱" value="${userInfo.userEmail }" style="border-radius: 4px"/>
</td>
</tr>
<tr>
<td>
<input type="text" name="userPhone" id="phone" placeholder="手机号" value="${userInfo.userPhone }" style="border-radius: 4px"/>
</td>
</tr>
<tr>
<td>
<input type="submit" id="submit" value="修改" style="border-radius: 10px"/>
<input class="tdButton" type="reset" id="reset" value="取消" style="border-radius: 10px"/>
</td>
</tr>
</table>
</form>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>日志列表</title>
</head>
<body>
<table border="1" collspacing="0" collpadding="0" align="center">
<tr>
<th colspan="4">日志信息</th>
</tr>
<tr>
<th>访问编号</th>
<th>访问用户名</th>
<th>创建时间</th>
<th>日志内容</th>
</tr>
<c:forEach items="${loggList }" var="loggInfo">
<tr align="center">
<td>${loggInfo.logg_id }</td>
<td>${loggInfo.logg_name }</td>
<td>${loggInfo.logg_createTime }</td>
<td>${loggInfo.logg_content }</td>
</tr>
</c:forEach>
</table>
</body>
</html>
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>Servlet_JDBC_UserCRUD</display-name>
<welcome-file-list>
<welcome-file>login.jsp</welcome-file>
</welcome-file-list>
<!-- 用户信息的servlet -->
<servlet>
<servlet-name>UserInfo</servlet-name>
<servlet-class>com.lyh.servlet.UserInfoServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>UserInfo</servlet-name>
<url-pattern>/userInfo</url-pattern>
</servlet-mapping>
<!-- 日志信息的servlet -->
<servlet>
<servlet-name>loggInfo</servlet-name>
<servlet-class>com.lyh.servlet.LoggInfoServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>loggInfo</servlet-name>
<url-pattern>/findAllLoggInfo</url-pattern>
</servlet-mapping>
<!-- 过滤器 -->
<filter>
<filter-name>UserInfoFilter</filter-name>
<filter-class>com.lyh.filter.UserInfoFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>UserInfoFilter</filter-name>
<url-pattern>/layout.jsp</url-pattern>
</filter-mapping>
<listener>
<listener-class>com.lyh.listener.SessionListener</listener-class>
</listener>
</web-app>
/**@文件名: UserInfoFilter.java
* @类功能说明: Servlet过滤器
* @作者: LiYuHui
* @Email: 1327711913@qq.com
* @日期: 2020年6月12日下午1:36:16
* @修改说明:<br>
* <pre>
* <li>作者: LiYuHui</li>
* <li>日期: 2020年6月12日下午1:36:16</li>
* <li>内容: </li>
* </pre>
*/
public class UserInfoFilter implements Filter {
@Override
public void destroy() {
}
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain)
throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest)servletRequest;//将servletRequest转换为HttpServletRequest
HttpSession session = request.getSession(); //获取session
Object ob = session.getAttribute("currentUserInfo"); //获取session作用域存的值
String path = request.getServletPath(); //获取用户请求的路径
if(ob==null && path.indexOf("login")<0) { //用户没有登录(直接访问其他页面)
request.getRequestDispatcher("login.jsp").forward(servletRequest, servletResponse);
}else {
filterChain.doFilter(servletRequest, servletResponse);
}
}
@Override
public void init(FilterConfig filterConfig) throws ServletException {
}
}
/**@文件名: SessionListener.java
* @类功能说明: 会话的监听器(域对象中属性的创建、替换和消除事件监听器)
* @作者: LiYuHui
* @Email: 1327711913@qq.com
* @日期: 2020年6月12日下午2:07:31
* @修改说明:<br>
* <pre>
* <li>作者: LiYuHui</li>
* <li>日期: 2020年6月12日下午2:07:31</li>
* <li>内容: </li>
* </pre>
*/
public class SessionListener implements HttpSessionAttributeListener {
@Override
public void attributeAdded(HttpSessionBindingEvent event) {
UserInfo userInfo = (UserInfo)event.getValue();
LoggInfo loggInfo = new LoggInfo();
LoggInfoDao loggInfoDao = new LoggInfoDaoImpl();
loggInfo.setLogg_name(userInfo.getUserName());
Date date = new Date();
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置格式
String createDate = simpleDateFormat.format(date);
loggInfo.setLogg_createTime(createDate);
loggInfo.setLogg_content("登录了本系统");
loggInfoDao.addLoggInfo(loggInfo);
}
@Override
public void attributeRemoved(HttpSessionBindingEvent event) {
UserInfo userInfo = (UserInfo)event.getValue();
LoggInfo loggInfo = new LoggInfo();
LoggInfoDao loggInfoDao = new LoggInfoDaoImpl();
loggInfo.setLogg_name(userInfo.getUserName());
Date date = new Date();
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置格式
String createDate = simpleDateFormat.format(date);
loggInfo.setLogg_createTime(createDate);
loggInfo.setLogg_content("退出了本系统");
loggInfoDao.addLoggInfo(loggInfo);
}
@Override
public void attributeReplaced(HttpSessionBindingEvent event) {
}
}
本来页面是用jsp写的,但是这个编辑器好像不识别jsp,全部是灰的,就改成html的吧