Java Reference
In-Depth Information
Antipattern: Monolithic JSPs
Faced with such a daunting list of problems, a programmer may choose to
rewrite a poorly designed, pure tag-language application using a more robust
development language, such as JSP s. The temptation to build the same flawed
architecture with the new technology can be overwhelming. After all, the JSP
specification includes a tag language. This antipattern is not limited to applica-
tion rewrites. The community of Java programmers has diverse roots, and
some are bound to come from tag-language backgrounds. Given this experi-
ence base, writing programs in this style seems natural.
Even if we don't have a tag-language background and are armed with the
proper knowledge, sometimes we just get lazy. If we plan to build a JSP by
hand, then making one scripted JSP page with no method or class definitions
and inline print statements is easier than taking the time to build a command
bean, an interaction controller, and a JSP . However, keep in mind that the
extra effort will be paid back with interest over time. Maintenance and user
interface design are dramatically improved with a clean separation of concerns.
This program lacks model-view separation
Let's revisit our bulletin board example from chapter 3. This time, I've refac-
tored it in the wrong direction in order to show the prototypical monolithic
JSP . The antipattern is similar, but the “feel” of the application is different. In
this case, it is easy to see the flow of the user interface. The model, however, is
poorly defined and awkward. The tag-language environment simply does not
allow the model's design to be cleanly partitioned in the way pure Java would.
Consider the program in listing 4.1. This antipattern is really just another ver-
sion of the Magic Servlet, but many programmers who would never even
think of building something so ugly will choose to build monolithic JSP s with
no reservations. I found three different JSP tutorials on the Internet that basi-
cally taught this model!
Listing 4.1
An example of the Monolithic JSP
<%@ page import="java.sql.*" %> B Model specific initialization
// instance variables for connection
ResultSet result;
Connection connection = null;
Statement statement = null;
String url = "jdbc:db2:board";
Search WWH ::

Custom Search