参考答案
转发是一个Web组件(Servlet/JSP)将未完成的处理通过容器转交给另外一个Web组件继续完成。
可以按照以下三个步骤来实现转发:
1.绑定数据到request对象,代码如下:
request.setAttribute(String name,Object obj);
2.获得转发器,代码如下:
RequestDispatcher rd = request.getRequestDispatcher( String uri);
3.转发,代码如下:
rd.forward ( request , response ) ;
参考答案
转发和重定向的区别有以下几点:
1. 重定向是浏览器发送请求并收到响应以后再次向一个新地址发请求,转发是服务器收到请求后为了完成响应转到一个新的地址。
2. 重定向中有两次请求,不共享数据,转发只产生一次请求,且在组件间共享数据。
3. 重定向后地址栏地址改变,而转发则不会。
4. 重定向的新地址可以是任意地址,转发到的新地址必须是同一个应用内的某地址。
将美工制作的页面转变成JSP页面,使用转发、重定向等技术实现完整的帐务帐号管理功能。
另外,有管理员表的信息如表-1所示。
表-1 管理员表(ADMIN_INFO)
为NETCTOSS项目添加登录功能。登录成功后显示所有帐务帐号信息。
参考答案
功能的完成顺序为查询(增加(删除(修改。在查询功能中,JSP页面负责展示,其数据来源是使用转发技术从request对象中获取。请求的地址映射规则为后缀匹配模式,由一个ActionServlet处理分发请求。
创建admin_info表,编写AdminInfo实体和AdminInfoDAO操作类。编写登录页面,提交到服务器以后,判断用户名是否存在,如果存在则显示所有帐务账号信息,如果不存在则绑定提示信息后转发到登录页面。
完成本案例可以按照如下步骤进行。
步骤一:创建web工程
创建名为Servlet_05_exec的web工程,该工程的结构如图-1所示。
图-1
步骤二:将前面工程中的dao包和entity包拷贝到src中。
dao包下的DBUtil.java文件为获取连接关闭连接的工具类,AccountDAO.java文件为针对数据库account表的数据操作对象。ActionServlet.java文件为处理请求的控制类。如图-2所示为完整结构。
图 - 2
步骤三:完成帐务帐号信息显示功能
修改ActionServlet中的service方法,判断动作是否是list,查询数据后存入request中,并转发给listAccount.jsp页面。修改service方法如图-3所示。
图– 3
WebRoot下面新建listAccount.jsp页面,复制静态代码至该页面,复制images、styles,修改page指令。项目结构如图-4所示,添加循环输出如图-5所示。
图– 4
图– 5
运行效果如图-6所示。
图– 6
步骤四:完成增加帐务帐号信息功能
新建addAccount.jsp,表单的action值为add.do。修改ActionServlet,添加对增加动作的处理。添加成功后,重定向到list.do,查看全部帐务帐号信息。工程结构如图-7所示。
图– 7
修改listAccount.jsp页面,将页面中的增加按钮导航到addAccount.jsp,如图-8所示。
图– 8
修改ActionServlet代码,如图-9所示。
图–9
点击“增加“按钮,进入addAccount.jsp,输入需要添加的帐务帐号信息。界面效果如图-10所示。
图– 10
点击“保存“按钮,进入listAccount.jsp页面显示数据,如图-11所示。
图– 11
图中黄色为添加进入的数据。
步骤五:完成删除员工信息功能
修改listAccount.jsp中删除操作的链接地址,如图-12所示。
图– 12
修改ActionServlet的判断分支,如图-13所示。
图–13
运行,删除上一步添加的姓名为“张三”的账务账号,如图-14所示。
图- 14
点击该条数据后的“删除”按钮,结果数据变化如图-15所示。
图– 15
步骤六:完成修改帐务帐号信息功能
要完成修改帐务帐号的信息,过程如下:
1)添加修改页面modifyAccount.jsp,工程结构如图-16所示。
图- 16
2)修改ActionServlet,添加加载的判断,如图-17所示。
图– 17
3)修改listAccount.jsp页面的修改链接的地址,如图-18所示。
图– 18
4)修改modifyAccount.jsp页面,读取绑定数据,将数据显示到表单中,如图-19所示。
图– 19
5)运行,查看是否能够正确加载员工信息,如图-20所示。
图-20
6)修改ActionServlet,保存成功后重定向到list.do,如图-21所示。
图– 21
步骤七:创建admin_info表
创建admin_info表;创建序列admin_id_seq并插入测试数据,SQL语句如图-22所示。
图– 22
步骤八:创建entity.AdminInfo类
创建实体类AdminInfo与数据表admin_info的一一对应关系,代码如下所示:
package entity; import java.util.Date; publicclass AdminInfo { privateint adminId; private String adminCode; private String password; private String name; private String telephone; private String email; private Date enrolldate; publicint getAdminId() { return adminId; } @Override public String toString() { return "AdminInfo [adminCode=" + adminCode + ", adminId=" + adminId + ", email=" + email + ", enrolldate=" + enrolldate + ", name=" + name + ", password=" + password + ", telephone=" + telephone + "]"; } publicvoid setAdminId(int adminId) { this.adminId = adminId; } public String getAdminCode() { return adminCode; } publicvoid setAdminCode(String adminCode) { this.adminCode = adminCode; } public String getPassword() { return password; } publicvoid setPassword(String password) { this.password = password; } public String getName() { return name; } publicvoid setName(String name) { this.name = name; } public String getTelephone() { return telephone; } publicvoid setTelephone(String telephone) { this.telephone = telephone; } public String getEmail() { return email; } publicvoid setEmail(String email) { this.email = email; } public Date getEnrolldate() { return enrolldate; } publicvoid setEnrolldate(Date enrolldate) { this.enrolldate = enrolldate; } }
步骤九:创建dao.AdminInfoDAO类
创建AdminInfoDAO类,其代码如下所示:
package dao; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import entity.AdminInfo; /** * 管理员的数据操作类 */ public class AdminInfoDAO { /** * 根据管理员帐号查找对应的管理员对象 * @param userName 帐号 * @return 管理员实体 */ public AdminInfo findByAdminCode(String adminCode) throws Exception { AdminInfo adminInfo = null; Connection con = null; PreparedStatement stmt = null; ResultSet rs = null; try { con = DBUtil.getConnection(); stmt = con.prepareStatement("select * from admin_info where admin_code=?"); stmt.setString(1, adminCode); rs = stmt.executeQuery(); if (rs.next()) { adminInfo = new AdminInfo(); adminInfo.setAdminId(rs.getInt("admin_id")); adminInfo.setAdminCode(rs.getString("admin_code")); adminInfo.setPassword(rs.getString("password")); adminInfo.setName(rs.getString("name")); adminInfo.setTelephone(rs.getString("telephone")); adminInfo.setEmail(rs.getString("email")); adminInfo.setEnrolldate(rs.getDate("enrolldate")); } } catch (Exception e) { e.printStackTrace(); throw e; } finally { DBUtil.close(con); } return adminInfo; } }
步骤十:创建login.jsp页面
创建login.jsp页面,其代码如下所示:
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>达内-NetCTOSS</title> <link type="text/css" rel="stylesheet" media="all" href="styles/global.css" /> <link type="text/css" rel="stylesheet" media="all" href="styles/global_color.css" /> </head> <body class="index"> <div class="login_box"> <form name="login" action="login.do" method="post"> <table> <tr> <td class="login_info"> 账号: </td> <td colspan="2"> <input name="adminCode" type="text" class="width150" /> </td> </tr> <tr> <td class="login_info"> 密码: </td> <td colspan="2"> <input name="password" type="password" class="width150" /> </td> </tr> <tr> <td></td> <td class="login_button" > <a href="javascript:document.login.submit();"><img src="images/login_btn.png" /> </a> </td> <td> <span class="required"> <% String errorMsg = (String) request.getAttribute("login_err"); %><span style="color: red;"><%=errorMsg == null ? "" : errorMsg%></span> </span> </td> </tr> <tr> </tr> </table> </form> </div> </body> </html>
步骤十一:修改ActionServlet
修改ActionServlet,添加处理登录的分支,代码如图-23所示。
图– 23
修改login.jsp页面,读取绑定信息,非空则将错误信息显示在“登录”按钮的后面,如图-24所示。
图– 24
最后,运行查看结果。
本案例的完整代码如下。
AccountDAO类的完整代码如下所示:
package dao; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.util.ArrayList; import java.util.List; import entity.Account; /** * Account的数据操作对象,负责Account的增删改查 */ public class AccountDAO { /** * 查询所有帐务帐号信息 */ public List<Account> findAll() throws Exception{ List<Account> accounts = new ArrayList<Account>(); Connection conn = null; PreparedStatement stmt = null; ResultSet rs = null; try{ conn = DBUtil.getConnection(); stmt = conn.prepareStatement("select * from account"); rs = stmt.executeQuery(); while(rs.next()){ Account account = new Account(); int accountId = rs.getInt("account_id"); String realName = rs.getString("real_name"); String idcardNo = rs.getString("idcard_no"); String loginName = rs.getString("login_name"); String status=rs.getString("status"); String telephone = rs.getString("telephone"); account.setAccountId(accountId); account.setRealName(realName); account.setIdcardNo(idcardNo); account.setLoginName(loginName); account.setStatus(status); account.setTelephone(telephone); accounts.add(account); } }catch(Exception e){ e.printStackTrace(); throw e; }finally{ DBUtil.close(conn); } return accounts; } /** * 删除帐务帐号信息 */ public void delete(int accountId) throws Exception{ Connection conn = null; PreparedStatement stmt = null; try{ conn = DBUtil.getConnection(); stmt = conn.prepareStatement("delete from account where account_id=?"); stmt.setInt(1, accountId); stmt.executeUpdate(); }catch(Exception e){ e.printStackTrace(); throw e; }finally{ DBUtil.close(conn); } } /** * 增加帐务帐号信息 */ public void save(Account account) throws Exception{ Connection conn = null; PreparedStatement stmt = null; try{ conn = DBUtil.getConnection(); stmt = conn.prepareStatement("insert into account" + "(account_id,login_name,status,real_name,idcard_no,telephone) " + "values(account_id_seq.nextval,?,?,?,?,?)"); stmt.setString(1, account.getLoginName()); stmt.setString(2, "0");//添加创建即为开通状态 stmt.setString(3, account.getRealName()); stmt.setString(4, account.getIdcardNo()); stmt.setString(5, account.getTelephone()); stmt.executeUpdate(); }catch(Exception e){ e.printStackTrace(); throw e; }finally{ DBUtil.close(conn); } } /** * 根据id查询员工信息 */ public Account findById(int id) throws Exception{ Account account = null; Connection conn = null; PreparedStatement stmt = null; ResultSet rs = null; try{ conn = DBUtil.getConnection(); stmt = conn.prepareStatement("select * from account where account_id=?"); stmt.setInt(1, id); rs = stmt.executeQuery(); if(rs.next()){ account = new Account(); int accountId = rs.getInt("account_id"); String realName = rs.getString("real_name"); String idcardNo = rs.getString("idcard_no"); String loginName = rs.getString("login_name"); String status=rs.getString("status"); String telephone = rs.getString("telephone"); account.setAccountId(accountId); account.setRealName(realName); account.setIdcardNo(idcardNo); account.setLoginName(loginName); account.setStatus(status); account.setTelephone(telephone); } }catch(Exception e){ e.printStackTrace(); throw e; }finally{ DBUtil.close(conn); } return account; } /** * 保存修改员工信息 */ public void modify(Account account)throws Exception{ Connection conn = null; PreparedStatement stmt = null; try{ conn = DBUtil.getConnection(); stmt = conn.prepareStatement("update account set login_name=?,real_name=?," + "idcard_no=?,telephone=? where account_id=?"); stmt.setString(1, account.getLoginName()); stmt.setString(2, account.getRealName()); stmt.setString(3, account.getIdcardNo()); stmt.setString(4, account.getTelephone()); stmt.setInt(5, account.getAccountId()); stmt.executeUpdate(); }catch(Exception e){ e.printStackTrace(); throw e; }finally{ DBUtil.close(conn); } } }
AdminInfoDAO的完整代码如下所示:
package dao; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import entity.AdminInfo; /** * 管理员的数据操作类 */ public class AdminInfoDAO { /** * 根据管理员帐号查找对应的管理员对象 * @param userName 帐号 * @return 管理员实体 */ public AdminInfo findByAdminCode(String adminCode) throws Exception { AdminInfo adminInfo = null; Connection con = null; PreparedStatement stmt = null; ResultSet rs = null; try { con = DBUtil.getConnection(); stmt = con.prepareStatement("select * from admin_info where admin_code=?"); stmt.setString(1, adminCode); rs = stmt.executeQuery(); if (rs.next()) { adminInfo = new AdminInfo(); adminInfo.setAdminId(rs.getInt("admin_id")); adminInfo.setAdminCode(rs.getString("admin_code")); adminInfo.setPassword(rs.getString("password")); adminInfo.setName(rs.getString("name")); adminInfo.setTelephone(rs.getString("telephone")); adminInfo.setEmail(rs.getString("email")); adminInfo.setEnrolldate(rs.getDate("enrolldate")); } } catch (Exception e) { e.printStackTrace(); throw e; } finally { DBUtil.close(con); } return adminInfo; } }
DBUtil类的完整代码如下所示:
package dao; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; /** * JDBC管理连接的工具类,可以获取连接和关闭连接 */ public class DBUtil { /** * 获取连接对象 */ public static Connection getConnection()throws Exception{ Connection conn = null; try { Class.forName("oracle.jdbc.OracleDriver"); conn = DriverManager.getConnection( "jdbc:oracle:thin:@localhost:1521:orcl", "scott", "tiger"); } catch (Exception e) { e.printStackTrace(); throw e; } return conn; } /** * 关闭连接对象 */ public static void close(Connection conn) throws Exception{ if(conn!=null){ try { conn.close(); } catch (SQLException e) { e.printStackTrace(); throw e; } } } public static void main(String[] args) throws Exception{ System.out.println(getConnection()); } }
Account类的完整代码如下所示:
package entity; publicclass Account { privateint accountId; private String realName; private String idcardNo; private String loginName; private String status; private String telephone; publicint getAccountId() { return accountId; } publicvoid setAccountId(int accountId) { this.accountId = accountId; } public String getRealName() { return realName; } publicvoid setRealName(String realName) { this.realName = realName; } public String getIdcardNo() { return idcardNo; } publicvoid setIdcardNo(String idcardNo) { this.idcardNo = idcardNo; } public String getLoginName() { return loginName; } publicvoid setLoginName(String loginName) { this.loginName = loginName; } public String getStatus() { return status; } publicvoid setStatus(String status) { this.status = status; } public String getTelephone() { return telephone; } publicvoid setTelephone(String telephone) { this.telephone = telephone; } }
AdminInfo类的完整代码如下所示:
package entity; import java.util.Date; publicclass AdminInfo { privateint adminId; private String adminCode; private String password; private String name; private String telephone; private String email; private Date enrolldate; publicint getAdminId() { return adminId; } @Override public String toString() { return "AdminInfo [adminCode=" + adminCode + ", adminId=" + adminId + ", email=" + email + ", enrolldate=" + enrolldate + ", name=" + name + ", password=" + password + ", telephone=" + telephone + "]"; } publicvoid setAdminId(int adminId) { this.adminId = adminId; } public String getAdminCode() { return adminCode; } publicvoid setAdminCode(String adminCode) { this.adminCode = adminCode; } public String getPassword() { return password; } publicvoid setPassword(String password) { this.password = password; } public String getName() { return name; } publicvoid setName(String name) { this.name = name; } public String getTelephone() { return telephone; } publicvoid setTelephone(String telephone) { this.telephone = telephone; } public String getEmail() { return email; } publicvoid setEmail(String email) { this.email = email; } public Date getEnrolldate() { return enrolldate; } publicvoid setEnrolldate(Date enrolldate) { this.enrolldate = enrolldate; } }
ActionServlet类的完整代码如下所示:
package web; import java.io.IOException; import java.io.PrintWriter; import java.util.List; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import dao.AccountDAO; import dao.AdminInfoDAO; import entity.Account; import entity.AdminInfo; public class ActionServlet extends HttpServlet { @Override protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("UTF-8"); response.setContentType("text/html;charset=UTF-8"); PrintWriter out = response.getWriter(); // 获取请求资源路径 String uri = request.getRequestURI(); // 获取请求资源路径中除应用名以外的部分 String action = uri.substring(uri.lastIndexOf("/") + 1, uri .lastIndexOf(".")); AccountDAO dao = new AccountDAO(); if (action.equals("list")) { try { List<Account> accounts = dao.findAll(); request.setAttribute("accounts", accounts); request.getRequestDispatcher("listAccount.jsp").forward(request, response); } catch (Exception e) { e.printStackTrace(); out.print("系统繁忙"); } } else if (action.equals("add")) { String realName = request.getParameter("realName"); String idcardNo = request.getParameter("idcardNo"); String loginName = request.getParameter("loginName"); String telephone = request.getParameter("telephone"); try { Account account = new Account(); account.setRealName(realName); account.setIdcardNo(idcardNo); account.setLoginName(loginName); account.setStatus("0"); account.setTelephone(telephone); dao.save(account); response.sendRedirect("list.do"); } catch (Exception e) { e.printStackTrace(); out.print("系统繁忙"); } } else if (action.equals("delete")) { String accountId = request.getParameter("accountId"); try { dao.delete(Integer.parseInt(accountId)); response.sendRedirect("list.do"); } catch (Exception e) { e.printStackTrace(); out.print("系统繁忙"); } } else if (action.equals("load")) { String accountId = request.getParameter("accountId"); try { Account acc = dao.findById((Integer.parseInt(accountId))); request.setAttribute("account", acc); request.getRequestDispatcher("modifyAccount.jsp").forward(request, response); } catch (Exception e) { e.printStackTrace(); out.print("系统繁忙"); } } else if (action.equals("modify")) { String accountId = request.getParameter("accountId"); String realName = request.getParameter("realName"); String idcardNo = request.getParameter("idcardNo"); String loginName = request.getParameter("loginName"); String telephone = request.getParameter("telephone"); try { Account account = new Account(); account.setAccountId(Integer.parseInt(accountId)); account.setRealName(realName); account.setIdcardNo(idcardNo); account.setLoginName(loginName); account.setTelephone(telephone); dao.modify(account); response.sendRedirect("list.do"); } catch (Exception e) { e.printStackTrace(); out.print("系统繁忙"); } }else if (action.equals("login")) { String adminCode = request.getParameter("adminCode"); String password = request.getParameter("password"); AdminInfoDAO adminInfoDAO = new AdminInfoDAO(); try { AdminInfo adminInfo = adminInfoDAO.findByAdminCode(adminCode); if (adminInfo == null || !adminInfo.getPassword().equals(password)) { request.setAttribute("login_err", "用户名或密码错误"); request.getRequestDispatcher("login.jsp").forward( request, response); } else { response.sendRedirect("list.do"); } } catch (Exception e) { e.printStackTrace(); throw new ServletException(e); } } } }
addAccount.jsp文件的代码如下所示:
<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>达内-NetCTOSS</title> <link type="text/css" rel="stylesheet" media="all" href="styles/global.css" /> <link type="text/css" rel="stylesheet" media="all" href="styles/global_color.css" /> <script language="javascript" type="text/javascript"> //保存成功的提示信息 functionshowResult() { showResultDiv(true); window.setTimeout("showResultDiv(false);", 3000); } functionshowResultDiv(flag) { var divResult = document.getElementById("save_result_info"); if (flag) divResult.style.display = "block"; else divResult.style.display = "none"; } //显示选填的信息项 functionshowOptionalInfo(imgObj) { var div = document.getElementById("optionalInfo"); if (div.className == "hide") { div.className = "show"; imgObj.src = "../images/hide.png"; } else { div.className = "hide"; imgObj.src = "../images/show.png"; } } </script> </head> <body> <!--Logo区域开始--> <div id="header"> <img src="../images/logo.png" alt="logo" class="left"/> <a href="#">[退出]</a> </div> <!--Logo区域结束--> <!--导航区域开始--> <div id="navi"> <ul id="menu"> <li><a href="../index.html" class="index_off"></a></li> <li><a href="../role/role_list.html" class="role_off"></a></li> <li><a href="../admin/admin_list.html" class="admin_off"></a></li> <li><a href="../fee/fee_list.html" class="fee_off"></a></li> <li><a href="../account/account_list.html" class="account_on"></a></li> <li><a href="../service/service_list.html" class="service_off"></a></li> <li><a href="../bill/bill_list.html" class="bill_off"></a></li> <li><a href="../report/report_list.html" class="report_off"></a></li> <li><a href="../user/user_info.html" class="information_off"></a></li> <li><a href="../user/user_modi_pwd.html" class="password_off"></a></li> </ul> </div> <!--导航区域结束--> <!--主要区域开始--> <div id="main"> <!--保存成功或者失败的提示消息--> <div id="save_result_info" class="save_fail">保存失败,该身份证已经开通过账务账号!</div> <form action="add.do" method="post" class="main_form"> <!--必填项--> <div class="text_info clearfix"><span>姓名:</span></div> <div class="input_info"> <input type="text" name="realName" value="" /> <span class="required">*</span> <div class="validate_msg_long">20长度以内的汉字、字母和数字的组合</div> </div> <div class="text_info clearfix"><span>身份证:</span></div> <div class="input_info"> <input type="text" name="idcardNo" value="" /> <span class="required">*</span> <div class="validate_msg_long">正确的身份证号码格式</div> </div> <div class="text_info clearfix"><span>登录账号:</span></div> <div class="input_info"> <input type="text" name="loginName" value="" /> <span class="required">*</span> <div class="validate_msg_long">30长度以内的字母、数字和下划线的组合</div> </div> <div class="text_info clearfix"><span>电话:</span></div> <div class="input_info"> <input type="text" name="telephone" class="width200"/> <span class="required">*</span> <div class="validate_msg_medium">正确的电话号码格式:手机或固话</div> </div> <!--操作按钮--> <div class="button_info clearfix"> <input type="submit" value="保存" class="btn_save" /> <input type="button" value="取消" class="btn_save" /> </div> </form> </div> <!--主要区域结束--> <div id="footer"> <span>[源自北美的技术,最优秀的师资,最真实的企业环境,最适用的实战项目]</span> <br /> <span>版权所有(C)加拿大达内IT培训集团公司 </span> </div> </body> </html>
listAccount.jsp文件的完整代码如下:
<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ page import="entity.*,java.util.*"%> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>达内-NetCTOSS</title> <link type="text/css" rel="stylesheet" media="all" href="styles/global.css" /> <link type="text/css" rel="stylesheet" media="all" href="styles/global_color.css" /> <script language="javascript" type="text/javascript"> //删除 functiondeleteAccount(accountid) { var r = window.confirm("确定要删除此账务账号吗?\r\n删除后将不能恢复,且会删除其下属的所有业务账号。"); document.getElementById("operate_result_info").style.display = "block"; } //开通或暂停 functionsetState() { var r = window.confirm("确定要开通此账务账号吗?"); document.getElementById("operate_result_info").style.display = "block"; } </script> </head> <body> <!--Logo区域开始--> <div id="header"> <img src="../images/logo.png" alt="logo" class="left" /> <a href="#">[退出]</a> </div> <!--Logo区域结束--> <!--导航区域开始--> <div id="navi"> <ul id="menu"> <li> <a href="../index.html" class="index_off"></a> </li> <li> <a href="../role/role_list.html" class="role_off"></a> </li> <li> <a href="../admin/admin_list.html" class="admin_off"></a> </li> <li> <a href="../fee/fee_list.html" class="fee_off"></a> </li> <li> <a href="../account/account_list.html" class="account_on"></a> </li> <li> <a href="../service/service_list.html" class="service_off"></a> </li> <li> <a href="../bill/bill_list.html" class="bill_off"></a> </li> <li> <a href="../report/report_list.html" class="report_off"></a> </li> <li> <a href="../user/user_info.html" class="information_off"></a> </li> <li> <a href="../user/user_modi_pwd.html" class="password_off"></a> </li> </ul> </div> <!--导航区域结束--> <!--主要区域开始--> <div id="main"> <form action="" method=""> <!--查询--> <div class="search_add"> <div> 身份证: <input type="text" value="不验证" class="text_search" /> </div> <div> 姓名: <input type="text" class="width70 text_search" value="不验证" /> </div> <div> 登录名: <input type="text" value="不验证" class="text_search" " /> </div> <div> 状态: <select class="select_search"> <option> 全部 </option> <option> 开通 </option> <option> 暂停 </option> <option> 删除 </option> </select> </div> <div> <input type="button" value="搜索" class="btn_search" /> </div> <input type="button" value="增加" class="btn_add" onclick="location.href='addAccount.jsp';" /> </div> <!--删除等的操作提示--> <div id="operate_result_info" class="operate_success"> <img src="../images/close.png" onclick="this.parentNode.style.display='none';" /> 删除成功,且已删除其下属的业务账号! </div> <!--数据区域:用表格展示数据--> <div id="data"> <table id="datalist"> <tr> <th> 账号ID </th> <th> 姓名 </th> <th class="width150"> 身份证 </th> <th> 登录名 </th> <th> 状态 </th> <th class="width200"></th> </tr> <% List<Account> accounts = (List<Account>)request.getAttribute("accounts"); for (Account account : accounts) { String strStatus = ""; if ("1".equals(account.getStatus())) { strStatus = "暂停"; } elseif ("2".equals(account.getStatus())) { strStatus = "删除"; } else { strStatus = "开通"; } %> <tr> <td><%=account.getAccountId()%></td> <td><a href="account_detail.html"><%=account.getRealName()%></a></td> <td><%=account.getIdcardNo()%></td> <td><%=account.getLoginName()%></td> <td><%=strStatus%></td> <td class="td_modi"> <input type="button" value="暂停" class="btn_pause" onclick="setState();" /> <input type="button" value="修改" class="btn_modify" onclick="location.href='load.do?accountId=<%=account.getAccountId() %>'" /> <input type="button" value="删除" class="btn_delete" onclick="location.href='delete.do?accountId=<%=account.getAccountId() %>'" /> </td> </tr> <% } %> </table> </div> <!--分页--> <div id="pages"> <a href="#">首页</a> <a href="#">上一页</a> <a href="#" class="current_page">1</a> <a href="#">2</a> <a href="#">3</a> <a href="#">4</a> <a href="#">5</a> <a href="#">下一页</a> <a href="#">末页</a> </div> </form> </div> <!--主要区域结束--> <div id="footer"> <p> [源自北美的技术,最优秀的师资,最真实的企业环境,最适用的实战项目] </p> <p> 版权所有(C)加拿大达内IT培训集团公司 </p> </div> </body> </html>
modifyAccount.jsp文件的完整代码如下:
<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ page import="entity.*,java.util.*"%> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>达内-NetCTOSS</title> <link type="text/css" rel="stylesheet" media="all" href="styles/global.css" /> <link type="text/css" rel="stylesheet" media="all" href="styles/global_color.css" /> <script language="javascript" type="text/javascript"> //保存成功的提示信息 functionshowResult() { showResultDiv(true); window.setTimeout("showResultDiv(false);", 3000); } functionshowResultDiv(flag) { var divResult = document.getElementById("save_result_info"); if (flag) divResult.style.display = "block"; else divResult.style.display = "none"; } //显示修改密码的信息项 functionshowPwd(chkObj) { if (chkObj.checked) document.getElementById("divPwds").style.display = "block"; else document.getElementById("divPwds").style.display = "none"; } </script> </head> <body> <!--Logo区域开始--> <div id="header"> <img src="../images/logo.png" alt="logo" class="left" /> <a href="#">[退出]</a> </div> <!--Logo区域结束--> <!--导航区域开始--> <div id="navi"> <ul id="menu"> <li> <a href="../index.html" class="index_off"></a> </li> <li> <a href="../role/role_list.html" class="role_off"></a> </li> <li> <a href="../admin/admin_list.html" class="admin_off"></a> </li> <li> <a href="../fee/fee_list.html" class="fee_off"></a> </li> <li> <a href="../account/account_list.html" class="account_on"></a> </li> <li> <a href="../service/service_list.html" class="service_off"></a> </li> <li> <a href="../bill/bill_list.html" class="bill_off"></a> </li> <li> <a href="../report/report_list.html" class="report_off"></a> </li> <li> <a href="../user/user_info.html" class="information_off"></a> </li> <li> <a href="../user/user_modi_pwd.html" class="password_off"></a> </li> </ul> </div> <!--导航区域结束--> <!--主要区域开始--> <div id="main"> <!--保存成功或者失败的提示消息--> <div id="save_result_info" class="save_fail"> 保存失败,该身份证已经开通过账务账号! </div> <% Account acc=(Account)request.getAttribute("account"); %> <form action="modify.do" method="post" class="main_form"> <!--必填项--> <div class="text_info clearfix"> <span>账务账号ID:</span> </div> <div class="input_info"> <input type="text" name="accountId" value="<%=acc.getAccountId() %>"readonly class="readonly" /> </div> <div class="text_info clearfix"> <span>姓名:</span> </div> <div class="input_info"> <input type="text" name="realName" value="<%=acc.getRealName() %>" /> <span class="required">*</span> <div class="validate_msg_long"> 20长度以内的汉字、字母和数字的组合 </div> </div> <div class="text_info clearfix"> <span>身份证:</span> </div> <div class="input_info"> <input type="text" name="idcardNo" value="<%=acc.getIdcardNo() %>" /> <span class="required">*</span> <div class="validate_msg_long"> 正确的身份证号码格式 </div> </div> <div class="text_info clearfix"> <span>登录账号:</span> </div> <div class="input_info"> <input type="text" name="loginName" value="<%=acc.getLoginName() %>" /> <span class="required">*</span> <div class="validate_msg_long"> 30长度以内的字母、数字和下划线的组合 </div> </div> <div class="text_info clearfix"> <span>电话:</span> </div> <div class="input_info"> <input type="text" name="telephone" class="width200" value="<%=acc.getTelephone() %>" /> <span class="required">*</span> <div class="validate_msg_medium"> 正确的电话号码格式:手机或固话 </div> </div> <!--操作按钮--> <div class="button_info clearfix"> <input type="submit" value="保存" class="btn_save" onclick="showResult();" /> <input type="button" value="取消" class="btn_save" /> </div> </form> </div> <!--主要区域结束--> <div id="footer"> <span>[源自北美的技术,最优秀的师资,最真实的企业环境,最适用的实战项目]</span> <br /> <span>版权所有(C)加拿大达内IT培训集团公司 </span> </div> </body> </html>
login.jsp完整代码如下所示:
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>达内-NetCTOSS</title> <link type="text/css" rel="stylesheet" media="all" href="styles/global.css" /> <link type="text/css" rel="stylesheet" media="all" href="styles/global_color.css" /> </head> <body class="index"> <div class="login_box"> <form name="login" action="login.do" method="post"> <table> <tr> <td class="login_info"> 账号: </td> <td colspan="2"> <input name="adminCode" type="text" class="width150" /> </td> </tr> <tr> <td class="login_info"> 密码: </td> <td colspan="2"> <input name="password" type="password" class="width150" /> </td> </tr> <tr> <td></td> <td class="login_button" > <a href="javascript:document.login.submit();"><img src="images/login_btn.png" /> </a> </td> <td> <span class="required"> <% String errorMsg = (String) request.getAttribute("login_err"); %><span style="color: red;"><%=errorMsg == null ? "" : errorMsg%></span> </span> </td> </tr> <tr> </tr> </table> </form> </div> </body> </html>
创建数据库表、序列以及插入语句的完整代码如下:
create table admin_info( admin_id number(4) primary key not null, admin_code varchar2(30) unique not null, password varchar2(30) not null, name varchar2(30) not null, telephone varchar2(15), email varchar2(50), enrolldate date default sysdate not null ); create sequence admin_id_seq increment by 1 start with 1; insert into admin_info values(888,'admin','admin','LiHonghe','123456789','lihh@tarena.com.cn',sysdate); insert into admin_info values(999,'syl','syl123','LiuBei','123456789','liubei@tarena.com.cn',sysdate); select * from admin_info;
web.xml文件的完整代码如下:
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <servlet> <servlet-name>actionServlet</servlet-name> <servlet-class>web.ActionServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>actionServlet</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> </web-app>