对<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body> <form action="check.jsp" method=post name=form> 请输入用户名:<input type="text" name="username" value=""><br><br> 请输入密码:<input type="password" name="password" value=""><br><br> <input type="submit" value="确定" name=submit> <input type="reset" value="重填" name=reset><br> </form> </body> </html>进行javabean封装并在jsp中使用
时间: 2023-07-19 14:19:56 浏览: 218
可以将上述HTML代码中的表单数据封装为一个JavaBean对象,如下所示:
```java
public class User {
private String username;
private String password;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
```
然后在JSP页面中使用`request.getParameter()`方法获取表单数据,并将其设置到JavaBean对象中,如下所示:
```java
<%
User user = new User();
user.setUsername(request.getParameter("username"));
user.setPassword(request.getParameter("password"));
%>
```
这样就可以方便地操作表单数据了。可以在JSP页面中使用EL表达式`${user.username}`和`${user.password}`获取用户输入的用户名和密码。例如:
```html
用户名:${user.username}<br>
密码:${user.password}<br>
```
完整的代码如下所示:
```html
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
User user = new User();
user.setUsername(request.getParameter("username"));
user.setPassword(request.getParameter("password"));
%>
用户名:${user.username}<br>
密码:${user.password}<br>
<form action="check.jsp" method="post" name="form">
请输入用户名:<input type="text" name="username" value=""><br><br>
请输入密码:<input type="password" name="password" value=""><br><br>
<input type="submit" value="确定" name="submit">
<input type="reset" value="重填" name="reset"><br>
</form>
</body>
</html>
```
阅读全文