A very simple JSP-architecture
by Keld H. Hansen
There's a lot of talk these days about J2EE and other advanced
Java-architectures, and it's sure hot stuff--and interesting and
relevant too. But you shouldn't forget that many smaller applications
are best (and fastest) built using a simpler architecture.
This article will present a simple JSP-architecture and also a
handful of useful techniques that will help you build
web-applications fast, without losing focus on quality. If you are
building prototypes, smaller applications, or simply want to
experiment with Java-technologies, then this might be the
architecture for you. The building blocks are JSP-pages and JavaBeans
in an MVC-architecture. No servlets are used. This makes coding very
fast, and has only one drawback as I see it: some errors in JSP-pages
are very poorly diagnosed by today's application servers (servlet
engines). When better JSP syntax checkers are built into the
development tools this will probably become a minor issue.
The MVC architecture
The Model-View-Controller architecture is today a de-facto
standard used in thousands of applications. The main idea behind this
architecture is to separate business logic, presentation and program
flow in a simple manner. In JSP-applications you may implement the
MVC-architecture by:
The "C" (Controller): coding a controller that
- handles common tasks like authentication
- examines the requests from the user
- invokes JavaBeans
- controls error handling
- controls module flow
The "M" (Model): coding a set of JavaBeans to handle the database
access (or access to other external resources)
The "V" (View): coding a view for presentation of the data fetched
by the beans
The controller is often programmed as a Java Servlet, but in this
article we will use the simpler JSP-format, which is also commonly
used for the view. Our application is therefore simplified by only
containing JSP-pages and JavaBeans.
For each window--or page--in the application you'll need one
controller and one view, and one or more JavaBeans. So we'll code a
JSP-page for each controller and one for each view. But, don't be
tempted to omit the controller for simpler web-pages, even if you
could combine the controller and view into one file. Maintenance and
further development on your application is greatly simplified if you
always stick to the controller/view-pairs.
A command-driven controller
Another principle in our design is to let "commands" passed to the
controller drive the application. A command will typically map to
each "function" that is available on a web-page. Every controller
will therefore start with identifying the request by looking at the
command received. Since a request is most often initiated from a
view-page shown in the user's browser, the command is typically sent
to the controller by one of two techniques: either you send the
command as part of the URL--e.g.
ctrlcustomer.jsp?command=lookup
Or you submit the command in a field (often hidden) in an
HTML-form.
More principles to follow
Before continuing with an example let's decide on a few other
useful techniques to be used:
- A JSP error-page will be used to handle
exceptions in a standardized way
- We'll use the Session object to store data that is not already
stored in JavaBeans. A naming standard that we'll use for this data
is <name-of-page>.<name-of-item> -- e.g.
customer.address.
- We'll use a simple Java utility method called "cString" that will
convert nulls to empty strings. Since form data--and often also data
in the Session object--are stored as strings this makes it
unnecessary to check for null-values.
Keld is a web architect working for one of the largest IT-companies in Denmark. He has more
than 30 years of experience within the computer industry.
New on the Java Boutique:
New Review:
Time Management Made Easy with the Quartz Enterprise Job Scheduler
Why not just use the Java timer API? This open source scheduling
API boasts simplicity, ease-of-integration, a well-rounded feature
set, and it's free!
New Applet:
Reverse Complement
Reverse Complement is a simple applet that converts DNA or RNA
sequences into three useful formats.
Elsewhere on internet.com:
WebDeveloper Java
Lots of Java information on webdeveloper.com
WDVL Java
Thorough Java resource at the Web Developer's Virtual Library.
ScriptSearch Java
Hundreds of free Java code files to download.
jGuru: Your View of the Java Universe
Customizable portal with online training, FAQs, regular news updates, and tutorials.
|