1.编写一个简单的登录界面,如果登录成功显示查看先学生等信息,javaweb登录失败则显示对不起登录失败等信息,登录页面以及成功失败页面参考教材205页
时间: 2024-05-04 21:21:15 浏览: 20
以下是一个简单的登录界面的代码,使用了JSP和Servlet:
login.jsp
```html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Login Page</title>
</head>
<body>
<h1>Login Page</h1>
<form action="login" method="post">
<label for="username">Username:</label>
<input type="text" id="username" name="username"><br><br>
<label for="password">Password:</label>
<input type="password" id="password" name="password"><br><br>
<input type="submit" value="Login">
</form>
</body>
</html>
```
LoginServlet.java
```java
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/login")
public class LoginServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String username = request.getParameter("username");
String password = request.getParameter("password");
if(username.equals("admin") && password.equals("123")) {
response.sendRedirect("success.jsp");
} else {
response.sendRedirect("failure.jsp");
}
}
}
```
success.jsp
```html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Success Page</title>
</head>
<body>
<h1>Welcome to Student Information System</h1>
<p>You can check student information here.</p>
</body>
</html>
```
failure.jsp
```html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Failure Page</title>
</head>
<body>
<h1>Login Failed</h1>
<p>Sorry, the username or password you entered is incorrect.</p>
<p>Please try again.</p>
<a href="login.jsp">Back to Login Page</a>
</body>
</html>
```
当用户在登录界面中输入用户名和密码并点击登录按钮时,会将这些信息提交到LoginServlet.java文件中进行处理。如果用户名和密码都正确,则重定向到success.jsp页面,否则重定向到failure.jsp页面。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)