首先把不同用户登录显示不同界面的功能实现
建立useradmin数据库表如下
数据库连接util类
package com.util; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; /** * 数据库连接工具 * @author 李泽宇 * */ public class Butil { //联结字符串 //数据库名test public static String db_url = "jdbc:mysql://localhost:3306/sys?&useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=UTC"; //数据库用户名 public static String db_user = "root"; //数据库密码名 public static String db_pass = "123456"; public static Connection getConn () { //声明与数据库的连接并实例化为null Connection conn = null; try { //驱动程序名 Class.forName("com.mysql.jdbc.Driver");//连接数据库 //具体地连接到数据库——联接字符串(数据库名),联接用户名,联接密码名 conn = DriverManager.getConnection(db_url, db_user, db_pass); } catch (Exception e) { e.printStackTrace(); } return conn; } /** * 关闭连接 * @param state * @param 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(); } } } }
servlet类,利用id进行不同用户登录不同界面,登录失败重新回到登录界面
package com.servlet; import java.io.IOException; import java.sql.SQLException; 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 java.util.ArrayList; import java.util.List; import com.bean.Admin; import com.dao.AdminDao; import com.mysql.fabric.xmlrpc.base.Array; /** * Servlet implementation class AdminServlet */ @WebServlet("/AdminServlet") public class AdminServlet extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public AdminServlet() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub response.getWriter().append("Served at: ").append(request.getContextPath()); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); } protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { req.setCharacterEncoding("utf-8"); String method=req.getParameter("method"); System.out.println(method); if(method.equals("post")) { try { login(req,resp); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); String msg = "登录信息错误"; //把错误信息放入Request域中 req.setAttribute("msg",msg); //请求转发到error.jsp页面 req.getRequestDispatcher("/error.jsp").forward(req,resp); } } } protected void login(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException, SQLException { req.setCharacterEncoding("utf-8"); String username=req.getParameter("username"); String password=req.getParameter("password"); System.out.println(username); System.out.println(password); Admin admin=new Admin(); AdminDao admindao=new AdminDao(); admin=admindao.login(username); System.out.println(admin.getPassword()); if(password.equals(admin.getPassword())) { if("1".equals(admin.getId())) { req.getRequestDispatcher("/1-director.jsp").forward(req,resp); } else if("2".equals(admin.getId())) { req.getRequestDispatcher("/manage.jsp").forward(req,resp); } else if("12".equals(admin.getId())) { req.getRequestDispatcher("/2-assist1.jsp").forward(req,resp); } else if("13".equals(admin.getId())) { req.getRequestDispatcher("/3-assist2.jsp").forward(req,resp); } else if("14".equals(admin.getId())) { req.getRequestDispatcher("/4-assist3.jsp").forward(req,resp); } else if("15".equals(admin.getId())) { req.getRequestDispatcher("/5-office.jsp").forward(req,resp); } else if("21".equals(admin.getId())) { req.getRequestDispatcher("/6-product.jsp").forward(req,resp); } else if("31".equals(admin.getId())) { req.getRequestDispatcher("/7-sale.jsp").forward(req,resp); } else if("41".equals(admin.getId())) { req.getRequestDispatcher("/8-finance.jsp").forward(req,resp); } } else { req.getRequestDispatcher("/login.jsp").forward(req,resp); } } }
jsp如下: