编写在线申请审批系统的登录页面,张三是学生、李四是老师,根据用户身份显示不同的登录信息要求分别使用请求转发方式,以request对象传递数据;用重定向方式,以cookie和session方式传递数据
时间: 2024-05-08 19:17:16 浏览: 54
请求转发方式,以request对象传递数据:
```
<!DOCTYPE html>
<html>
<head>
<title>登录页面</title>
</head>
<body>
<h1>欢迎使用在线申请审批系统</h1>
<form method="post" action="login">
<label for="username">用户名:</label>
<input type="text" id="username" name="username"><br><br>
<label for="password">密码:</label>
<input type="password" id="password" name="password"><br><br>
<label for="identity">身份:</label>
<select id="identity" name="identity">
<option value="student">学生</option>
<option value="teacher">老师</option>
</select><br><br>
<input type="submit" value="登录">
</form>
</body>
</html>
```
登录Servlet代码:
```
@WebServlet("/login")
public class LoginServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
// 处理POST请求
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String username = request.getParameter("username");
String password = request.getParameter("password");
String identity = request.getParameter("identity");
// 验证用户名和密码
if (username.equals("张三") && password.equals("123456")) {
if (identity.equals("student")) {
// 学生身份,转发到学生首页
request.setAttribute("username", username);
request.getRequestDispatcher("/student").forward(request, response);
} else if (identity.equals("teacher")) {
// 老师身份,转发到老师首页
request.setAttribute("username", username);
request.getRequestDispatcher("/teacher").forward(request, response);
}
} else {
// 用户名或密码不正确,返回登录页面
response.sendRedirect(request.getContextPath() + "/login.jsp");
}
}
}
```
重定向方式,以cookie和session方式传递数据:
```
<!DOCTYPE html>
<html>
<head>
<title>登录页面</title>
</head>
<body>
<h1>欢迎使用在线申请审批系统</h1>
<form method="post" action="login">
<label for="username">用户名:</label>
<input type="text" id="username" name="username"><br><br>
<label for="password">密码:</label>
<input type="password" id="password" name="password"><br><br>
<label for="identity">身份:</label>
<select id="identity" name="identity">
<option value="student">学生</option>
<option value="teacher">老师</option>
</select><br><br>
<input type="submit" value="登录">
</form>
</body>
</html>
```
登录Servlet代码:
```
@WebServlet("/login")
public class LoginServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
// 处理POST请求
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String username = request.getParameter("username");
String password = request.getParameter("password");
String identity = request.getParameter("identity");
// 验证用户名和密码
if (username.equals("张三") && password.equals("123456")) {
if (identity.equals("student")) {
// 学生身份,重定向到学生首页
Cookie cookie = new Cookie("username", username);
response.addCookie(cookie);
response.sendRedirect(request.getContextPath() + "/student");
} else if (identity.equals("teacher")) {
// 老师身份,重定向到老师首页
HttpSession session = request.getSession();
session.setAttribute("username", username);
response.sendRedirect(request.getContextPath() + "/teacher");
}
} else {
// 用户名或密码不正确,返回登录页面
response.sendRedirect(request.getContextPath() + "/login.jsp");
}
}
}
```
阅读全文