Planning Your JSP-MVC Application: Building The Controller
Planning Your JSP-MVC Application: Building The Controller
The first step in using JSP-MVC, as with in any program, is to plan. Primarily, you will
need to break up the design (View) logic (Model), and Controller issues. It might help to
actually draw a flowchart of these pieces and how they will all work together.
The user interface is simple enough, and can be represented by simple HTML layout
screens. It might be easiest to start with these. For example, in a shopping cart, you have
the category display page, the search screen, the search results, and the form interfaces
for the checkout process.
The Model section is a little more abstract, but can be done fairly easily. Your primary
question in an all-JSP application is where to draw the lines. For example, querying the
database to obtain the data for the product category display page in your shopping cart
would technically be considered part of the Model. It might be easier (especially if you
are using custom actions from your app server vendor) to simply include this in the View,
however. Just be careful. If, for example, you want to change the database of product
categories from Access to MS SQL Server, or even to data from a JavaBeans component,
will this be difficult? If the answer is yes, then break it out. This is the whole point of the
MVC method.
The Controller is probably the most abstract section of your code. This single JSP page
controls everything in your application. The information passed from your users will
always be sent to this page first. So you should make sure that this page is organized and
well-commented. Remember, there should be no actual logic here, mostly just flow
control. Checking for certain variables, checking security; then including files or
redirecting to the appropriate display page.
So, the next section of your Controller page will be made up of if/else blocks (or case
blocks) that check this action variable:
<% // first check to make sure there is an action
// if not, do default action
if (request.getParameter("action") == null) { %>
<% }
// check to see if user is adding item to cart
else if (request.getParameter("action").equals("additem")) { %>
<% } %>
<%-- add more else ifs for all of your processes and a final else in
case of an error --%>
You can see how this will work by checking what the action is. You can even have a
number of substeps within a single action (just remember to indent your ifs, and
comment your code well). The key to remember is to keep all display and data handling
out of this template.
The final step will be to create your actual display and processing pages. For display (or
View) pages, remember that you will have your header and footer already included, so
only the actual interface of the application step needs to be designed in the page.
In the processing (Model) pages, you will handle your processes; then reforward to the
Controller with a new action value. For example, when you are adding an item to the
shopping cart, do so and then forward the user to /index.jsp?action=displaycart, so
that this will display the contents of the cart. You can even include additional variables
using <jsp:param>, which can trigger additional options—such as displaying a message
confirming that the appropriate item has just been added to the cart.
Overall, you will find that using an organized approach such as the MVC method to your
JSP application will enable you to develop Web applications that are scalable and highly
modular. Using this method, your JSP applications will be easy to update, easy to break
up (for the purpose of scaling), and easier to maintain overall.