<jsp:useBean id="beanName" class="package.beanClass"
In most of my examples, the bean name and the class name are the
Identify the required attributes for each command. These will usually
be defined as database fields, transaction fields, or something similar.
You must define set methods for fields used to update the model and
get methods for fields used to display the model. Create attributes in
the commands as required and implement them.
Identify the core logic that will initialize and trigger the model's
update or query. Move these Java statements to the model's initial-
ize and execute methods, as in listing 3.4.
In the controller object, instantiate the command. This can either be
done with a basic constructor ( command = new CommandClass() ), or if
the class object needs special consideration, use the code that follows.
Assume your command's variable name is command and that your com-
mand's class is of type CommandClass :
CommandClass command =
Then, initialize and execute the command:
Forward the response object to the JSP with the following call:
ServletContext servletContext = getServletContext ();
RequestDispatcher dispatcher =
Replace the code that prints dynamic content in the original JSP with
command bean references instead, as in the JSP in listing 3.4.
Using this method, we can take a monolithic JSP and refactor it to the coveted
Triangle design pattern. In general, we are stripping out the code into three
pieces: the JSP (encapsulating the return-trip view), the command (encapsulat-
ing the model), and the servlet (encapsulating the controller). For teams