You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@struts.apache.org by Apache Wiki <wi...@apache.org> on 2007/02/21 02:59:03 UTC
[Struts Wiki] Trivial Update of "StrutsQuickStartServletJSP" by
MichaelJouravlev
Dear Wiki user,
You have subscribed to a wiki page or wiki category on "Struts Wiki" for change notification.
The following page has been changed by MichaelJouravlev:
http://wiki.apache.org/struts/StrutsQuickStartServletJSP
------------------------------------------------------------------------------
- == Employee list with pure Servlet + JSP (Model 2) ==
+ == 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.
inline: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:
- One can argue that JSTL 2.0 greatly simplifies access to dynamic data from JSP, therefore pure JSP applications look less cluttered. Nevertheless, Model 2 allows additional benefits. With Model 2 approach, the data-preparation code is located in a servlet, while JSP performs purely presentation tasks. This makes JSP pages smaller, cleaner and simpler. Also, a servlet may easily replace one presentation with another by changing the name of a JSP page it forwards too. Overall design is more structured.
-
- Here is how the servlet would look like:
{{{public class EmployeeListServlet extends HttpServlet {
@@ -15, +17 @@
{
HttpSession session = request.getSession();
- // Prepare data for output
+ // Prepare data for output, store it in the session scope
if (session.getAttribute("employees") == null) {
session.setAttribute("employees", EmployeeManager.loadEmployees());
}
@@ -28, +30 @@
}
}}}
- The request is processed by the servlet, then the servlet forwards to "/jspservlet/employees.jsp" page, which displays the result:
+ 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>
<body>
+
<table>
<tr>
<th align="left">Emp #</th>
@@ -48, +51 @@
</tr>
</c:forEach>
</table>
+
</body>
</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.
+
+ [:StrutsQuickStart1:Next: converting servlet-based application into Struts application]
+