- Model: A POJO (Email.java)
- View: A JSP (email-list.jsp)
- Controller: A Servlet (EmailListServlet.java)
Email.java (The Model)
public class Email { private String from; private String subject; private String message; public Email(String from, String subject, String message) { this.from = from; this.subject = subject; this.message = message; } // Getters and Setters... }
email-list.jsp (The View)
<%@ 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 PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <title>Email List</title> </head> <body> <table> <tr><th>From</th><th>Subject</th><th>Message</th></tr> <c:forEach var="email" items="${emails}"> <tr> <td>${email.from}</td> <td>${email.subject}</td> <td>${email.message}</td> </tr> </c:forEach> </table> </body> </html>
EmailListServlet.java (The Controller)
public class EmailListServlet extends HttpServlet { private static final long serialVersionUID = 1L; @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { List<Email> emails = yourMethodThatReturnsEmailList(); req.setAttribute("emails", emails); getServletContext().getRequestDispatcher("/email-list.jsp").forward(req, resp); } }
The servlet mappings in web.xml
<servlet> <servlet-name>EmailList</servlet-name> <servlet-class>com.test.servlet.EmailListServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>EmailList</servlet-name> <url-pattern>/email</url-pattern> </servlet-mapping>
Then just access the servlet i.e www.mydomain.com/email