javaweb登陆界面实现不同角色进入不同界面
2021/11/27 1:10:13
本文主要是介绍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是该用户类型
如果全部匹配就可进入对应界面
这篇关于javaweb登陆界面实现不同角色进入不同界面的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-10-01基于Python+Vue开发的医院门诊预约挂号系统
- 2024-10-01基于Python+Vue开发的旅游景区管理系统
- 2024-10-01RestfulAPI入门指南:打造简单易懂的API接口
- 2024-10-01初学者指南:了解和使用Server Action
- 2024-10-01Server Component入门指南:搭建与配置详解
- 2024-10-01React 中使用 useRequest 实现数据请求
- 2024-10-01使用 golang 将ETH账户的资产平均分散到其他账户
- 2024-10-01JWT用户校验课程:从入门到实践
- 2024-10-01Server Component课程入门指南
- 2024-09-30Dnd-Kit学习:新手快速入门指南