(1)新建一个学生类Student,属性包括:学号number、姓名userName、性别sex(注这个属性要设置成枚举类型:enum Gender)、班级className、所属院系department (2)新建一个学生信息展示页面list.jsp,主要用来显示已存在的学生信息列表,并在此页面中添加一个链接用来请求add.jsp页面 (3)新建一个Servlet,名为ListServlet,用来读取已添加的学生信息到list.jsp页面 (4)新建一个学生信息添加页面add.jsp,学生信息如同学生类封装的属性 (5)新建一个Servlet,名为AddServlet,用来将页面add.jsp提交的学生信息存储到一个List<Student>类型的list集合中 (6)新建一个Servlet,名为LoginServlet,用来验证学生的身份,设学号为1001,密码为“123”时验证通过 (7)新建一个Filter,名为LoginFilter,用来拦截请求资源:list.jsp,add.jsp,具体逻辑是只有通过身份验证的学生才能访问这两个页面,否则转到登录页面login.jsp (8)新建一个监听器OnlineListener,用来统计网站在线人数和网站的访问量并显示在list.jsp页面上
时间: 2024-03-15 19:41:27 浏览: 93
这是一个比较复杂的题目,需要一定的Java Web开发经验。以下是大致的代码框架,具体实现细节可能因个人实现方式而有所不同。
1. 学生类Student
```java
public class Student {
private String number;
private String userName;
private Gender sex;
private String className;
private String department;
// 构造函数、getter和setter方法
}
public enum Gender {
MALE, FEMALE
}
```
2. 学生信息展示页面list.jsp
```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="${studentList}">
<tr>
<td>${student.number}</td>
<td>${student.userName}</td>
<td>${student.sex}</td>
<td>${student.className}</td>
<td>${student.department}</td>
</tr>
</c:forEach>
</tbody>
</table>
<a href="add.jsp">添加学生信息</a>
</body>
</html>
```
3. Servlet ListServlet
```java
@WebServlet("/list")
public class ListServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// 从数据库或其他数据源读取学生信息列表
List<Student> studentList = new ArrayList<>();
studentList.add(new Student("1001", "张三", Gender.MALE, "计算机1班", "计算机系"));
studentList.add(new Student("1002", "李四", Gender.FEMALE, "计算机2班", "计算机系"));
// 将学生信息列表存储到request域中
request.setAttribute("studentList", studentList);
// 转发到list.jsp页面
request.getRequestDispatcher("/WEB-INF/jsp/list.jsp").forward(request, response);
}
}
```
4. 学生信息添加页面add.jsp
```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>
<%-- 表单提交到AddServlet --%>
<form action="add" method="post">
<label>学号:<input type="text" name="number"></label><br>
<label>姓名:<input type="text" name="userName"></label><br>
<label>性别:<select name="sex">
<option value="MALE">男</option>
<option value="FEMALE">女</option>
</select></label><br>
<label>班级:<input type="text" name="className"></label><br>
<label>院系:<input type="text" name="department"></label><br>
<input type="submit" value="提交">
</form>
</body>
</html>
```
5. Servlet AddServlet
```java
@WebServlet("/add")
public class AddServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// 从表单中获取学生信息
String number = request.getParameter("number");
String userName = request.getParameter("userName");
Gender sex = Gender.valueOf(request.getParameter("sex"));
String className = request.getParameter("className");
String department = request.getParameter("department");
// 创建学生对象并添加到学生信息列表
Student student = new Student(number, userName, sex, className, department);
List<Student> studentList = new ArrayList<>();
studentList.add(student);
// 将学生信息列表存储到ServletContext域中
getServletContext().setAttribute("studentList", studentList);
// 重定向到list.jsp页面
response.sendRedirect(request.getContextPath() + "/list");
}
}
```
6. Servlet LoginServlet
```java
@WebServlet("/login")
public class LoginServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// 验证学生身份
String number = request.getParameter("number");
String password = request.getParameter("password");
if ("1001".equals(number) && "123".equals(password)) {
// 登录成功,将学生信息存储到session域中
HttpSession session = request.getSession();
session.setAttribute("number", number);
session.setAttribute("userName", "张三");
// 重定向到list.jsp页面
response.sendRedirect(request.getContextPath() + "/list");
} else {
// 登录失败,重定向到login.jsp页面
response.sendRedirect(request.getContextPath() + "/login.jsp");
}
}
}
```
7. Filter LoginFilter
```java
@WebFilter(filterName = "LoginFilter", urlPatterns = { "/list.jsp", "/add.jsp" })
public class LoginFilter implements Filter {
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException {
HttpServletRequest req = (HttpServletRequest) request;
HttpServletResponse resp = (HttpServletResponse) response;
HttpSession session = req.getSession();
String number = (String) session.getAttribute("number");
if (number != null && "1001".equals(number)) {
// 学生已登录,放行请求
chain.doFilter(request, response);
} else {
// 学生未登录,重定向到login.jsp页面
resp.sendRedirect(req.getContextPath() + "/login.jsp");
}
}
}
```
8. 监听器 OnlineListener
```java
@WebListener
public class OnlineListener implements HttpSessionListener, ServletContextListener {
private int onlineCount;
private int visitCount;
public void sessionCreated(HttpSessionEvent event) {
onlineCount++;
}
public void sessionDestroyed(HttpSessionEvent event) {
onlineCount--;
}
public void contextInitialized(ServletContextEvent event) {
// 初始化网站访问量为0
ServletContext context = event.getServletContext();
context.setAttribute("visitCount", 0);
}
public void contextDestroyed(ServletContextEvent event) {
}
public void requestInitialized(ServletRequestEvent event) {
// 每次请求时增加网站访问量
ServletContext context = event.getServletContext();
Integer count = (Integer) context.getAttribute("visitCount");
context.setAttribute("visitCount", count + 1);
}
public void requestDestroyed(ServletRequestEvent event) {
}
public void sessionDidActivate(HttpSessionEvent event) {
}
public void sessionWillPassivate(HttpSessionEvent event) {
}
}
```
需要注意的是,为了保证网站的安全性,不能将Servlet和JSP页面直接放在Web应用的根目录下,需要将它们放在WEB-INF目录下,这样外部用户就无法直接访问它们。同时,为了避免重复提交表单,可以在AddServlet中进行表单重复提交校验。
阅读全文