基于javaweb的人物介绍网站模板
时间: 2023-07-26 19:34:33 浏览: 113
基于javaweb的小型图书网站
以下是一个基于JavaWeb的人物介绍网站模板的示例代码,供您参考:
1. 首页(index.jsp):
```jsp
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>人物介绍网站</title>
</head>
<body>
<h1>人物介绍网站</h1>
<p>欢迎来到人物介绍网站!请选择你想要了解的人物:</p>
<ul>
<li><a href="person.jsp?id=1">张三</a></li>
<li><a href="person.jsp?id=2">李四</a></li>
<li><a href="person.jsp?id=3">王五</a></li>
</ul>
</body>
</html>
```
2. 人物介绍页面(person.jsp):
```jsp
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>人物介绍网站 - ${person.name}</title>
</head>
<body>
<h1>${person.name}</h1>
<p>出生日期:${person.birthdate}</p>
<p>职业:${person.occupation}</p>
<p>简介:${person.introduction}</p>
<h2>成就</h2>
<ul>
<c:forEach var="achievement" items="${person.achievements}">
<li>${achievement}</li>
</c:forEach>
</ul>
<h2>名言</h2>
<ul>
<c:forEach var="saying" items="${person.sayings}">
<li>${saying}</li>
</c:forEach>
</ul>
</body>
</html>
```
3. Servlet(PersonServlet.java):
```java
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
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("/person")
public class PersonServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
private static final List<Person> persons = new ArrayList<>();
static {
persons.add(new Person(1, "张三", "1970-01-01", "教师", "张三是一名资深教师。",
new String[]{"获得过优秀教师称号。", "撰写过多篇教育论文。"},
new String[]{"教育要立德、立功、立业。", "教育是造就人的灵魂。"}));
persons.add(new Person(2, "李四", "1980-01-01", "医生", "李四是一名知名医生。",
new String[]{"曾获得过全国医学奖。", "发表过多篇医学论文。"},
new String[]{"生命只有一次,要珍惜健康。", "医生的职责是拯救生命。"}));
persons.add(new Person(3, "王五", "1990-01-01", "艺术家", "王五是一名著名艺术家。",
new String[]{"作品多次入选国际艺术展。", "曾获得过国家艺术奖。"},
new String[]{"艺术是生命的延续。", "艺术是感性和理性的结合。"}));
}
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
int id = Integer.parseInt(request.getParameter("id"));
Person person = persons.get(id - 1);
request.setAttribute("person", person);
request.getRequestDispatcher("/person.jsp").forward(request, response);
}
}
```
4. 实体类(Person.java):
```java
public class Person {
private int id;
private String name;
private String birthdate;
private String occupation;
private String introduction;
private String[] achievements;
private String[] sayings;
public Person(int id, String name, String birthdate, String occupation, String introduction,
String[] achievements, String[] sayings) {
this.id = id;
this.name = name;
this.birthdate = birthdate;
this.occupation = occupation;
this.introduction = introduction;
this.achievements = achievements;
this.sayings = sayings;
}
// 省略 getter 和 setter 方法
}
```
这个示例代码演示了一个基本的人物介绍网站的实现,包括首页、人物介绍页面、Servlet 和实体类。您可以根据自己的需求进行修改和扩展。
阅读全文