[编程题]编写登录功能代码: 要求 1.页面使用jsp,包括用户名、密码 2.用表单提交,用servlet接收 3.如果用户名是“niit_user1”、密码是“123456”返回登录成功;否则,返回登录失败; 设登录页面名称为login.jsp
时间: 2024-05-10 16:15:51 浏览: 116
以下是一个简单的实现登录功能的代码示例,使用JSP页面和Servlet接收表单数据:
login.jsp:
```jsp
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Login Page</title>
</head>
<body>
<h1>Login Page</h1>
<form action="loginServlet" 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.*;
import javax.servlet.*;
import javax.servlet.http.*;
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("niit_user1") && password.equals("123456")) {
response.sendRedirect("success.jsp");
} else {
response.sendRedirect("failure.jsp");
}
}
}
```
success.jsp:
```jsp
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Success Page</title>
</head>
<body>
<h1>Login Successful!</h1>
</body>
</html>
```
failure.jsp:
```jsp
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Failure Page</title>
</head>
<body>
<h1>Login Failed!</h1>
</body>
</html>
```
以上是一个简单的登录功能实现的代码示例,当用户输入用户名和密码并提交表单时,Servlet会接收表单数据并进行验证,如果验证通过就跳转到成功页面,否则跳转到失败页面。
阅读全文