编写3个JSP程序login. jsp.loginCheck. jsp、main. jsp,程序功能分别是进行用户登录、登录信息处理和登录后的工作页面中获取保存在session中的用户信息。
时间: 2023-06-11 22:07:21 浏览: 168
1. login.jsp
```
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Login Page</title>
</head>
<body>
<h1>Login Page</h1>
<form method="post" action="loginCheck.jsp">
<label for="username">Username:</label>
<input type="text" name="username" id="username" required>
<br>
<label for="password">Password:</label>
<input type="password" name="password" id="password" required>
<br>
<input type="submit" value="Login">
</form>
</body>
</html>
```
2. loginCheck.jsp
```
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
String username = request.getParameter("username");
String password = request.getParameter("password");
if(username.equals("admin") && password.equals("123456")) {
session.setAttribute("username", username);
response.sendRedirect("main.jsp");
} else {
out.println("<h1>Invalid username or password!</h1>");
}
%>
```
3. main.jsp
```
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Main Page</title>
</head>
<body>
<h1>Main Page</h1>
<% String username = (String) session.getAttribute("username");
if(username != null) { %>
<p>Welcome <%= username %>!</p>
<p>This is your main page after login.</p>
<% } else { %>
<p>Please login first.</p>
<% response.sendRedirect("login.jsp"); %>
<% } %>
</body>
</html>
```