javaweb登陆界面实现不同角色进入不同界面
目录结构
类包:
- 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是该用户类型
如果全部匹配就可进入对应界面
分类:
Web后端
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· DeepSeek 开源周回顾「GitHub 热点速览」
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了