Employee list with Servlet + JSP (Model 2)

Model 2 offers some benefits over simple Model 1 approach, for example the data-preparation code can be pulled out of JSP file into a servlet, leaving JSP with purely presentation tasks. This makes JSP pages smaller, cleaner and simpler. Also, a servlet can easily swap one presentation with another by changing the name of a JSP page it forwards too. Overall design of Model 2 application is more structured and flexible, though requires more files and more configuration.

This application produces exactly the same employee list as original JSP application.

employee_list.gif

When request is sent from the browser, the servlet gets called first. It contains all data preparation tasks and looks like this:

{{{public class EmployeeListServlet extends HttpServlet {

} }}}

After the request is processed by the servlet, the servlet forwards to "/jspservlet/employees.jsp" page, which displays the result:

{{{<%@ page contentType="text/html;charset=UTF-8" language="java" %>

<html>

</html>}}}

Notice that data access code is removed and the page looks almost like regular HTML. This simplifies making changes to the markup of this page in an HTML editor.

Next: converting servlet-based application into Struts application

StrutsQuickStartServletJSP (last edited 2009-09-20 23:12:22 by localhost)