老师帮忙检查下,谢谢!
来源:2-13 自由编程
爬海东56
2020-04-25 12:47:50
package com.imooc.jstl;
public class Notice {
private String id;
private String name;
public Notice(String id, String name) {
super();
this.id = id;
this.name = name;
}
public Notice() {
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return id+"————"+name;
}
}
package com.imooc.jstl;
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;
/**
* Servlet implementation class Hw4
*/
@WebServlet("/hw4")
public class Hw4 extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public Hw4() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
List<Notice> list=new ArrayList<>();
list.add(new Notice("001", "一定要多写代码,多联系哟!"));
list.add(new Notice("002", "欢迎学习Java Web课程!"));
list.add(new Notice("003", "欢迎来到慕课网!"));
request.setAttribute("list", list);
request.getRequestDispatcher("/hw4.jsp").forward(request, response);
}
}
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>公告展示</h1>
<c:forEach items="${requestScope.list }" var="l" >
<h2>${l }</h2>
</c:forEach>
</body>
</html>
1回答
同学的代码完成的不错,继续加油。
祝:学习愉快~
相似问题