1 添加用户持久层
1.1 创建添加用户的持久层接口
public interface UserManagerDao { void insertUser(Users users); }
1.2 持久层用户添加的接口实现类
/** * 用户管理持久层 */ public class UserManagerDaoImpl implements UserManagerDao { @Override public void insertUser(Users users) { Connection conn = null; try{ conn = JdbcUtils.getConnection(); conn.setAutoCommit(false); PreparedStatement ps = conn.prepareStatement("insert into users values(default,?,?,?,?,?)"); ps.setString(1,users.getUsername()); ps.setString(2,users.getUserpwd()); ps.setString(3,users.getUsersex()); ps.setString(4,users.getPhonenumber()); ps.setString(5,users.getQqnumber()); ps.execute(); conn.commit(); }catch(Exception e){ e.printStackTrace(); JdbcUtils.rollbackConnection(conn); }finally{ JdbcUtils.closeConnection(conn); } }
2 添加用户业务层
2.1 创建添加用户的业务层接口
public interface UserManagerService { void addUser(Users users); }
2.2 创建添加用户的业务层实现类
/** * 用户管理业务层 */ public class UserManagerServiceImpl implements UserManagerService { /** * 添加用户 * @param users */ @Override public void addUser(Users users) { UserManagerDao userManagerDao = new UserManagerDaoImpl(); userManagerDao.insertUser(users); } }
3 添加用户的 Servlet
/** * 处理用管理请求 */ @WebServlet("/userManager.do") public class UserManagerServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { this.doPost(req, resp); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String flag = req.getParameter("flag"); if("addUser".equals(flag)){ this.addUser(req,resp); } } //处理添加用户请求 private void addUser(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { Users users = this.createUsers(req); try{ UserManagerService userManagerService = new UserManagerServiceImpl(); userManagerService.addUser(users); resp.sendRedirect("ok.jsp"); }catch(Exception e){ e.printStackTrace(); resp.sendRedirect("error.jsp"); } } //获取用户提交数据 private Users createUsers(HttpServletRequest req){ String username = req.getParameter("username"); String userpwd = req.getParameter("userpwd"); String usersex = req.getParameter("usersex"); String phonenumber = req.getParameter("phonenumber"); String qqnumber = req.getParameter("qqnumber"); Users users = new Users(); users.setQqnumber(qqnumber); users.setPhonenumber(phonenumber); users.setUsername(username); users.setUserpwd(userpwd); users.setUsersex(usersex); return users; } }
4 前台添加页面
将一个模块放入独立的文件,要不全是jsp文件过于臃肿。
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>无标题文档</title> <link href="../css/style.css" rel="stylesheet" type="text/css" /> <link href="../css/style.css" rel="stylesheet" type="text/css" /> <link href="../css/select.css" rel="stylesheet" type="text/css" /> </head> <body> <div class="place"> <span>位置:</span> <ul class="placeul"> <li><a href="#">用户管理</a></li> <li><a href="#">添加用户</a></li> </ul> </div> <div class="formbody"> <div class="formtitle"><span>基本信息</span></div> <form action="../userManager.do" method="post"> <input type="hidden" name="flag" value="addUser"/> <ul class="forminfo"> <li> <label>用户名</label> <input name="username" type="text" class="dfinput" /></li> <li> <li> <label>用户密码</label> <input name="userpwd" type="text" class="dfinput" /><i></i></li> <li> <label>性别</label><cite> <input name="usersex" type="radio" value="1" checked="checked" />男 <input name="usersex" type="radio" value="0" />女 </li> <li> <label>联系方式</label> <input name="phonenumber" type="text" class="dfinput" /> </li> <li> <label>QQ号</label> <input name="qqnumber" type="text" class="dfinput" /> </li> <li> <label> </label> <input type="submit" class="btn" value="确认保存" /> </li> </ul> </form> </div> </body> </html>
4.1 反馈成功的jsp页面
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title></title> <link href="css/style.css" rel="stylesheet" type="text/css" /> <script type="text/javascript"> function submitForm(){ window.close(); } </script> </head> <body> <div class="place"> <span>位置:</span> <ul class="placeul"> <li><a href="#">操作提示</a></li> </ul> </div> 操作成功! </body> </html>