公文流转系统部分之分角色登录

这一部分我主要实现了多用户登录,主要是设定一个权限,通过比较权限实现不同用户的登录。

dbutil层

package com.fin.util;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class BaseConnection {
     public static Connection getConnection(){//用这个方法获取mysql的连接
         Connection conn=null;
         String driver = "com.mysql.jdbc.Driver";
         String url = "jdbc:mysql://localhost:3306/zsgc1?characterEncoding=utf8&useSSL=true";
         String user = "root";
         String password = "123456";
         try{
             Class.forName(driver);//加载驱动类
             conn=DriverManager.   
                     getConnection(url,user,password);//(url数据库的IP地址,user数据库用户名,password数据库密码)
         }catch(Exception e){
             e.printStackTrace();
         }
         return conn;
     }
     
     public static void close (Statement state, Connection conn) {
            if (state != null) {
                try {
                    state.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            
            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
        
        public static void close (ResultSet rs, Statement state, Connection conn) {
            if (rs != null) {
                try {
                    rs.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            
            if (state != null) {
                try {
                    state.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            
            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }


    public static void main(String[] args) {
        System.out.println("连接成功");
    }

}
View Code

Javabean

package com.xk.bean;

public class user {
     private String username;
     private String password;
     private int id;
     private String id1;
    

    
    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
    public String getId1() {
        return id1;
    }
    public void setId1(String id1) {
        this.id1 = id1;
    }
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    
    
    
    
    public user(int id, String username, String password,String id1) {
        // TODO 自动生成的构造函数存根
        this.id = id;
        this.username = username;
        this.password = password;
        this.id1 = id1;
    }


    
    public user(String username, String password,String id1) {
        // TODO 自动生成的构造函数存根
        this.username = username;
        this.password = password;
        this.id1 = id1;
    }
}
View Code

dao

package com.xk.dao;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import com.xk.bean.user;
import com.xk.bean.user1;
import com.xk.bean.user2;
import com.xk.bean.user3;
import com.xk.bean.Page;
import com.xk.dbutil.Dbutil;;

public class Dao {
    public String dopost(user dl) {
        String i="0";
        String sql="select * from admin where username = '"+dl.getUsername()+"'";
        Connection conn = Dbutil.getConn();
        Statement state = null;
        ResultSet rs = null;
        try {
            state = conn.createStatement();
            rs = state.executeQuery(sql);
            while(rs.next()){
            String password1 = rs.getString("password");
            String password2 = dl.getPassword();
            if(dl.getPassword().equals(password1)) {
                i=rs.getString("id1");
            }
            break;
           }
        }catch (Exception e) {
            e.printStackTrace();
        } finally {
            Dbutil.close(rs,state, conn);
        }
        return i;
    }
    
    
}
View Code

servlet

package com.xk.servlet;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


//import javax.smartcardio.ResponseAPDU;
import com.xk.bean.user;
import com.xk.bean.user1;
import com.xk.bean.user2;
import com.xk.bean.user3;
import com.xk.dao.Dao;
/**
 * Servlet implementation class CourseServlet
 */
@WebServlet("/Servlet")

public class Servlet extends HttpServlet {
     private static final long serialVersionUID = 1L;
     
        
        public Servlet() {
            super();
            
        }
        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            // TODO Auto-generated method stub
            //response.getWriter().append("Served at: ").append(request.getContextPath());
            
            response.setContentType("text/html;charset=UTF-8");
            request.setCharacterEncoding("UTF-8");
            
            String method = request.getParameter("method");
             if(method.equals("dopost"))
            {
                dopost(request,response);
                
            }
            
            
        }
        
        private void dopost(HttpServletRequest requst, HttpServletResponse response) throws IOException, ServletException{
            requst.setCharacterEncoding("utf-8");
            String username = requst.getParameter("username");
            String password = requst.getParameter("password");
            String id1 = requst.getParameter("id1");
            System.out.println(username);
            Dao dao=new Dao();
            user course = new user(username,password,id1);
            String id=dao.dopost(course);
            if(id.equals("0")) {
                requst.setAttribute("message", "登录失败!");
                requst.getRequestDispatcher("index.jsp").forward(requst,response);
            }
            else if(id.equals("1")) {
                requst.setAttribute("message", "登陆成功!");
                requst.getRequestDispatcher("manager.jsp").forward(requst,response);
            }
            else if(id.equals("2")) {
                requst.setAttribute("message", "登陆成功!");
                requst.getRequestDispatcher("teacher.jsp").forward(requst,response);
            }
            else if(id.equals("3")) {
                requst.setAttribute("message", "登陆成功!");
                requst.getRequestDispatcher("student.jsp").forward(requst,response);
            }
            
        }
      
        protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            // TODO Auto-generated method stub
            doGet(request, response);
            
        }


}
View Code
posted @ 2019-12-09 22:11  林某大帅比  阅读(263)  评论(0编辑  收藏  举报