本节博文旨在实现本次Web应用的个人管理功能。可以在登陆后 查看并修改个人信息或者进行密码修改。同时,在输入的时候进行表单验证,验证输入的格式。
笔者会把大概实现过程贴出来,有看不懂过程且需要项目源码的请戳:http://download.csdn.net/detail/xie_xiansheng/9486872,需要数据库表格的请留言。有些小细节没完善,有些代码冗余,初学请见谅!
效果图如下:
接下来,上代码:
org.great.servlet包下的PersonalServlet类:
public class PersonalServlet extends HttpServlet{ @Override protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { req.setCharacterEncoding("utf-8"); resp.setContentType("text/html;charset=utf-8"); /**根据LoginServlet的session拿到登录用户*/ HttpSession session = req.getSession(); UserBean userBean = (UserBean)session.getAttribute("Logindo"); //获取jsp页面传过来的task,判断要进行什么操作 String task = req.getParameter("task"); String lname = userBean.getUser_lname(); UserDao userDao = DaoFactory.getUserDao(); //点击导航栏获取的task->checkinfo.jsp if("checkinfo".equals(task)){ userBean = userDao.findUser_ByLname(lname); req.setAttribute("userBean", userBean); req.getRequestDispatcher("jsp/personal/checkinfo.jsp").forward(req, resp); //checkinfo.jsp->个人管理下的信息修改 }else if("alerinfo".equals(task)){ userBean = userDao.findUser_ByLname(lname); req.setAttribute("userBean", userBean); req.getRequestDispatcher("jsp/personal/alertperson.jsp").forward(req, resp); //alertperson-> 修改个人信息点击提交 }else if("alertinfoend".equals(task)){ String username = req.getParameter("username"); String usex = req.getParameter("usex"); String tel = req.getParameter("tel"); int alertinfoid = Integer.parseInt(req.getParameter("userid")); //修改个人管理信息 String msg = userDao.alertInfo_ById(alertinfoid, username, usex, tel); userBean = userDao.findUser_ById(alertinfoid); req.setAttribute("userBean", userBean); req.setAttribute("msg", msg); req.getRequestDispatcher("jsp/personal/alertperson.jsp").forward(req, resp); }else if("alertpwd".equals(task)){ //点击导航栏获取的task->alertpwd.jsp userBean = userDao.findUser_ByLname(lname); req.setAttribute("userBean", userBean); req.getRequestDispatcher("jsp/personal/alertpwd.jsp").forward(req, resp); //修改密码点击提交 }else if("alertpwdend".equals(task)){ String oldPwd = req.getParameter("oldpwd"); String newPwd = req.getParameter("newpwd"); String newPwd2 = req.getParameter("newpwd2"); int alertpwdid = Integer.parseInt(req.getParameter("userid")); userBean = userDao.findUser_ById(alertpwdid); if(oldPwd.equals(userBean.getUser_pwd())){ if(newPwd.equals(newPwd2)){ //修改新密码 String msg = userDao.alertPwd_ById(alertpwdid, newPwd); req.setAttribute("msg", msg); }else{ req.setAttribute("msg", "密码确认失败,请重新输入!"); } }else{ req.setAttribute("msg", "原密码错误,请重新输入!"); } req.setAttribute("userBean", userBean); req.getRequestDispatcher("jsp/personal/alertpwd.jsp").forward(req, resp); } } }
public UserBean findUser_ByLname(String lname) { //根据用户名查找用户信息 Connection conn = DBUtils.getConn(); UserBean userBean = null; String sql = "select user_id,role_id,user_lname,user_pwd,user_name,user_sex,user_status," + "createtime,driving_id,tel from t_user where user_lname = ? and user_status!='D'" ; try { pre = conn.prepareStatement(sql); pre.setString(1, lname); rs = pre.executeQuery(); if(rs.next()){ userBean = new UserBean(); userBean.setUser_id(rs.getInt(1)); userBean.setRole_id(rs.getInt(2)); userBean.setUser_lname(rs.getString(3)); userBean.setUser_pwd(rs.getString(4)); userBean.setUser_name(rs.getString(5)); userBean.setUser_sex(rs.getString(6)); userBean.setUser_status(rs.getString(7)); userBean.setCreatetime(rs.getString(8)); userBean.setDriving_id(rs.getInt(9)); userBean.setTel(rs.getString(10)); } } catch (SQLException e) { e.printStackTrace(); } finally{ DBUtils.close(conn, pre, rs); } return userBean; }
public String alertInfo_ById(int userID, String username, String usersex, String tel) { //根据用户填写的信息修改用户数据表 Connection conn = DBUtils.getConn(); String msg = ""; String sql = "update t_user set user_name = ?,user_sex = ?,tel = ? where user_id = ?" ; try { pre = conn.prepareStatement(sql); pre.setString(1, username); pre.setString(2, usersex); pre.setString(3, tel); pre.setInt(4, userID); rs = pre.executeQuery(); if(rs.next()){ msg = "修改完成!"; } } catch (SQLException e) { e.printStackTrace(); } finally{ DBUtils.close(conn, pre, rs); } return msg; }
public UserBean findUser_ById(int userID) { //根据用户ID查找用户信息 Connection conn = DBUtils.getConn(); UserBean userBean = null; String sql = "select u.user_id,u.role_id,u.user_lname,u.user_pwd,u.user_name,u.user_sex,u.user_status,"+ "u.createtime,u.tel,d.driving_name from t_user u,t_drivingschool d where "+ "u.driving_id = d.driving_id and user_id = ? and user_status!='D'" ; try { pre = conn.prepareStatement(sql); pre.setInt(1, userID); rs = pre.executeQuery(); if(rs.next()){ userBean = new UserBean(); userBean.setUser_id(rs.getInt(1)); userBean.setRole_id(rs.getInt(2)); userBean.setUser_lname(rs.getString(3)); userBean.setUser_pwd(rs.getString(4)); userBean.setUser_name(rs.getString(5)); userBean.setUser_sex(rs.getString(6)); userBean.setUser_status(rs.getString(7)); userBean.setCreatetime(rs.getString(8)); userBean.setTel(rs.getString(9)); userBean.setDriving_name(rs.getString(10)); } } catch (SQLException e) { e.printStackTrace(); } finally{ DBUtils.close(conn, pre, rs); } return userBean; }
public String alertPwd_ById(int userID,String newPwd) { //根据用户ID修改新密码 Connection conn = DBUtils.getConn(); String msg = ""; String sql = "update t_user set user_pwd = ? where user_id = ?" ; try { pre = conn.prepareStatement(sql); pre.setString(1, newPwd); pre.setInt(2, userID); rs = pre.executeQuery(); if(rs.next()){ msg = "修改完成!"; } } catch (SQLException e) { e.printStackTrace(); } finally{ DBUtils.close(conn, pre, rs); } return msg; }
<%@ page language="java" import="java.util.*,org.great.bean.*" pageEncoding="utf-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'usermanager.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> <link rel="stylesheet" type="text/css" href="<%=basePath%>/background/Style/skin.css" /> </head> <body> <table width="100%" border="0" cellpadding="0" cellspacing="0"> <!-- 头部开始 --> <tr> <td width="17" valign="top" background="<%=basePath%>/background/Images/mail_left_bg.gif"> <img src="<%=basePath%>/background/Images/left_top_right.gif" width="17" height="29" /> </td> <td valign="top" background="<%=basePath%>/background/Images/content_bg.gif"> <table width="100%" height="31" border="0" cellpadding="0" cellspacing="0" background="<%=basePath%>/background/<%=basePath%>/background/Images/content_bg.gif"> <tr><td height="31"><div class="title" style="color: red">信息查看</div></td></tr> </table> </td> <td width="16" valign="top" background="<%=basePath%>/background/Images/mail_right_bg.gif"><img src="<%=basePath%>/background/Images/nav_right_bg.gif" width="16" height="29" /></td> </tr> <!-- 中间部分开始 --> <tr> <!--第一行左边框--> <td valign="middle" background="<%=basePath%>/background/Images/mail_left_bg.gif"> </td> <!--第一行中间内容--> <td valign="top" bgcolor="#F7F8F9"> <table width="100%" border="0" align="center" cellpadding="0" cellspacing="0"> <!-- 空白行--> <tr><td colspan="2" valign="top"> </td><td> </td><td valign="top"> </td></tr> <tr> <td colspan="4"> <table> <tr> <td width="100" align="center"><img src="<%=basePath%>/background/Images/mime.gif" /></td> <td valign="bottom"><h3 style="letter-spacing:1px;color: blue">在这里,您可以查看您的个人信息</h3></td> </tr> </table> </td> </tr> <!-- 一条线 --> <tr> <td height="40" colspan="4"> <table width="100%" height="1" border="0" cellpadding="0" cellspacing="0" bgcolor="#CCCCCC"> <tr><td></td></tr> </table> </td> </tr> <!-- 产品列表开始 --> <%UserBean userBean = (UserBean)request.getAttribute("userBean"); %> <tr> <td width="2%"> </td> <td width="96%"> <table width="100%"> <tr> <td colspan="2"> <form action="" method="post"> <table width="100%" class="cont tr_color"> <tr> <th>ID</th> <th>账户</th> <th>用户名</th> <th>性别</th> <th>电话号码</th> <th>注册时间</th> <th>操作</th> </tr> <tr align="center" class="d"> <td><%=userBean.getUser_id()%></td> <td><%=userBean.getUser_lname() %></td> <td><%=userBean.getUser_name() %></td> <td><%=userBean.getUser_sex().equals("M")?"男":"女" %></td> <td><%=userBean.getTel() %></td> <td><%=userBean.getCreatetime() %></td> <td> <a href="personal.do?task=alerinfo&lname=<%=userBean.getUser_lname() %>"style="color: blue">修改 </a> </td> </tr> </table> </form> </td> </tr> </table> </td> <td width="2%"> </td> </tr> <!-- 产品列表结束 --> <tr> <td height="40" colspan="4"> <table width="100%" height="1" border="0" cellpadding="0" cellspacing="0" bgcolor="#CCCCCC"> <tr><td></td></tr> </table> </td> </tr> <tr> <td width="2%"> </td> <td width="51%" class="left_txt"> <img src="<%=basePath%>/background/Images/icon_mail.gif" width="16" height="11"> 客户服务邮箱:[email protected]<br /> <img src="<%=basePath%>/background/Images/icon_phone.gif" width="17" height="14"> 官方网站:<a href="http://my.csdn.net/xie_xiansheng" target="_blank">作者博客</a> </td> <td> </td><td> </td> </tr> </table> </td> <td background="<%=basePath%>/background/Images/mail_right_bg.gif"> </td> </tr> <!-- 底部部分 --> <tr> <td valign="bottom" background="<%=basePath%>/background/Images/mail_left_bg.gif"> <img src="<%=basePath%>/background/Images/buttom_left.gif" width="17" height="17" /> </td> <td background="<%=basePath%>/background/Images/buttom_bgs.gif"> <img src="<%=basePath%>/background/Images/buttom_bgs.gif" width="17" height="17"> </td> <td valign="bottom" background="<%=basePath%>/background/Images/mail_right_bg.gif"> <img src="<%=basePath%>/background/Images/buttom_right.gif" width="16" height="17" /> </td> </tr> </table> </body> </html>
<%@ page language="java" import="java.util.*,org.great.bean.*" pageEncoding="utf-8"%> <%@page import="org.great.bean.UserBean"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'alertuser.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> <link rel="stylesheet" type="text/css" href="<%=basePath%>/background/Style/skin.css" /> <script type="text/javascript" src="<%=basePath%>/jquery-2.1.4/jquery1.9.0.min.js"></script> <script type="text/javascript" src="<%=basePath%>/easyvalidator2/js/validate.pack.js"> </script> <link href="<%=basePath%>/easyvalidator2/css/validate.css" rel="stylesheet" type="text/css" /> </head> <body> <table width="100%" border="0" cellpadding="0" cellspacing="0"> <!-- 头部开始 --> <tr> <td width="17" valign="top" background="<%=basePath%>/background/Images/mail_left_bg.gif"> <img src="<%=basePath%>/background/Images/left_top_right.gif" width="17" height="29" /> </td> <td valign="top" background="<%=basePath%>/background/Images/content_bg.gif"> <table width="100%" height="31" border="0" cellpadding="0" cellspacing="0" background="<%=basePath%>/background/<%=basePath%>/background/Images/content_bg.gif"> <tr><td height="31"><div class="title" style="color: red">信息修改</div></td></tr> </table> </td> <td width="16" valign="top" background="<%=basePath%>/background/Images/mail_right_bg.gif"><img src="<%=basePath%>/background/Images/nav_right_bg.gif" width="16" height="29" /></td> </tr> <!-- 中间部分开始 --> <tr> <!--第一行左边框--> <td valign="middle" background="<%=basePath%>/background/Images/mail_left_bg.gif"> </td> <!--第一行中间内容--> <td valign="top" bgcolor="#F7F8F9"> <table width="100%" border="0" align="center" cellpadding="0" cellspacing="0"> <!-- 空白行--> <tr><td colspan="2" valign="top"> </td><td> </td><td valign="top"> </td></tr> <tr> <td colspan="4"> <table> <tr> <td width="100" align="center"><img src="<%=basePath%>/background/Images/mime.gif" /></td> <td valign="bottom"><h3 style="letter-spacing:1px;color: blue">在这里,您可以修改您的个人信息</h3></td> </tr> </table> </td> </tr> <!-- 一条线 --> <tr> <td height="40" colspan="4"> <table width="100%" height="1" border="0" cellpadding="0" cellspacing="0" bgcolor="#CCCCCC"> <tr><td></td></tr> </table> </td> </tr> <!-- 添加栏目开始 --> <%UserBean userBean = (UserBean)request.getAttribute("userBean"); %> <tr> <td width="2%"> </td> <td width="96%"> <table width="100%"> <tr> <td colspan="2"> <form action="personal.do?task=alertinfoend" method="post"> <table width="50%" class="cont" > <tr> <td width="2%"> </td> <td>用户名:</td> <td width="80%"><input class="text" type="text" name="username" value="<%=userBean.getUser_name() %>" reg="^[\u4e00-\u9fa5]{1,6}$"/> <span name="easyTip">(只允许1-6个中文字符)</span> <input type="hidden" name="userid" value="<%=userBean.getUser_id()%>" /> </td> <td width="2%"> </td> </tr> <tr> <td width="2%"> </td> <td>性别:</td> <td width="20%"> <select id="usex" name="usex"> <option value="M">男</option> <option value="W">女</option> </select> </td> <td width="2%"> </td> </tr> <tr> <td width="2%"> </td> <td>电话号码:</td> <td width="20%"> <input class="text" type="text" name="tel" value="<%=userBean.getTel()%>" reg="^1\d{10}$" /><span name="easyTip">(11位以1开头手机号码格式)</span> </td> <td width="2%"> </td> </tr> <tr> <td> </td> <td colspan="5" align="center"><input class="btn" type="submit" value="提交" /></td> <td> </td> </tr> </table> </form> </td> </tr> </table> </td> <td width="2%"> </td> </tr> <!-- 添加栏目结束 --> <tr> <td height="40" colspan="4"> <table width="100%" height="1" border="0" cellpadding="0" cellspacing="0" bgcolor="#CCCCCC"> <tr><td></td></tr> </table> </td> </tr> <tr> <td width="2%"> </td> <td width="51%" class="left_txt"> <img src="<%=basePath%>/background/Images/icon_mail.gif" width="16" height="11"> 客户服务邮箱:[email protected]<br /> <img src="<%=basePath%>/background/Images/icon_phone.gif" width="17" height="14"> 官方网站:<a href="http://my.csdn.net/xie_xiansheng" target="_blank">作者博客</a> </td> <td> </td><td> </td> </tr> </table> </td> <td background="<%=basePath%>/background/Images/mail_right_bg.gif"> </td> </tr> <!-- 底部部分 --> <tr> <td valign="bottom" background="<%=basePath%>/background/Images/mail_left_bg.gif"> <img src="<%=basePath%>/background/Images/buttom_left.gif" width="17" height="17" /> </td> <td background="<%=basePath%>/background/Images/buttom_bgs.gif"> <img src="<%=basePath%>/background/Images/buttom_bgs.gif" width="17" height="17"> </td> <td valign="bottom" background="<%=basePath%>/background/Images/mail_right_bg.gif"> <img src="<%=basePath%>/background/Images/buttom_right.gif" width="16" height="17" /> </td> </tr> </table> </body> <script type="text/javascript"> $("#usex").val("<%=userBean.getUser_sex()%>"); var msg = "<%=request.getAttribute("msg")%>"; if("null"!=msg){ alert(msg); } </script> </html>
<%@ page language="java" import="java.util.*,org.great.bean.*" pageEncoding="utf-8"%> <%@page import="org.great.bean.UserBean"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'alertuser.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> <link rel="stylesheet" type="text/css" href="<%=basePath%>/background/Style/skin.css" /> <script type="text/javascript" src="<%=basePath%>/jquery-2.1.4/jquery1.9.0.min.js"></script> <script type="text/javascript" src="<%=basePath%>/easyvalidator2/js/validate.pack.js"> </script> <link href="<%=basePath%>/easyvalidator2/css/validate.css" rel="stylesheet" type="text/css" /> </head> <body> <table width="100%" border="0" cellpadding="0" cellspacing="0"> <!-- 头部开始 --> <tr> <td width="17" valign="top" background="<%=basePath%>/background/Images/mail_left_bg.gif"> <img src="<%=basePath%>/background/Images/left_top_right.gif" width="17" height="29" /> </td> <td valign="top" background="<%=basePath%>/background/Images/content_bg.gif"> <table width="100%" height="31" border="0" cellpadding="0" cellspacing="0" background="<%=basePath%>/background/<%=basePath%>/background/Images/content_bg.gif"> <tr><td height="31"><div class="title" style="color: red">密码修改</div></td></tr> </table> </td> <td width="16" valign="top" background="<%=basePath%>/background/Images/mail_right_bg.gif"><img src="<%=basePath%>/background/Images/nav_right_bg.gif" width="16" height="29" /></td> </tr> <!-- 中间部分开始 --> <tr> <!--第一行左边框--> <td valign="middle" background="<%=basePath%>/background/Images/mail_left_bg.gif"> </td> <!--第一行中间内容--> <td valign="top" bgcolor="#F7F8F9"> <table width="100%" border="0" align="center" cellpadding="0" cellspacing="0"> <!-- 空白行--> <tr><td colspan="2" valign="top"> </td><td> </td><td valign="top"> </td></tr> <tr> <td colspan="4"> <table> <tr> <td width="100" align="center"><img src="<%=basePath%>/background/Images/mime.gif" /></td> <td valign="bottom"><h3 style="letter-spacing:1px; color: blue">在这里,您可以修改您的个人密码</h3></td> </tr> </table> </td> </tr> <!-- 一条线 --> <tr> <td height="40" colspan="4"> <table width="100%" height="1" border="0" cellpadding="0" cellspacing="0" bgcolor="#CCCCCC"> <tr><td></td></tr> </table> </td> </tr> <!-- 添加栏目开始 --> <%UserBean userBean = (UserBean)request.getAttribute("userBean"); %> <tr> <td width="2%"> </td> <td width="96%"> <table width="100%"> <tr> <td colspan="2"> <form action="personal.do?task=alertpwdend" method="post"> <table width="50%" class="cont" > <tr> <td width="4%"> </td> <td width="20%">用户名:</td> <td width="80%"><span><%=userBean.getUser_name() %></span> <input type="hidden" name="userid" value="<%=userBean.getUser_id()%>" /> </td> <td width="2%"> </td> </tr> <tr> <td width="4%"> </td> <td width="20%">原密码:</td> <td width="20%"> <input class="text" type="text" name="oldpwd" value="" /> </td> <td width="2%"> </td> </tr> <tr> <td width="4%"> </td> <td width="20%">新密码:</td> <td width="20%"> <input class="text" type="password" name="newpwd" value="" reg="^[A-Za-z0-9 ]{1,16}$" /> <span name="easyTip" style="width: 220px;">(*只允许英文或数字1-16位)</span> </td> <td width="2%"> </td> </tr> <tr> <td width="4%"> </td> <td width="20%">新密码确认:</td> <td width="20%"> <input class="text" type="password" name="newpwd2" value="" /> </td> <td width="2%"> </td> </tr> <tr> <td> </td> <td colspan="5" align="center"><input class="btn" type="submit" value="提交" /></td> <td> </td> </tr> </table> </form> </td> </tr> </table> </td> <td width="2%"> </td> </tr> <!-- 添加栏目结束 --> <tr> <td height="40" colspan="4"> <table width="100%" height="1" border="0" cellpadding="0" cellspacing="0" bgcolor="#CCCCCC"> <tr><td></td></tr> </table> </td> </tr> <tr> <td width="2%"> </td> <td width="51%" class="left_txt"> <img src="<%=basePath%>/background/Images/icon_mail.gif" width="16" height="11"> 客户服务邮箱:[email protected]<br /> <img src="<%=basePath%>/background/Images/icon_phone.gif" width="17" height="14"> 官方网站:<a href="http://my.csdn.net/xie_xiansheng" target="_blank">作者博客</a> </td> <td> </td><td> </td> </tr> </table> </td> <td background="<%=basePath%>/background/Images/mail_right_bg.gif"> </td> </tr> <!-- 底部部分 --> <tr> <td valign="bottom" background="<%=basePath%>/background/Images/mail_left_bg.gif"> <img src="<%=basePath%>/background/Images/buttom_left.gif" width="17" height="17" /> </td> <td background="<%=basePath%>/background/Images/buttom_bgs.gif"> <img src="<%=basePath%>/background/Images/buttom_bgs.gif" width="17" height="17"> </td> <td valign="bottom" background="<%=basePath%>/background/Images/mail_right_bg.gif"> <img src="<%=basePath%>/background/Images/buttom_right.gif" width="16" height="17" /> </td> </tr> </table> </body> <script type="text/javascript"> var msg = "<%=request.getAttribute("msg")%>"; if("null"!=msg){ alert(msg); } </script> </html>