在login.jsp页面上,输入userName,Password 时,提交到控制层 userlogin方法,并在控制台输出userName,password, 如果,用户名和密码是ZhangSan和123时,则跳转到 main.jsp 并显示用户名在页面上。否则,跳转到 login.jsp页面,并显示用户名或密码错误。
时间: 2024-02-29 20:52:04 浏览: 54
下面是一个示例代码,实现了在 login.jsp 页面上输入用户名和密码,提交到控制层 userlogin 方法,并输出用户名和密码。如果用户名和密码是 "ZhangSan" 和 "123",则跳转到 main.jsp 页面并显示用户名;否则,跳转回 login.jsp 页面并显示错误信息。
login.jsp 页面:
```html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Login</title>
</head>
<body>
<form method="post" action="userlogin">
<input type="text" name="username" placeholder="Username"><br>
<input type="password" name="password" placeholder="Password"><br>
<input type="submit" value="Login">
</form>
<% if (request.getAttribute("errorMessage") != null) { %>
<p><%= request.getAttribute("errorMessage") %></p>
<% } %>
</body>
</html>
```
Controller:
```java
@Controller
public class UserController {
@RequestMapping(value = "/userlogin", method = RequestMethod.POST)
public String userlogin(HttpServletRequest request, HttpServletResponse response) throws IOException {
String username = request.getParameter("username");
String password = request.getParameter("password");
System.out.println("Username: " + username);
System.out.println("Password: " + password);
if (username.equals("ZhangSan") && password.equals("123")) {
request.setAttribute("username", username);
return "main";
} else {
request.setAttribute("errorMessage", "用户名或密码错误");
return "login";
}
}
}
```
在控制器 UserController 中,定义了 userlogin 方法,通过 HttpServletRequest 对象获取用户名和密码,然后输出到控制台。如果用户名和密码正确,将用户名保存到 request 对象中,并返回 main.jsp 页面;否则,将错误信息保存到 request 对象中,并返回 login.jsp 页面。
在 main.jsp 页面中,可以通过 EL 表达式获取到保存在 request 对象中的用户名,然后在页面上显示出来。