zoukankan      html  css  js  c++  java
  • 基于servlet和jsp的简单注册登录页面(包括:增删查改及分页显示)

    一共建了8个包:分别是com.etc.biz,

               com.etc.dao,

               com.etc.dao.impl,

               com.etc.entity,

               com.etc.rowmapping,

               com.etc.servlet,

               com.etc.svc,

               com.etc.tools

    com.etc.biz
     1 package com.etc.biz;
     2 
     3 import java.sql.SQLException;
     4 import java.util.List;
     5 import com.etc.dao.UserDaoI;
     6 import com.etc.dao.impl.UserDaoImpl;
     7 import com.etc.entity.Page;
     8 import com.etc.entity.User;
     9 
    10 
    11 public class UserBiz {
    12 
    13     UserDaoI udi = new UserDaoImpl();
    14     public boolean save(User user) throws SQLException
    15     {
    16         User u = udi.getUserByUname(user.getUsername());
    17         if(u!=null)
    18             throw new RuntimeException("用户已存在");
    19         int re = udi.insert(user);
    20         return re>0?true:false;
    21     }
    22     
    23     public boolean login(String username, String password) throws SQLException
    24     {
    25         User u = udi.getUserByUname(username);
    26         if(u==null)
    27         {
    28             throw new RuntimeException("该用户名不存在");
    29         }
    30         else
    31         {
    32             if(udi.login(username, password)!=null)
    33             {
    34                 return true;
    35             }
    36             else
    37             {
    38                 throw new RuntimeException("密码错误");
    39             }
    40             
    41         }
    42     }
    43     
    44     public boolean delUser(String username,String password) throws SQLException{
    45         User u = udi.getUserByUname(username);
    46         if(!u.getPassword().equals(password))
    47             throw new RuntimeException("无法删除他人信息");
    48         udi.remove(username);
    49         return true;
    50     }
    51     
    52     public boolean updUser(User user) throws SQLException{
    53         int i = udi.update(user);
    54         if(i>0)
    55             return true;
    56         throw new RuntimeException("无法修改他人信息");
    57         
    58     }
    59     
    60     public boolean modpwd(String username,String opwd,String npwd) throws SQLException{
    61         User temp = udi.getUserByUname(username);
    62         if(temp==null)
    63             throw new RuntimeException("该用户不存在");
    64         if(!opwd.equals(temp.getPassword()))
    65             throw new RuntimeException("用户密码错误");
    66         temp.setPassword(npwd);
    67         int i = udi.update(temp);
    68         return i>0?true:false;
    69     }
    70     
    71     public List<User> getAllUser(Page page) throws SQLException{ 
    72         return udi.getAllUser(page);
    73     }
    74 
    75     public List<User> getUser(String username,Page page) throws SQLException{
    76         return udi.getUserBy(username,page);
    77     }
    78     
    79     public int getCount() throws SQLException
    80     {
    81         int i = udi.getCountOfUser();
    82         if(i>0)
    83             return i;
    84         throw new RuntimeException("表中无数据");
    85     }
    86     
    87     public int queryCount(String username) throws SQLException
    88     {
    89         int i = udi.queryCountOfUser(username);
    90         if(i>0)
    91             return i;
    92         throw new RuntimeException("表中无数据");
    93     }
    94 }
    com.etc.dao
     1 package com.etc.dao;
     2 
     3 import java.sql.SQLException;
     4 import java.util.List;
     5 
     6 import com.etc.entity.Page;
     7 import com.etc.entity.User;
     8 
     9 public interface UserDaoI {
    10 
    11     public int insert(User user) throws SQLException;
    12     
    13     public int update(User user) throws SQLException;
    14     
    15     public void remove(String username) throws SQLException;
    16     
    17     public List<User> getAllUser(Page page) throws SQLException;
    18     
    19     public List<User> getUserBy(String username,Page page) throws SQLException;
    20     
    21     public User getUserByUname(String uname) throws SQLException;
    22     
    23     public User login(String username, String password) throws SQLException;
    24     
    25     public int getCountOfUser() throws SQLException;
    26     
    27     public int queryCountOfUser(String username) throws SQLException;
    28 }
    com.etc.dao.impl
      1 package com.etc.dao.impl;
      2 
      3 import java.sql.PreparedStatement;
      4 import java.sql.ResultSet;
      5 import java.sql.SQLException;
      6 import java.util.ArrayList;
      7 import java.util.List;
      8 
      9 import com.etc.dao.UserDaoI;
     10 import com.etc.entity.Page;
     11 import com.etc.entity.User;
     12 import com.etc.rowmapping.UserMapping;
     13 import com.etc.tools.MyDBConnection;
     14 
     15 public class UserDaoImpl implements UserDaoI{
     16 
     17     public int insert(User user) throws SQLException
     18     {
     19         String sql = "insert into user" +
     20             "(username,password,name,sex,major,college,phone)values"
     21             + "('"+user.getUsername()
     22             + "','"+user.getPassword()
     23             + "','"+user.getName()
     24             + "','"+user.getSex()
     25             + "','"+user.getMajor()
     26             + "','"+user.getCollege()
     27             + "','"+user.getPhone()+"')";
     28         return MyDBConnection.ExecSQL(sql);    
     29 
     30     }
     31     
     32     public int update(User user) throws SQLException
     33     {
     34         int i = 0;
     35         String sql = "update user set name=?,sex=?,major=?,college=?,phone=? where username=? and password=?";
     36         PreparedStatement pstmt = MyDBConnection.getPrepareStatement(sql);
     37         pstmt.setString(1, user.getName());
     38         pstmt.setString(2, user.getSex());
     39         pstmt.setString(3, user.getMajor());
     40         pstmt.setString(4, user.getCollege());
     41         pstmt.setString(5, user.getPhone());
     42         pstmt.setString(6, user.getUsername());
     43         pstmt.setString(7, user.getPassword());
     44         i = pstmt.executeUpdate();
     45         return i;
     46     }
     47     
     48     public void remove(String username) throws SQLException
     49     {
     50         String sql = "delete from user where username=?";
     51         PreparedStatement pstmt = MyDBConnection.getPrepareStatement(sql);
     52         pstmt.setString(1, username);
     53         pstmt.executeUpdate();
     54         
     55 //        String sql = "delete from user where username="+username;
     56 //        int resulte = MyDBConnection.ExecSQL(sql);
     57     }
     58     
     59     public List<User> getAllUser(Page page) throws SQLException//传入一个page参数
     60     {
     61         //String sql ="select * from user limit "+0+","+2;   limit page.getfirstrow(),page.getRecordofpage()
     62         String sql ="select * from user limit "+ page.getFirstrow()+","+page.getRecordofpage();
     63         ResultSet rs = MyDBConnection.ExecQuery(sql);
     64         List<User> users = new ArrayList<User>();
     65         while(rs.next()){
     66             users.add(UserMapping.getUser(rs));
     67         }
     68         return users;
     69     }
     70     
     71     public List<User> getUserBy(String username,Page page) throws SQLException
     72     {
     73         String sql ="select * from user where username like"+"'%"+username+"%'" + "limit " + page.getFirstrow()+","+page.getRecordofpage();
     74         ResultSet rs = MyDBConnection.ExecQuery(sql);
     75         List<User> users = new ArrayList<User>();
     76         while(rs.next()){
     77             users.add(UserMapping.getUser(rs));
     78         }
     79         return users;
     80     }
     81     
     82     public User getUserByUname(String uname) throws SQLException
     83     {
     84         String sql ="select * from user where username=?";
     85         PreparedStatement pstmt=  MyDBConnection.getPrepareStatement(sql);
     86         pstmt.setString(1, uname);
     87         ResultSet rs = pstmt.executeQuery();
     88         User user = null;
     89         if(rs.next()){
     90             user = UserMapping.getUser(rs);
     91         }
     92         return user;
     93     }
     94     
     95     public User login(String username, String password) throws SQLException
     96     {
     97         
     98         String sql = "select * from user where username=? and password=?";
     99         PreparedStatement pstmt = MyDBConnection.getPrepareStatement(sql);
    100         pstmt.setString(1, username);
    101         pstmt.setString(2, password);
    102         ResultSet rs = pstmt.executeQuery();
    103         User user = null;
    104         if(rs.next())
    105         {
    106             user = UserMapping.getUser(rs);
    107         }
    108         return user;
    109     }
    110     
    111     public int getCountOfUser() throws SQLException
    112     {
    113         String sql = "select count(*) num from user";
    114         PreparedStatement pstmt = MyDBConnection.getPrepareStatement(sql);
    115         ResultSet rs = pstmt.executeQuery();
    116         int user = 0;
    117 
    118         if(rs.next())
    119         {
    120             user = rs.getInt("num");
    121 
    122         }
    123         return user;
    124     }
    125     
    126     public int queryCountOfUser(String username) throws SQLException
    127     {
    128         String sql = "select count(*) num from user where username like"+"'%"+username+"%'";
    129         PreparedStatement pstmt = MyDBConnection.getPrepareStatement(sql);
    130         ResultSet rs = pstmt.executeQuery();
    131         int user = 0;
    132 
    133         if(rs.next())
    134         {
    135             user = rs.getInt("num");
    136 
    137         }
    138         return user;
    139     }
    140 }

    com.etc.entity包下两个类:

    Page.java
     1 package com.etc.entity;
     2 
     3 public class Page {
     4 
     5     private int totalrecordcount;  //总条数select count(*) from user;
     6     private int recordofpage=5;//每页条数
     7     private int totalpage;//总页数
     8     private int currentpage;//当前页数  int page = 1;
     9                                     //try{
    10                                         //integet.parseint(request.getparameter("page"));
    11                                     //}catch()
    12                                     
    13     private int firstpage=1;//第一页 1
    14     private int previouspage;//上一页get(currentpage)-1
    15     private int nextpage;//下一页  get(currentpage)+1
    16     private int firstrow;//从第几条记录开始显示currentpage*recordofpage
    17     public int getTotalrecordcount() {
    18         return totalrecordcount;
    19     }
    20     public void setTotalrecordcount(int totalrecordcount) {
    21         this.totalrecordcount = totalrecordcount;
    22     }
    23     public int getRecordofpage() {
    24         return recordofpage;
    25     }
    26     public void setRecordofpage(int recordofpage) {
    27         this.recordofpage = recordofpage;
    28     }
    29     public int getTotalpage() {
    30         if(totalrecordcount%recordofpage==0)
    31             return totalrecordcount/recordofpage;
    32         else
    33             return totalrecordcount/recordofpage+1;
    34     }
    35     public void setTotalpage(int totalpage) {
    36         this.totalpage = totalpage;
    37     }
    38     public int getCurrentpage() {
    39         return currentpage;
    40     }
    41     public void setCurrentpage(int currentpage) {
    42         this.currentpage = currentpage;
    43     }
    44     public int getFirstpage() {
    45         return firstpage;
    46     }
    47     public void setFirstpage(int firstpage) {
    48         this.firstpage = firstpage;
    49     }
    50     public int getPreviouspage() {
    51         return previouspage;
    52     }
    53     public void setPreviouspage(int previouspage) {
    54         this.previouspage = previouspage;
    55     }
    56     public int getNextpage() {
    57         return nextpage;
    58     }
    59     public void setNextpage(int nextpage) {
    60         this.nextpage = nextpage;
    61     }
    62     public int getFirstrow() {
    63         return firstrow;
    64     }
    65     public void setFirstrow(int fitstrow) {
    66         this.firstrow = fitstrow;
    67     }
    68     public Page(int totalrecordcount, int recordofpage, int totalpage,
    69             int currentpage, int firstpage, int previouspage,
    70             int nextpage, int firstrow) {
    71         super();
    72         this.totalrecordcount = totalrecordcount;
    73         this.recordofpage = recordofpage;
    74         this.totalpage = totalpage;
    75         this.currentpage = currentpage;
    76         this.firstpage = firstpage;
    77         this.previouspage = previouspage;
    78         this.nextpage = nextpage;
    79         this.firstrow = firstrow;
    80     }
    81     
    82     
    83 }
    User.java
      1 package com.etc.entity;
      2 
      3 
      4 public class User {
      5 
      6     private int id;
      7     private String username;
      8     private String password;
      9     private String name;
     10     private String sex;
     11     private String major;
     12     private String college;
     13     private String phone;
     14     
     15 //    private Collection<User> objs;//从数据库中读的集合
     16 //    private int totalCount;//总的条数
     17 //    private int pageNo;//当前的页数
     18 //    private int pageCount;//每页的条数
     19     
     20     
     21     
     22     
     23     public int getId() {
     24         return id;
     25     }
     26     public void setId(int id) {
     27         this.id = id;
     28     }
     29     public String getUsername() {
     30         return username;
     31     }
     32     public void setUsername(String username) {
     33         this.username = username;
     34     }
     35     public String getPassword() {
     36         return password;
     37     }
     38     public void setPassword(String password) {
     39         this.password = password;
     40     }
     41     public String getName() {
     42         return name;
     43     }
     44     public void setName(String name) {
     45         this.name = name;
     46     }
     47     public String getSex() {
     48         return sex;
     49     }
     50     public void setSex(String sex) {
     51         this.sex = sex;
     52     }
     53     public String getMajor() {
     54         return major;
     55     }
     56     public void setMajor(String major) {
     57         this.major = major;
     58     }
     59     public String getCollege() {
     60         return college;
     61     }
     62     public void setCollege(String college) {
     63         this.college = college;
     64     }
     65     public String getPhone() {
     66         return phone;
     67     }
     68     public void setPhone(String phone) {
     69         this.phone = phone;
     70     }
     71     public User(int id, String username, String password, String name,
     72             String sex, String major, String college, String phone) {
     73         super();
     74         this.id = id;
     75         this.username = username;
     76         this.password = password;
     77         this.name = name;
     78         this.sex = sex;
     79         this.major = major;
     80         this.college = college;
     81         this.phone = phone;
     82     }
     83     public User(String username, String password, String name, String sex,
     84             String major, String college, String phone) {
     85         super();
     86         this.username = username;
     87         this.password = password;
     88         this.name = name;
     89         this.sex = sex;
     90         this.major = major;
     91         this.college = college;
     92         this.phone = phone;
     93     }
     94     
     95     public User(String password,String name, String sex, String major,
     96             String college, String phone) {
     97         super();
     98         this.password = password;
     99         this.name = name;
    100         this.sex = sex;
    101         this.major = major;
    102         this.college = college;
    103         this.phone = phone;
    104     }
    105     public User(String username, String password) {
    106         super();
    107         this.username = username;
    108         this.password = password;
    109     }
    110     public User() {
    111         super();
    112     }
    113     
    114     
    115     
    116     
    117     
    118     
    119     
    120 }
    com.etc.rowmapping
     1 package com.etc.rowmapping;
     2 
     3 import java.sql.ResultSet;
     4 import java.sql.SQLException;
     5 
     6 import com.etc.entity.User;
     7 
     8 public class UserMapping {
     9 
    10     public static User getUser(ResultSet rs)throws SQLException
    11     {
    12         User user = null;
    13         user = new User();
    14         user.setId(rs.getInt("id"));
    15         user.setUsername(rs.getString("username"));
    16         user.setPassword(rs.getString("password"));
    17         user.setName(rs.getString("name"));
    18         user.setSex(rs.getString("sex"));
    19         user.setMajor(rs.getString("major"));
    20         user.setCollege(rs.getString("college"));
    21         user.setPhone(rs.getString("phone"));
    22         return user;
    23     }
    24 }

     com.etc.servlet包下有6个类:

    DeleteUserServlet
     1 package com.etc.servlet;
     2 
     3 import java.io.IOException;
     4 
     5 import javax.servlet.ServletException;
     6 import javax.servlet.http.HttpServlet;
     7 import javax.servlet.http.HttpServletRequest;
     8 import javax.servlet.http.HttpServletResponse;
     9 import javax.servlet.http.HttpSession;
    10 
    11 import com.etc.svc.UserSvcImpl;
    12 
    13 public class DeleteUserServlet extends HttpServlet {
    14 
    15     @Override
    16     protected void doGet(HttpServletRequest req, HttpServletResponse resp)
    17             throws ServletException, IOException {
    18         // TODO Auto-generated method stub
    19         //super.doGet(req, resp);
    20         doPost(req, resp);
    21     }
    22     
    23     @Override
    24     protected void doPost(HttpServletRequest req, HttpServletResponse resp)
    25             throws ServletException, IOException {
    26         // TODO Auto-generated method stub
    27         //super.doPost(req, resp);
    28         String uname = req.getParameter("username");
    29         HttpSession session = req.getSession();
    30         String upwd = (String) session.getAttribute("password");
    31         UserSvcImpl usi = new UserSvcImpl();
    32         usi.DeleteUser(uname,upwd);
    33         resp.sendRedirect("viewAll");
    34     }
    35 }
    LoginServlet
     1 package com.etc.servlet;
     2 
     3 import java.io.IOException;
     4 
     5 import javax.servlet.ServletException;
     6 import javax.servlet.http.HttpServlet;
     7 import javax.servlet.http.HttpServletRequest;
     8 import javax.servlet.http.HttpServletResponse;
     9 import javax.servlet.http.HttpSession;
    10 
    11 import com.etc.svc.UserSvcImpl;
    12 
    13 public class LoginServlet extends HttpServlet {
    14 
    15     @Override
    16     protected void doGet(HttpServletRequest req, HttpServletResponse resp)
    17             throws ServletException, IOException {
    18         // TODO Auto-generated method stub
    19         //super.doGet(req, resp);
    20         doPost(req, resp);
    21     }
    22     
    23     @Override
    24     protected void doPost(HttpServletRequest req, HttpServletResponse resp)
    25             throws ServletException, IOException {
    26         // TODO Auto-generated method stub
    27         //super.doPost(req, resp);
    28         String uname = req.getParameter("username");
    29         String upwd = req.getParameter("password");
    30         UserSvcImpl usi = new UserSvcImpl();
    31         usi.login(uname, upwd);
    32         HttpSession session = req.getSession();
    33         session.setAttribute("username",uname);
    34         session.setAttribute("password", upwd);
    35         req.getRequestDispatcher("admin/welcome.jsp").forward(req, resp);
    36     }
    37 }
    QueryUserServlet
     1 package com.etc.servlet;
     2 
     3 import java.io.IOException;
     4 import java.util.List;
     5 
     6 import javax.servlet.ServletException;
     7 import javax.servlet.http.HttpServlet;
     8 import javax.servlet.http.HttpServletRequest;
     9 import javax.servlet.http.HttpServletResponse;
    10 
    11 import com.etc.entity.Page;
    12 import com.etc.entity.User;
    13 import com.etc.svc.UserSvcImpl;
    14 
    15 public class QueryUserServlet extends HttpServlet {
    16 
    17     @Override
    18     protected void doGet(HttpServletRequest req, HttpServletResponse resp)
    19             throws ServletException, IOException {
    20         // TODO Auto-generated method stub
    21         //super.doGet(req, resp);
    22         doPost(req, resp);
    23     }
    24     
    25     @Override
    26     protected void doPost(HttpServletRequest req, HttpServletResponse resp)
    27             throws ServletException, IOException {
    28         // TODO Auto-generated method stub
    29         //super.doPost(req, resp);
    30         String uname = req.getParameter("username");
    31         UserSvcImpl usi = new UserSvcImpl();
    32         
    33         int trc = usi.queryUserCount(uname);//记录总条数
    34         int ctp = 1;//当前页数
    35         try{
    36             ctp = Integer.parseInt((req.getParameter("currentpage")));
    37         }catch(NumberFormatException e){
    38             e.printStackTrace();
    39         }
    40         
    41         
    42         int rdop = 2;//每页条数
    43         int tp;//总页数,即最后一页数
    44         int fp = 1;//第一页
    45         int pp = ctp - 1;//上一页
    46         int np = ctp + 1;//下一页
    47         int fr = (ctp-1)*rdop; //从第几条记录开始显示
    48         if(trc%5==0)
    49             tp = trc / 5;
    50         else
    51             tp = trc / 5 + 1;
    52         Page page = new Page(trc, rdop, tp, ctp, fp, pp, np, fr);
    53         
    54         List<User> list = usi.QueryUser(uname,page);
    55         req.setAttribute("queryUser",list);
    56         page.setTotalrecordcount(usi.queryUserCount(uname));
    57         page.setCurrentpage(ctp);
    58         req.setAttribute("Page",page);
    59         req.setAttribute("name", uname);
    60         req.getRequestDispatcher("admin/queryUser.jsp").forward(req, resp);
    61     }
    62 }
    RegisterServlet
     1 package com.etc.servlet;
     2 
     3 import java.io.IOException;
     4 
     5 import javax.servlet.ServletException;
     6 import javax.servlet.http.HttpServlet;
     7 import javax.servlet.http.HttpServletRequest;
     8 import javax.servlet.http.HttpServletResponse;
     9 
    10 import com.etc.entity.User;
    11 import com.etc.svc.UserSvcImpl;
    12 
    13 public class RegisterServlet extends HttpServlet {
    14 
    15     @Override
    16     protected void doGet(HttpServletRequest req, HttpServletResponse resp)
    17             throws ServletException, IOException {
    18         // TODO Auto-generated method stub
    19         //super.doGet(req, resp);
    20         doPost(req, resp);
    21     }
    22     
    23     @Override
    24     protected void doPost(HttpServletRequest req, HttpServletResponse resp)
    25             throws ServletException, IOException {
    26         
    27         String uname = new String(req.getParameter("username").getBytes("ISO-8859-1"),"UTF-8");
    28         String upwd = new String(req.getParameter("password").getBytes("ISO-8859-1"),"UTF-8");
    29         String reupwd = new String(req.getParameter("repassword").getBytes("ISO-8859-1"),"UTF-8");
    30         String tname = new String(req.getParameter("name").getBytes("ISO-8859-1"),"UTF-8");
    31         String usex = new String(req.getParameter("sex").getBytes("ISO-8859-1"),"UTF-8");
    32         String umajor = new String(req.getParameter("major").getBytes("ISO-8859-1"),"UTF-8");
    33         String ucollege = new String(req.getParameter("college").getBytes("ISO-8859-1"),"UTF-8");
    34         String uphone = new String(req.getParameter("phone").getBytes("ISO-8859-1"),"UTF-8");
    35         
    36         if(upwd.equals(reupwd))
    37         {
    38             UserSvcImpl usi = new UserSvcImpl();
    39             User user = new User(uname, upwd, tname, usex, umajor, ucollege, uphone);
    40             usi.register(user);
    41             req.getRequestDispatcher("login.jsp").forward(req, resp);
    42         }
    43         else 
    44         {
    45             req.setAttribute("error", "密码不匹配");
    46             req.getRequestDispatcher("register.jsp").forward(req, resp);
    47         }
    48     }
    49 }
    UpdateUserServlet
     1 package com.etc.servlet;
     2 
     3 import java.io.IOException;
     4 
     5 import javax.servlet.ServletException;
     6 import javax.servlet.http.HttpServlet;
     7 import javax.servlet.http.HttpServletRequest;
     8 import javax.servlet.http.HttpServletResponse;
     9 import javax.servlet.http.HttpSession;
    10 
    11 import com.etc.entity.User;
    12 import com.etc.svc.UserSvcImpl;
    13 
    14 public class UpdateUserServlet extends HttpServlet {
    15 
    16     @Override
    17     protected void doGet(HttpServletRequest req, HttpServletResponse resp)
    18             throws ServletException, IOException {
    19         HttpSession session = req.getSession();
    20         String upwd = (String) session.getAttribute("password");
    21         String uname = new String(req.getParameter("username").getBytes("ISO-8859-1"),"UTF-8");
    22         String tname = new String(req.getParameter("name").getBytes("ISO-8859-1"),"UTF-8");
    23         String usex = new String(req.getParameter("sex").getBytes("ISO-8859-1"),"UTF-8");
    24         String umajor = new String(req.getParameter("major").getBytes("ISO-8859-1"),"UTF-8");
    25         String ucollege = new String(req.getParameter("college").getBytes("ISO-8859-1"),"UTF-8");
    26         String uphone = new String(req.getParameter("phone").getBytes("ISO-8859-1"),"UTF-8");
    27         
    28         UserSvcImpl usi = new UserSvcImpl();
    29         User user = new User(uname,upwd,tname, usex, umajor, ucollege, uphone);
    30         usi.UpdateUser(user);
    31         resp.sendRedirect("viewAll");
    32     }
    33     
    34     @Override
    35     protected void doPost(HttpServletRequest req, HttpServletResponse resp)
    36             throws ServletException, IOException {
    37         
    38         doGet(req, resp);
    39     }
    40 }
    ViewAllServlet
     1 package com.etc.servlet;
     2 
     3 import java.io.IOException;
     4 import java.util.List;
     5 
     6 import javax.servlet.ServletException;
     7 import javax.servlet.http.HttpServlet;
     8 import javax.servlet.http.HttpServletRequest;
     9 import javax.servlet.http.HttpServletResponse;
    10 
    11 import com.etc.entity.Page;
    12 import com.etc.entity.User;
    13 import com.etc.svc.UserSvcImpl;
    14 
    15 public class ViewAllServlet extends HttpServlet {
    16 
    17     @Override
    18     protected void doGet(HttpServletRequest req, HttpServletResponse resp)
    19             throws ServletException, IOException {
    20         // TODO Auto-generated method stub
    21         //super.doGet(req, resp);
    22         doPost(req, resp);
    23     }
    24     
    25     @Override
    26     protected void doPost(HttpServletRequest req, HttpServletResponse resp)
    27             throws ServletException, IOException {
    28         // TODO Auto-generated method stub
    29         //super.doPost(req, resp);
    30         //调用业务逻辑
    31         UserSvcImpl usi = new UserSvcImpl();
    32         int trc = usi.getUserCount();//记录总条数
    33         int ctp = 1;//当前页数
    34         try{
    35             ctp = Integer.parseInt((req.getParameter("currentpage")));
    36         }catch(NumberFormatException e){
    37             e.printStackTrace();
    38         }
    39         
    40         
    41         int rdop = 5;//每页条数
    42         int tp;//总页数
    43         int fp = 1;//第一页
    44         int pp = ctp - 1;//上一页
    45         int np = ctp + 1;//下一页
    46         int fr = (ctp-1)*rdop; //从第几条记录开始显示
    47         if(trc%5==0)
    48             tp = trc / 5;
    49         else
    50             tp = trc / 5 + 1;
    51         Page page = new Page(trc, rdop, tp, ctp, fp, pp, np, fr);
    52         List<User> list = usi.getUserAll(page);
    53         //如何将list传递到allUser.jsp是关键问题
    54         //将返回值作为请求属性存储
    55         req.setAttribute("allUser",list);
    56         page.setTotalrecordcount(usi.getUserCount());
    57         page.setCurrentpage(ctp);
    58         req.setAttribute("Page",page);
    59         //跳转
    60         req.getRequestDispatcher("admin/allUser.jsp").forward(req, resp);
    61     }
    62 }

    com.etc.svc包下有两个类:

    UserSvcI
     1 package com.etc.svc;
     2 
     3 
     4 import java.util.List;
     5 
     6 import com.etc.entity.Page;
     7 import com.etc.entity.User;
     8 
     9 public interface UserSvcI {
    10 
    11     //public void SaveUser(User user);
    12     
    13     
    14     public boolean register(User user);
    15     
    16     public boolean login(String username,String password);
    17     
    18     public List<User> getUserAll(Page page);
    19     
    20     public boolean DeleteUser(String username,String password);
    21     
    22     public List<User> QueryUser(String username,Page page);
    23     
    24     public boolean UpdateUser(User user);
    25     
    26     
    27 }
    UserSvcImpl
      1 package com.etc.svc;
      2 
      3 import java.sql.Connection;
      4 import java.sql.SQLException;
      5 import java.util.List;
      6 
      7 import com.etc.biz.UserBiz;
      8 import com.etc.entity.Page;
      9 import com.etc.entity.User;
     10 import com.etc.tools.MyDBConnection;
     11 
     12 public class UserSvcImpl implements UserSvcI{
     13 //增删改的操作需要对事务进行操作,查不需要
     14     UserBiz ub = new UserBiz();
     15     Connection conn = MyDBConnection.getConnection();
     16     public boolean DeleteUser(String username,String password)
     17     {
     18         
     19         try {
     20             conn.setAutoCommit(false);
     21             ub.delUser(username,password);
     22             conn.commit();
     23             return true;
     24         } catch (SQLException e) {
     25             // TODO Auto-generated catch block
     26             e.printStackTrace();
     27             try {
     28                 conn.rollback();
     29             } catch (SQLException e1) {
     30                 // TODO Auto-generated catch block
     31                 e1.printStackTrace();
     32             }
     33         }
     34         return false;
     35     }
     36     
     37     public boolean login(String username, String password){
     38         // TODO Auto-generated method stub
     39 
     40             try {
     41                 if(ub.login(username, password))
     42                     return true;
     43             } catch (SQLException e) {
     44                 // TODO Auto-generated catch block
     45                 e.printStackTrace();
     46             }
     47             return false;
     48     }
     49     
     50     public boolean register(User user) {
     51         // TODO Auto-generated method stub
     52         try {
     53             conn.setAutoCommit(false);
     54             ub.save(user);
     55             conn.commit();
     56             return true;
     57         } catch (SQLException e) {
     58             // TODO Auto-generated catch block
     59             try {
     60                 conn.rollback();
     61             } catch (SQLException e1) {
     62                 // TODO Auto-generated catch block
     63                 e1.printStackTrace();
     64             }
     65             e.printStackTrace();
     66         }
     67         return false;
     68     }
     69     
     70     public boolean UpdateUser(User user){
     71         try {
     72             conn.setAutoCommit(false);
     73             ub.updUser(user);
     74             conn.commit();
     75             return true;
     76         } catch (SQLException e) {
     77             // TODO Auto-generated catch block
     78             try {
     79                 conn.rollback();
     80             } catch (SQLException e1) {
     81                 // TODO Auto-generated catch block
     82                 e1.printStackTrace();
     83             }
     84             e.printStackTrace();
     85         }
     86         return false;
     87     }
     88     
     89     public List<User> getUserAll(Page page)//传参
     90     {
     91         try {
     92             return ub.getAllUser(page);//传参
     93         } catch (SQLException e) {
     94             // TODO Auto-generated catch block
     95             e.printStackTrace();
     96         }
     97         return null;
     98     }
     99     
    100     public List<User> QueryUser(String username,Page page)
    101     {
    102         try {
    103             return ub.getUser(username,page);
    104         } catch (SQLException e) {
    105             // TODO Auto-generated catch block
    106             e.printStackTrace();
    107         }
    108         return null;
    109         
    110     }
    111     
    112     public int getUserCount()
    113     {
    114         try {
    115             return ub.getCount();
    116         } catch (SQLException e) {
    117             // TODO Auto-generated catch block
    118             e.printStackTrace();
    119         }
    120         return 0;
    121     }
    122     
    123     public int queryUserCount(String username)
    124     {
    125         try {
    126             return ub.queryCount(username);
    127         } catch (SQLException e) {
    128             // TODO Auto-generated catch block
    129             e.printStackTrace();
    130         }
    131         return 0;
    132     }
    133 }
    com.etc.tools
     1 package com.etc.tools;
     2 
     3 import java.sql.Connection;
     4 import java.sql.DriverManager;
     5 import java.sql.PreparedStatement;
     6 import java.sql.ResultSet;
     7 import java.sql.SQLException;
     8 import java.sql.Statement;
     9 
    10 public class MyDBConnection {
    11 
    12     public static Connection conn = null;
    13     public static Connection getConnection()
    14     {
    15         if(conn !=null)
    16             return conn;
    17         
    18         try {
    19             Class.forName("com.mysql.jdbc.Driver");
    20             conn = DriverManager.getConnection(
    21                     "jdbc:mysql://localhost:3306/kaijie?useUnicode=true&characterEncoding=utf8", "root", "123");
    22             
    23         } catch (ClassNotFoundException e) {
    24             e.printStackTrace();
    25         } catch (SQLException e) {
    26             e.printStackTrace();
    27         }
    28         return conn;
    29     }
    30     
    31     public static Statement getStatement(){
    32         if(conn==null)
    33             getConnection();
    34         try {
    35             return conn.createStatement();
    36         } catch (SQLException e) {
    37             // TODO Auto-generated catch block
    38             e.printStackTrace();
    39         }
    40         return null;
    41     }
    42     
    43     public static PreparedStatement getPrepareStatement(String sql){
    44         if(conn==null)
    45             getConnection();
    46         try {
    47             return conn.prepareStatement(sql);
    48         } catch (SQLException e) {
    49             // TODO Auto-generated catch block
    50             e.printStackTrace();
    51         }
    52         return null;
    53     }
    54 
    55     public static ResultSet ExecQuery(String sql){
    56         if(conn==null)
    57             getConnection();
    58         Statement stmt;
    59         try {
    60             stmt = conn.createStatement();
    61             ResultSet rs = stmt.executeQuery(sql);
    62             return rs;
    63         } catch (SQLException e) {
    64             // TODO Auto-generated catch block
    65             e.printStackTrace();
    66         }
    67         return null;
    68     }
    69     
    70     public static int ExecSQL(String sql){
    71         if(conn==null)
    72             getConnection();
    73         Statement stmt;
    74         try {
    75             stmt = conn.createStatement();
    76             int result = stmt.executeUpdate(sql);
    77             return result;
    78         } catch (SQLException e) {
    79             // TODO Auto-generated catch block
    80             e.printStackTrace();
    81         }
    82         return 0;
    83     }
    84 }
  • 相关阅读:
    USACO 1.1-ride
    USACO 1.1-gift1
    USACO 1.1-Friday the Thirteenth
    SQL详解(上)
    Python入门神图
    JSTL标签详解以及应用实例
    EL表达式详解及应用实例
    session应用----登录验证小案例
    各种编码问题产生原因以及解决办法---------响应编码,请求编码,URL编码
    Servlet的request应用案例
  • 原文地址:https://www.cnblogs.com/dhg1003/p/2737224.html
Copyright © 2011-2022 走看看