发布时间 : 星期日 文章JAVAEE实验报告更新完毕开始阅读2aa9a4fcba4cf7ec4afe04a1b0717fd5360cb282
} }
public User findUser(String id) { conn = getConnectionn(); try { pStat = conn.prepareStatement(\ pStat.setString(1, id); rs = pStat.executeQuery(); User user=null; if(rs.next()){ user=new User(); user.setId(Integer.parseInt(rs.getString(\ user.setUsername(rs.getString(\ user.setPassword(rs.getString(\ } return user; } catch (Exception e) { return null; } finally { close(); } }
public List
public List
try { pStat = conn.prepareStatement(\
pStat.setString(1, username); pStat.setString(2, password); rs = pStat.executeQuery(); List
public boolean Update(User user) { conn = getConnectionn(); try { pStat = conn.prepareStatement(\ pStat.setString(1, user.getUsername()); pStat.setString(2, user.getPassword()); pStat.setInt(3, user.getId()); int cnt = pStat.executeUpdate(); if (cnt > 0) return true; else return false; } catch (Exception e) { return false; } finally { close(); } }
public boolean addUser(User user) { conn = getConnectionn(); try { pStat = conn.prepareStatement(\ pStat.setString(1, user.getUsername()); pStat.setString(2, user.getPassword());
int cnt = pStat.executeUpdate(); if (cnt > 0) return true; else return false; } catch (Exception e) { return false; } finally { close(); } } public boolean deleteUser(String id) { conn = getConnectionn(); try { pStat = conn.prepareStatement(\ pStat.setString(1, id); int cnt = pStat.executeUpdate(); if (cnt > 0) return true; else return false; } catch (Exception e) { return false; } finally { close(); } } }
//LoginServlet.java
public class LoginServlet extends HttpServlet { public void service(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
String username = request.getParameter(\String password = request.getParameter(\String path = request.getContextPath(); UserDAO userdao=new UserDAO(); if(userdao.isRoot(username, password)){ request.getSession().setAttribute(\ request.getSession().setAttribute(\ response.sendRedirect(path + \}else if(userdao.isExists(username, password)){ request.getSession().setAttribute(\ request.getSession().setAttribute(\ response.sendRedirect(path + \
}else { request.getSession().setAttribute(\用户名或密码不正确!\ response.sendRedirect(path + \ } } }
//RegServlet.java
public class RegServlet extends HttpServlet { public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String username = request.getParameter(\ String password = request.getParameter(\ String path = request.getContextPath(); // 获取请求路径 User user = new User(); user.setUsername(username); user.setPassword(password); UserDAO userdao = new UserDAO(); if (userdao.isExists(username)) { request.getSession().setAttribute(\用户名已存在\ response.sendRedirect(path + \ } else { boolean flag = userdao.addUser(user); if (flag) { request.getSession().setAttribute(\ response.sendRedirect(path + \ } else { request.getSession().setAttribute(\注册失败\ response.sendRedirect(path + \ } } } }
//EditServlet.java
public class EditServlet extends HttpServlet { public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { }
}
String path = request.getContextPath(); UserDAO userdao = new UserDAO();
String id=(String)request.getParameter(\User user=userdao.findUser(id);
request.getSession().setAttribute(\response.sendRedirect(path + \