Java Reference
In-Depth Information
B In this program, we will switch back and forth between model and view logic. This
block of code, enclosed within the <% %> brackets, contains the initialization for
the model language. It declares the variables and imports that will be used to han-
dle the database logic.
C We switch to view logic. This is HTML -tagged text, and it handles the formatting
of the entire page, up to the table headers.
D We switch, once again, back to the model language. Each switch is distracting to
the overall flow of the application. This model logic is Java code that prints the
individual rows of the table.
E With one final switch back to view logic, we clean up by closing the table, body,
and HTML sections.
This application is only marginally better than the initial Magic Servlet. It has
many of the same troublesome characteristics, including poor separation of
concerns and difficult maintenance. Many readers of this topic will open the
third chapter, skim it, and see “… some irrelevant text … Magic Servlet … still
more irrelevant text … solution: refactor … blah, blah, blah JSP .” A JSP , by
itself, is not a complete architecture; it is merely one of many tools in our bag
of tricks that can be used to craft a complete architecture. We must also define
how it interacts with the rest of the application and how external data is incor-
porated, and we must determine the scope of its responsibility.
Solution: Refactor to Model-View-Controller
It should be fairly obvious by now that the refactored solution is the refac-
tored solution from the previous chapter. In general, this procedure can be
used to methodically refactor these beasts:
Steps to refactor a monolithic JSP
A poorly structured JSP is every bit as damaging as a servlet without good structure.
These steps can be used to refactor the solution.
Create a template for a controller object. This should be a generic
HTTPServlet , with the performTask similar to the one in listing 3.4.
Identify all major areas that build dynamic content. These will be the
commands. Create a command template for each major area of
dynamic content.
Within the JSP , for each command, create a bean tag in the JSP , within
the body, that looks like this:
Search WWH ::

Custom Search