zoukankan      html  css  js  c++  java
  • model1模式变为mv模式,实现业务逻辑和画面的分离

    增加UserBeanCl类

      1 /**
      2  * 这是一个处理user表的类,操作UserBean
      3  * 业务逻辑在这里
      4  */
      5 package com;
      6 
      7 import java.sql.Connection;
      8 import java.sql.DriverManager;
      9 import java.sql.PreparedStatement;
     10 import java.sql.ResultSet;
     11 import java.sql.SQLException;
     12 import java.util.ArrayList;
     13 
     14 public class UserBeanCl {
     15     // 业务逻辑
     16     private Connection connection = null;
     17     private PreparedStatement preStatement = null;
     18     private ResultSet rs = null;
     19     private int pageCount = 0;
     20 
     21     // 用户验证
     22     public boolean checkUser(String username, String passwd) {
     23         boolean flag = false;
     24         // 得到连接
     25         ConnDB cd = new ConnDB();
     26         connection = cd.getConnection();
     27 
     28         try {
     29             String sql = "select password from db_users where username=?";
     30             preStatement = connection.prepareStatement(sql);
     31             preStatement.setString(1, username);
     32             rs = preStatement.executeQuery();
     33             if (rs.next()) {
     34                 String dpasswd = rs.getString(1);
     35                 if (dpasswd.equals(passwd)) {
     36                     flag = true;
     37                 }
     38             }
     39         } catch (SQLException e) {
     40             // TODO Auto-generated catch block
     41             e.printStackTrace();
     42         } finally {
     43             this.close();
     44         }
     45         return flag;
     46     }
     47 
     48     // 关闭资源
     49     public void close() {
     50         if (rs != null) {
     51             try {
     52                 rs.close();
     53                 rs = null;
     54             } catch (SQLException e) {
     55                 // TODO Auto-generated catch block
     56                 e.printStackTrace();
     57             }
     58         }
     59         if (preStatement != null) {
     60             try {
     61                 preStatement.close();
     62                 preStatement = null;
     63             } catch (SQLException e) {
     64                 // TODO Auto-generated catch block
     65                 e.printStackTrace();
     66             }
     67         }
     68         if (connection != null) {
     69             try {
     70                 connection.close();
     71                 connection = null;
     72             } catch (SQLException e) {
     73                 // TODO Auto-generated catch block
     74                 e.printStackTrace();
     75             }
     76         }
     77     }
     78 
     79     // 分页显示
     80     public ArrayList getResultByPage(int pageNow, int pageSize) {
     81         ArrayList aList = new ArrayList();
     82         int rowCount = 0;
     83         
     84         try {
     85             ConnDB cd = new ConnDB();
     86             connection = cd.getConnection();
     87             preStatement = connection.prepareStatement("select count(*) from db_users");
     88             rs = preStatement.executeQuery();
     89             if (rs.next()) {
     90                 rowCount = rs.getInt(1);
     91             }
     92             // 计算pageCount
     93             if (rowCount % pageSize == 0) {
     94                 pageCount = rowCount / pageSize;
     95             } else {
     96                 pageCount = rowCount / pageSize + 1;
     97             }
     98 
     99             preStatement = connection.prepareStatement("select * from db_users limit ?,?");
    100             // 给?赋值
    101             preStatement.setInt(1, pageSize * (pageNow - 1));
    102             preStatement.setInt(2, pageSize);
    103             rs = preStatement.executeQuery();
    104             
    105             while(rs.next())
    106             {
    107                 // 将rs中的每条记录封装到UserBean中
    108                 UserBean ub = new UserBean();
    109                 ub.setUserId(rs.getInt(1));
    110                 ub.setUserName(rs.getString(2));
    111                 ub.setPassWord(rs.getString(3));
    112                 
    113                 aList.add(ub);
    114             }
    115         } catch (Exception e) {
    116             // TODO Auto-generated catch block
    117             e.printStackTrace();
    118         }finally {
    119             this.close();
    120         }
    121         return aList;
    122     }
    123 
    124     public int getPageCount()
    125     {
    126         return this.pageCount;
    127     }
    128 }

    增加ConnDB类

     1 package com;
     2 
     3 import java.sql.Connection;
     4 import java.sql.DriverManager;
     5 
     6 public class ConnDB {
     7     
     8     private Connection connection=null;
     9     public Connection getConnection()
    10     {
    11         try {
    12             Class.forName("com.mysql.jdbc.Driver");
    13             connection =DriverManager.getConnection("jdbc:mysql://localhost/dbjdbc","root","123456");
    14         } catch (Exception e) {
    15             // TODO Auto-generated catch block
    16             e.printStackTrace();
    17         }
    18         return connection;
    19         
    20     }
    21 
    22 }

    增加UserBean类

     1 package com;
     2 
     3 public class UserBean {
     4     
     5     private int userId;
     6     private String userName;
     7     private String passWord;
     8     public int getUserId() {
     9         return userId;
    10     }
    11     public void setUserId(int userId) {
    12         this.userId = userId;
    13     }
    14     public String getUserName() {
    15         return userName;
    16     }
    17     public void setUserName(String userName) {
    18         this.userName = userName;
    19     }
    20     public String getPassWord() {
    21         return passWord;
    22     }
    23     public void setPassWord(String passWord) {
    24         this.passWord = passWord;
    25     }
    26     
    27 
    28 }

    优化后的LoginCl类

     1 package com;
     2 
     3 import javax.servlet.http.Cookie;
     4 import javax.servlet.http.HttpServlet;
     5 import javax.servlet.http.HttpServletRequest;
     6 import javax.servlet.http.HttpServletResponse;
     7 import javax.servlet.http.HttpSession;
     8 
     9 public class LoginCl extends HttpServlet {
    10 
    11     public void doGet(HttpServletRequest req, HttpServletResponse res) {
    12 
    13         try {
    14             req.setCharacterEncoding("gbk");
    15             res.setContentType("text/html;charset=gbk");
    16 
    17             String username = req.getParameter("username");
    18             String password = req.getParameter("password");
    19             String sex = req.getParameter("sex");
    20             // 用户合法性check
    21             UserBeanCl ubc = new UserBeanCl();
    22             if (ubc.checkUser(username, password)) {
    23                 // 用户合法
    24                 // 得到Session
    25                 HttpSession hs = req.getSession(true);
    26                 // 修改Session存在时间
    27                 hs.setMaxInactiveInterval(30);
    28                 // Session添加属性pass
    29                 hs.setAttribute("pass", "ok");
    30 
    31                 String keep = req.getParameter("keep");
    32                 if (keep != null) {
    33                     // 创建Cookie
    34                     Cookie name = new Cookie("myname", username);
    35                     Cookie pass = new Cookie("mypass", password);
    36                     // 设置时间
    37                     name.setMaxAge(14 * 24 * 3600);
    38                     pass.setMaxAge(14 * 24 * 3600);
    39                     // 回写到客户端
    40                     res.addCookie(name);
    41                     res.addCookie(pass);
    42                 }
    43                 res.sendRedirect("welcome?uname=" + username + "&upassw=" + password + "&sx=" + sex);
    44             } else {
    45                 // 说明密码错误
    46                 res.sendRedirect("login?info=error1");
    47             }
    48         } catch (Exception e) {
    49             // TODO Auto-generated catch block
    50             e.printStackTrace();
    51         }
    52 
    53     }
    54 
    55     public void doPost(HttpServletRequest req, HttpServletResponse res) {
    56         this.doGet(req, res);
    57     }
    58 
    59 }

    优化后的Welcome类

      1 package com;
      2 
      3 import java.io.IOException;
      4 import java.io.PrintWriter;
      5 import java.sql.Connection;
      6 import java.sql.DriverManager;
      7 import java.sql.PreparedStatement;
      8 import java.sql.ResultSet;
      9 import java.sql.Statement;
     10 import java.util.ArrayList;
     11 import java.util.concurrent.CountDownLatch;
     12 
     13 import javax.servlet.http.Cookie;
     14 import javax.servlet.http.HttpServlet;
     15 import javax.servlet.http.HttpServletRequest;
     16 import javax.servlet.http.HttpServletResponse;
     17 import javax.servlet.http.HttpSession;
     18 
     19 import com.sun.corba.se.impl.encoding.CodeSetConversion.CTBConverter;
     20 import com.sun.org.apache.bcel.internal.generic.Select;
     21 
     22 import javafx.css.PseudoClass;
     23 
     24 public class Welcome extends HttpServlet {
     25 
     26     public void doGet(HttpServletRequest req, HttpServletResponse res) {
     27 
     28         try {
     29             req.setCharacterEncoding("gbk");
     30             res.setContentType("text/html;charset=gbk");
     31             HttpSession hs = req.getSession(true);
     32             String val = (String) hs.getAttribute("pass");
     33             String sessionId = hs.getId();
     34             String name = "";
     35             String passwd = "";
     36             if (val == null) {
     37                 // 如果Session中没有用户信息,再看看有没有Cookie信息
     38                 // 从客户端读取Cookie信息
     39                 Cookie[] allCookies = req.getCookies();
     40                 int i = 0;
     41                 // 如果allCookies不为空
     42                 if (allCookies != null) {
     43                     // 从中取出Cookie
     44                     for (i = 0; i < allCookies.length; i++) {
     45                         // 依次取出Cookie
     46                         Cookie tempCookie = allCookies[i];
     47                         if (tempCookie.getName().equals("myname")) {
     48                             name = tempCookie.getValue();
     49                         } else if (tempCookie.getName().equals("mypass")) {
     50                             passwd = tempCookie.getValue();
     51                         }
     52                     }
     53                     if (!name.equals("") && !passwd.equals("")) {
     54 
     55                         // System.out.println("准备跳转logincl页面,username="+name+",password="+passwd);
     56                         res.sendRedirect("logincl?username=" + name + "&password=" + passwd);
     57                     }
     58                 }
     59             }
     60 
     61             //res.sendRedirect("login?info=error1");
     62             String u = req.getParameter("uname");
     63             String p = req.getParameter("upassw");
     64             String s = req.getParameter("sx");
     65 
     66             PrintWriter pw = res.getWriter();
     67             pw.println("<body><center>");
     68             pw.println("<img src=imgs/521.jpg><br>");
     69             pw.println("welcom to my world,u=" + u + ",p=" + p + ",s=" + s + ",sessionId=" + sessionId);
     70 
     71             // ------------------开始分页--------------------
     72             int pageSize = 3;
     73             int pageNow = 1;
     74             String sPageNow = req.getParameter("pageNow");
     75             if (sPageNow != null) {
     76                 pageNow = Integer.parseInt(sPageNow);
     77             }
     78 
     79             // 调用分页函数
     80             UserBeanCl ubc = new UserBeanCl();
     81             ArrayList arrayList = ubc.getResultByPage(pageNow, pageSize);
     82 
     83             // 显示结果
     84             // 显示表头
     85             pw.println("<table border=1>");
     86             pw.println("<tr><th>id</th><th>username</th><th>password</th></tr>");
     87             for (int i = 0; i < arrayList.size(); i++) {
     88                 UserBean ub = (UserBean) arrayList.get(i);
     89                 pw.println("<tr>");
     90                 pw.println("<td>" + ub.getUserId() + "</td>");
     91                 pw.println("<td>" + ub.getUserName() + "</td>");
     92                 pw.print("<td>" + ub.getPassWord() + "</td>");
     93                 pw.println("</tr>");
     94             }
     95             pw.println("</table>");
     96             // 显示上一页
     97             if (pageNow != 1) {
     98                 pw.println("<a href=welcome?pageNow=" + (pageNow - 1) + ">" + "上一页</a>");
     99             }
    100 
    101             // 显示超链接
    102             for (int i = pageNow; i <= ubc.getPageCount(); i++) {
    103                 pw.println("<a href=welcome?pageNow=" + i + ">" + i + "</a>");
    104             }
    105             if (pageNow != ubc.getPageCount()) {
    106                 pw.println("<a href=welcome?pageNow=" + (pageNow + 1) + ">" + "下一页</a>");
    107             }
    108             pw.println("</center></body>");
    109         } catch (Exception e) {
    110             // TODO: handle exception
    111             e.printStackTrace();
    112         }
    113 
    114     }
    115 
    116     public void doPost(HttpServletRequest req, HttpServletResponse res) {
    117         this.doGet(req, res);
    118     }
    119 
    120 }
  • 相关阅读:
    51nod1260
    51nod1327
    51nod1342
    51nod1479
    LOJ6088
    51nod1634
    51nod1778
    JAVA循环结构学校上机经常遇到的几题 笔记
    B. The Number of Products(Codeforces Round #585 (Div. 2))
    A. Yellow Cards ( Codeforces Round #585 (Div. 2) 思维水题
  • 原文地址:https://www.cnblogs.com/buxingzhelyd/p/7376388.html
Copyright © 2011-2022 走看看