按照javabean的编写规范编写一个user.java类,含有用户序号userid,用户名username,性别gender,年龄age,并编写对应属性的getter方法然后编写一个jsp程序,在jsp界面中引入User对象,对所有属性进行设置,并利用标签语句输出所有属性值
时间: 2024-11-22 12:39:35 浏览: 25
```java
// user.java (按照JavaBean规范)
public class User implements Serializable {
private String userId;
private String username;
private String gender;
private int age;
// Getter方法
public String getUserId() {
return userId;
}
public void setUserId(String userId) {
this.userId = userId;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public int getAge() {
return age;
}
public void setAge(int age) {
if (age >= 0) { // 确保年龄非负
this.age = age;
}
}
}
// JSP程序部分
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>User Information</title>
</head>
<body>
<%-- 引入User对象 --%>
<jsp:useBean id="user" class="com.example.User" scope="request"></jsp:useBean>
<!-- 设置属性 -->
<form action="#" method="post">
User ID: <input type="text" name="userId" value="<%= user.getUserId() %>"><br>
Username: <input type="text" name="username" value="<%= user.getUsername() %>"><br>
Gender: <input type="text" name="gender" value="<%= user.getGender() %>"><br>
Age: <input type="number" name="age" value="<%= user.getAge() %>"><br>
<input type="submit" value="Submit">
</form>
<!-- 输出属性值 -->
<h2>User Details:</h2>
<p>UserID: <%= user.getUserId() %></p>
<p>Username: <%= user.getUsername() %></p>
<p>Gender: <%= user.getGender() %></p>
<p>Age: <%= user.getAge() %></p>
</body>
</html>
```
阅读全文