I will print the items from a list on a jsp page. There is one item in the list. Servlet code:
@WebServlet("/BlogListServlet")
public class BlogListServlet extends HttpServlet{
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
Blog blog= (Blog) getServletContext().getAttribute("blog");
Map<Integer, Post> posts= blog.getPosts();
List<Post> list = new ArrayList<Post>(posts.values());
req.setAttribute("posts",list);
resp.sendRedirect("bloglist.jsp");
}
}
jsp:
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@page contentType="text/html" pageEncoding="UTF-8" %>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>BlogPost</title>
<link rel="stylesheet" type="text/css" href="css/blogpost.css"/>
</head>
<body>
<table>
<c:forEach var="post" items="${posts}">
<tr>
<td>${post.name} - ${post.jaar}</td>
<td>${post.specialisatie}</td>
<td>${post.omschrijving}<br/><a href="${post.url}">${post.url}</a></td>
</tr>
</c:forEach>
</table>
</body>
</html>
result in the browser:
${post.name} - ${post.jaar} ${post.specialisatie} ${post.omschrijving} ${post.url}