jsp声明一个student类
时间: 2024-06-06 20:07:48 浏览: 21
<%!
public class Student {
private String name;
private int age;
public Student(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
}
%>
相关问题
写一个学生列表页面list.jsp
以下是一个简单的学生列表页面list.jsp的示例代码:
```
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>学生列表</title>
</head>
<body>
<h1>学生列表</h1>
<table>
<thead>
<tr>
<th>学号</th>
<th>姓名</th>
<th>年龄</th>
<th>性别</th>
<th>操作</th>
</tr>
</thead>
<tbody>
<c:forEach var="student" items="${students}">
<tr>
<td>${student.id}</td>
<td>${student.name}</td>
<td>${student.age}</td>
<td>${student.gender}</td>
<td><a href="edit.jsp?id=${student.id}">编辑</a> | <a href="delete?id=${student.id}">删除</a></td>
</tr>
</c:forEach>
</tbody>
</table>
<p>
<a href="add.jsp">添加学生</a>
</p>
</body>
</html>
```
解释一下代码:
1. 第1行到第4行是jsp页面的声明,指定编码为UTF-8。
2. 第8行到第10行是页面标题和标题标签。
3. 第12行到第23行是学生列表的表格结构,包括表头和表体。
4. 第15行到第22行使用JSTL的forEach标签循环遍历学生列表,并输出学生的信息。
5. 第19行和第20行分别是编辑和删除学生的链接,链接中的参数使用了EL表达式。
6. 第25行到第27行是添加学生的链接。
在使用这个页面之前,需要先从后台获取学生列表并将其存储在request的属性中,例如:
```
List<Student> students = studentService.findAll();
request.setAttribute("students", students);
```
然后将request转发到list.jsp页面即可。
编写程序newclass2.jsp,学会在声明中定义类student,初始化该类(学号、姓名、成绩)
<%@ page language="java" %>
<%
// 定义类student
class student {
String id; // 学号
String name; // 姓名
double score; // 成绩
// 构造方法
public student(String id, String name, double score) {
this.id = id;
this.name = name;
this.score = score;
}
// 输出信息
public void printInfo() {
out.println("学号:" + id + "<br>");
out.println("姓名:" + name + "<br>");
out.println("成绩:" + score + "<br>");
}
}
// 初始化学生信息
student s1 = new student("001", "张三", 89.5);
student s2 = new student("002", "李四", 92.0);
student s3 = new student("003", "王五", 78.5);
// 输出学生信息
out.println("<h3>学生信息列表</h3>");
s1.printInfo();
s2.printInfo();
s3.printInfo();
%>
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![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)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)