package UserServlet; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import dao.impl.UserDAOImpl; import domain.User; //登录功能 @WebServlet("/login") public class LoginServlet extends HttpServlet{ @Override protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { req.setCharacterEncoding("utf-8"); resp.setContentType("text/html;charset=utf-8"); String name = req.getParameter("name"); String password = req.getParameter("pwd"); UserDAOImpl udi = new UserDAOImpl(); //调用登录方法 User user = udi.login(name, password); //判断密码错误还是账号问题 if (user == null) { req.setAttribute("error", "没有此用户,请检查用户名!"); req.getRequestDispatcher("/Login.jsp").forward(req, resp); } else if (name.equals(user.getUsername()) && password.equals(user.getPassword())) { req.getSession().setAttribute("user", user); //req.getRequestDispatcher("/user?cmd=list").forward(req, resp); resp.sendRedirect("/user?cmd=list"); } else { req.setAttribute("passworderror", "您的密码有误,请检查!"); req.getRequestDispatcher("/Login.jsp").forward(req, resp); } } }
流程是,先从Login.jsp进去,然后登陆。登录成功转到UserList.jsp-->这个表里可以做增加用户,删除用户,和修改用户 以及注销账户的操作。之前没做登录和注销功能的时候,添加一直可以正常添加。但是做了之后,一点添加的连接就变成了修改登录的用户。不能添加了。。。我找了好久,试了改变路径也不对,我猜测是因为新增和修改是做了一个判断(在save的Servlet里),然后里面的判断不起作用了。登陆的时候传进来的User跑到新增里面了。所以一点击就是修改登陆的用户的信息,而不是新增?但是不报错。,实在不知道该改哪里了?求大佬们帮我看看
这个是增删改查的Servlet
1 package UserServlet; 2 3 import java.io.IOException; 4 import java.util.List; 5 6 import javax.servlet.ServletException; 7 import javax.servlet.annotation.WebServlet; 8 import javax.servlet.http.HttpServlet; 9 import javax.servlet.http.HttpServletRequest; 10 import javax.servlet.http.HttpServletResponse; 11 12 import dao.impl.UserDAOImpl; 13 import domain.User; 14 15 //合并 16 @WebServlet("/user") 17 public class UserServlet extends HttpServlet{ 18 @Override 19 protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 20 req.setCharacterEncoding("utf-8"); 21 resp.setContentType("text/html;utf-8"); 22 //权限控制,如若取到的用户为空,则返回登录页面. 23 User user = (User)req.getSession().getAttribute("user"); 24 if (user == null) { 25 resp.sendRedirect("/Login.jsp"); 26 return; 27 } 28 String cmd = req.getParameter("cmd"); 29 if ("list".equals(cmd)) { 30 this.list(req, resp); 31 }else if ("edit".equals(cmd)) { 32 this.edit(req, resp); 33 }else if ("save".equals(cmd)) { 34 this.save(req, resp); 35 }else { 36 this.delete(req, resp); 37 } 38 } 39 //新增 40 protected void save(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 41 req.setCharacterEncoding("utf-8"); 42 resp.setContentType("text/html;utf-8"); 43 String id = req.getParameter("id"); 44 String name = req.getParameter("name"); 45 String pwd = req.getParameter("pwd"); 46 Integer age = Integer.valueOf(req.getParameter("age")); 47 UserDAOImpl udi = new UserDAOImpl(); 48 User user = new User(); 49 user.setUsername(name); 50 user.setPassword(pwd); 51 user.setAge(age); 52 if (id != null && !id.equals("")) { 53 //这个条件证明有id,所以是修改这条数据 54 user.setId(Integer.valueOf(id)); 55 udi.update(user); 56 } else { 57 //不满足就是没id,就属于新增的数据 58 udi.save(user); 59 } 60 req.getRequestDispatcher("/user?cmd=list").forward(req, resp);; 61 } 62 //查询一条,修改 63 protected void edit(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 64 String id = req.getParameter("id"); 65 UserDAOImpl udi = new UserDAOImpl(); 66 User user = udi.queryOne(Integer.valueOf(id)); 67 req.setAttribute("user", user); 68 req.getRequestDispatcher("/Add.jsp").forward(req, resp); 69 } 70 //删除 71 protected void delete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 72 req.setCharacterEncoding("utf-8"); 73 resp.setContentType("text/html;utf-8"); 74 int id = Integer.parseInt(req.getParameter("id")); 75 UserDAOImpl udi = new UserDAOImpl(); 76 udi.delete(id); 77 resp.sendRedirect("/user?cmd=list"); 78 } 79 //查询全部数据 80 protected void list(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 81 req.setCharacterEncoding("utf-8"); 82 resp.setContentType("text/html;charset=utf-8"); 83 UserDAOImpl dao = new UserDAOImpl(); 84 List<User> userlist = dao.queryAll(); 85 req.setAttribute("userlist", userlist); 86 req.getRequestDispatcher("/UserList.jsp").forward(req, resp); 87 88 } 89 }
package dao.impl; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.util.ArrayList; import java.util.List; import dao.IUserDAO; import domain.User; public class UserDAOImpl implements IUserDAO{ //增删改方法抽取 public void change(String sql, Object...params){ Connection conn = null; PreparedStatement ps = null; try { JDBCTools jt = JDBCTools.getInstance(); conn = jt.getConnection(); ps = conn.prepareStatement(sql); for (int i = 1; i <= params.length; i++) { ps.setObject(i, params[i-1]); } ps.executeUpdate(); } catch (Exception e) { e.printStackTrace(); } finally { JDBCTools.getInstance().close(null, ps, conn); } } //增加 @Override public void save(User user) { Connection conn = null; PreparedStatement ps = null; try { conn = JDBCTools.getInstance().getConnection(); ps = conn.prepareStatement("insert into user (username, password, age) values(?, ?, ?)"); //ps.setInt(1, user.getId()); ps.setString(1, user.getUsername()); ps.setString(2, user.getPassword()); ps.setInt(3, user.getAge()); ps.executeUpdate(); } catch (Exception e) { e.printStackTrace(); } finally { JDBCTools.getInstance().close(null, ps, conn); } } //修改 @Override public void update(User user) { Connection conn = null; PreparedStatement ps = null; try { conn = JDBCTools.getInstance().getConnection(); ps = conn.prepareStatement("update user set username=?, password=?, age=? where id=?"); ps.setString(1, user.getUsername()); ps.setString(2, user.getPassword()); ps.setInt(3, user.getAge()); ps.setInt(4, user.getId()); ps.executeUpdate(); } catch (Exception e) { e.printStackTrace(); } finally { JDBCTools.getInstance().close(null, ps, conn); } } //删除 @Override public void delete(int id) { Connection conn = null; PreparedStatement ps = null; try { conn = JDBCTools.getInstance().getConnection(); ps = conn.prepareStatement("delete from user where id= ?"); ps.setInt(1, id); ps.executeUpdate(); } catch (Exception e) { e.printStackTrace(); } finally { JDBCTools.getInstance().close(null, ps, conn); } } //查询一个 @Override public User queryOne(int id) { Connection conn = null; PreparedStatement ps = null; ResultSet rs = null; User user = null; try { conn = JDBCTools.getInstance().getConnection(); ps = conn.prepareStatement("select * from user where id= ?"); ps.setInt(1, id); rs = ps.executeQuery(); while(rs.next()) { user = new User(); user.setUsername(rs.getString("username")); user.setPassword(rs.getString("password")); user.setAge(rs.getInt("age")); user.setId(rs.getInt("id")); } } catch (Exception e) { e.printStackTrace(); } finally { JDBCTools.getInstance().close(null, ps, conn); } return user; } //查询全部 @Override public List<User> queryAll() { Connection conn = null; PreparedStatement ps = null; ResultSet rs = null; User user = null; List<User> list = new ArrayList<User>(); try { conn = JDBCTools.getInstance().getConnection(); ps = conn.prepareStatement("select * from user"); rs = ps.executeQuery(); while(rs.next()) { user = new User(); user.setUsername(rs.getString("username")); user.setPassword(rs.getString("password")); user.setAge(rs.getInt("age")); user.setId(rs.getInt("id")); list.add(user); } } catch (Exception e) { e.printStackTrace(); } finally { JDBCTools.getInstance().close(null, ps, conn); } return list; } //登录方法 @Override public User login(String username, String password) { Connection conn = null; PreparedStatement ps = null; ResultSet rs = null; User user = null; try { conn = JDBCTools.getInstance().getConnection(); ps = conn.prepareStatement("select * from user where username= ?"); //用传入的用户名和数据库里的对比,如果有则返回一个用户,没有返回空 ps.setString(1, username); rs = ps.executeQuery(); if (rs != null) { while(rs.next()) { user = new User(); user.setUsername(rs.getString("username")); user.setPassword(rs.getString("password")); user.setAge(rs.getInt("age")); user.setId(rs.getInt("id")); } return user; } } catch (Exception e) { e.printStackTrace(); } finally { JDBCTools.getInstance().close(null, ps, conn); } return user; } }
<%@ 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="/login" method="post"> 用户名<input type="text" name="name"/><br/> 密码<input type="password" name="pwd"/><br/> <input type="submit" value="登入" /><br/> </form> ${error} ${passworderror} </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>Insert title here</title> <style type="text/css"> table,tr,th,td{ border: 1px solid black; border-collapse: collapse; margin-top: auto; } </style> </head> <body> 恭喜${user.username}登陆成功!<a href="/logout">注销</a><br/> <a href="/Add.jsp">添加数据</a><br/> <table> <tr> <th>id</th> <th>用户名</th> <th>密码</th> <th>年龄</th> <th>操作</th> </tr> <c:forEach items="${userlist}" var="user"> <tr> <td>${user.id}</td> <td>${user.username}</td> <td>${user.password}</td> <td>${user.age}</td> <td><a href="/user?cmd=delete&id=${user.id}">删除</a>/<a href="/user?cmd=edit&id=${user.id}">修改</a></td> </tr> </c:forEach> </table> </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="/user?cmd=save" method="post"> <input type="hidden" type="text" name="id" value="${user.id}"> 用户名<input type="text" name="name" value="${user.username}"/><br> 密码<input type="password" name="pwd" value="${user.password}"/><br> 年龄<input type="text" name="age" value="${user.age}"/><br> <input type="submit" name="sub" value="提交"/> </form> </body> </html>
我把登录和权限控制那里注释了就又可以添加了,我猜想是因为,我在判断登录的方法的时候,如果成功了就返回一个对象,然后把这个对象放在session里,是不是由于这个USER对象存在,所以在判断是否修改的那里,由于有ID,所以就判定为是修改。。然后jsp里value里的值读取的也是登录用户的信息。。
登录对应的Servlet怎么没看到啊,应该就是登录就应的Servlet写得有问题导致的
上传了。感觉没问题啊,因为是从list开始添加数据的
解决了,就是因为判断修改的那里把登录用户的id穿进去了,所以判定为修改。谢谢
1,先打印出变量cmd的值,看看是否符合新增的条件。,2,调试一下,看看在哪里调用的
我试下
解决了,就是因为判断修改的那里把登录用户的id穿进去了,所以判定为修改。谢谢