目錄結構
類包:
- AccountBean.java
- AccountDao.java
- JudgeServlet.java
登陸界面:
- index.jsp
代碼實現
AccountBean.java
package login; public class AccountBean { String account; String password; String type; public String getAccount() { return account; } public void setAccount(String account) { this.account = account; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getType() { return type; } public void setType(String type) { this.type = type; } }
AccountDao.java
package login; import dao.Con_CloseSql; import java.sql.*; public class AccountDao { public AccountBean checkAccount(String user, String password) throws Exception { Connection connect = new Con_CloseSql().getConnect(); Statement stat = connect.createStatement(); String sql = "select * from account where account = '" + user + "' and password = '" + password + "'"; ResultSet rs = stat.executeQuery(sql); AccountBean accountBean = new AccountBean(); if (rs.next()){ accountBean.setAccount(rs.getString("account")); accountBean.setPassword(rs.getString("password")); accountBean.setType(rs.getString("type")); } Con_CloseSql.closeConnection(connect); return accountBean; } }
JudgeServlet.java
package login; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; public class JudgeServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { req.setCharacterEncoding("utf-8"); resp.setCharacterEncoding("utf-8"); resp.setContentType("text/html;utf-8"); String account = req.getParameter("account"); String password = req.getParameter("password"); String type = req.getParameter("type"); AccountDao dao = new AccountDao(); try { AccountBean bean = dao.checkAccount(account, password); String type1 = bean.getType(); if(type1.equals(type)) { if(type.equals("學生")) req.getRequestDispatcher("stuhome.jsp").forward(req,resp); if(type.equals("教師")) req.getRequestDispatcher("teahome.jsp").forward(req,resp); if(type.equals("管理員")) req.getRequestDispatcher("Adminhome.jsp").forward(req,resp); } } catch (Exception e) { resp.getWriter().print("<script>alert(\"賬號或用戶名出錯\"); </script>"); } } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doGet(req, resp); } }
index.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> </head> <body> <form method="post" action="judge"> 賬號:<input type="text" name="account"><br> 密碼:<input type="password" name="password"><br> 用戶類型: <select name="type"> <option value="學生" name="xuesheng">學生</option> <option value="教師" name="jiaoshi">教師</option> <option value="管理員" name="guanliyuan">管理員</option> </select><br> <input type="submit" value="登陸"> </form> </body> </html>
數據庫表結構為
其中account與password是用戶密碼
type是該用戶類型
如果全部匹配就可進入對應界面