有一个servlet文件 ,它接受从jsp页面传来的登陆信息。登录成功调整至success.jsp成功页面,登录失败则重定向至失败页面fail .jsp
时间: 2024-05-07 20:16:03 浏览: 13
以下是一个简单的示例代码:
```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");
// 假设用户名为admin,密码为123456
if ("admin".equals(username) && "123456".equals(password)) {
response.sendRedirect("success.jsp"); // 登录成功,重定向到success.jsp页面
} else {
response.sendRedirect("fail.jsp"); // 登录失败,重定向到fail.jsp页面
}
}
}
```
在jsp页面中,需要有一个form表单来提交登录信息:
```html
<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>
```
在success.jsp和fail.jsp页面中,可以展示登录成功或失败的信息,例如:
```html
<%-- success.jsp --%>
<h1>Login Success</h1>
<%-- fail.jsp --%>
<h1>Login Failed</h1>
```
相关推荐
![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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)