java web请按照以下要求编写程序。 1.编写用户登录的界面login.html和登录成功的界面welcome.html。 2.编写处理用户登录请求的Servlet类Login。 3.Login类中判断表单中如果输入的用户名为“itcast”,密码为“itcast”,将请求重定向到welcome.html页面,否则重定向到login.html页面。
时间: 2024-02-05 07:12:05 浏览: 31
好的,以下是代码实现:
1. login.html
```html
<!DOCTYPE html>
<html>
<head>
<title>Login</title>
</head>
<body>
<form action="login" method="post">
<label>Username:</label>
<input type="text" name="username"><br>
<label>Password:</label>
<input type="password" name="password"><br>
<input type="submit" value="Login">
</form>
</body>
</html>
```
2. welcome.html
```html
<!DOCTYPE html>
<html>
<head>
<title>Welcome</title>
</head>
<body>
<h1>Welcome!</h1>
</body>
</html>
```
3. Login Servlet
```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 Login extends HttpServlet {
private static final long serialVersionUID = 1L;
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String username = request.getParameter("username");
String password = request.getParameter("password");
if (username.equals("itcast") && password.equals("itcast")) {
response.sendRedirect("welcome.html");
} else {
response.sendRedirect("login.html");
}
}
}
```
在这个Servlet中,我们首先获取表单中的用户名和密码,然后判断是否为“itcast”,如果是,将请求重定向到welcome.html页面,否则重定向到login.html页面。注意,我们在@WebServlet注解中指定了Servlet的访问路径为“/login”。