1、流程分析javascript
1.後臺頁面和模塊css
2.管理員html
2、代碼java
1.view層jquery
(1)loigin.jspsql
1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> 2 <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> 3 4 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 5 <html> 6 <head> 7 <title>管理員登陸頁面</title> 8 9 <meta http-equiv="pragma" content="no-cache"> 10 <meta http-equiv="cache-control" content="no-cache"> 11 <meta http-equiv="expires" content="0"> 12 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> 13 <meta http-equiv="description" content="This is my page"> 14 <!-- 15 <link rel="stylesheet" type="text/css" href="styles.css"> 16 --> 17 <script type="text/javascript" src="<c:url value='/jquery/jquery-1.5.1.js'/>"></script> 18 <script type="text/javascript"> 19 function checkForm() { 20 if(!$("#adminname").val()) { 21 alert("管理員名稱不能爲空!"); 22 return false; 23 } 24 if(!$("#adminpwd").val()) { 25 alert("管理員密碼不能爲空!"); 26 return false; 27 } 28 return true; 29 } 30 </script> 31 </head> 32 33 <body> 34 <h1>管理員登陸頁面</h1> 35 <hr/> 36 <p style="font-weight: 900; color: red">${msg }</p> 37 <form action="<c:url value='/AdminServlet'/>" method="post" onsubmit="return checkForm()"> 38 <input type="hidden" name="method" value="login"/> 39 管理員帳戶:<input type="text" name="adminname" value="" id="adminname"/><br/> 40 密 碼:<input type="password" name="adminpwd" id="adminpwd"/><br/> 41 <input type="submit" value="進入後臺"/> 42 </form> 43 </body> 44 </html>
2.servlet層jsp
(1)AdminServlet.javapost
1 public class AdminServlet extends BaseServlet { 2 private AdminService service = new AdminService(); 3 4 public String login(HttpServletRequest req, HttpServletResponse resp) 5 throws ServletException, IOException { 6 Admin formAdmin = CommonUtils.toBean(req.getParameterMap(), Admin.class); 7 Admin admin = service.login(formAdmin); 8 if(admin == null) { 9 req.setAttribute("code", "error"); 10 req.setAttribute("msg", "用戶名或密碼錯誤!"); 11 return "f:/adminjsps/login.jsp"; 12 } 13 req.getSession().setAttribute("admin", admin); 14 return "f:/adminjsps/admin/index.jsp"; 15 } 16 }
3.service層ui
(1)AdminService.java url
1 public class AdminService { 2 private AdminDao dao = new AdminDao(); 3 4 public Admin login(Admin admin) { 5 try { 6 return dao.findByNameAndPwd(admin.getAdminname(), admin.getAdminpwd()); 7 } catch (SQLException e) { 8 throw new RuntimeException(e); 9 } 10 } 11 12 }
4.dao層
(1)AdminDao.java
1 public class AdminDao { 2 3 private QueryRunner qr = new TxQueryRunner(); 4 5 public Admin findByNameAndPwd(String adminname, String adminpwd) throws SQLException { 6 String sql = "select * from t_admin where adminname=? and adminpwd=?"; 7 Admin admin = qr.query(sql, new BeanHandler<Admin>(Admin.class), adminname, adminpwd); 8 return admin; 9 } 10 }