Tutorials :
: A very simple JSP-architecture Part 2
Enhancing the Customer-Lookup application
In the first version of the application two functions were implemented:
- "prompt" … used when the window is shown the first time
- "getdata" … used when the user has entered a customer ID, and
the customer data should be retrieved
(in real life probably from a database) and presented
Now let's add the functions "Update Customer", "Delete Customer" and "Create New Customer" to the
application. This will show how the simple MVC-architecture facilitates adding new functions. First of
all we must define three new "commands" to the controller:
- "update"
- "delete"
- "create"
We also need to implement an important rule--which is actually quite general when programming
update/delete functions:
Update and delete of a customer is only allowed if the customer's data has been shown
So you'll first have to enter a customer ID, and press "Search", before you can update or delete the
customer. This is not a rule enforced by our MVC-architecture--it's just common sense.
We'll therefore define a new property--a boolean--in the Customer bean: dataFound, which can be
accessed by a new method: isDataFound. It will return true if data for the current customer ID has
been found.
We'll also give our Customer bean scope "session", so customer data acquires persistence for more than
one request:
<jsp:useBean
id="customer"
class="hansen.playground.Customer"
scope="session" />
If we don't do this we'll have to use some other means of holding data, for example the Session object
or a dedicated bean made just for holding session data. This is an important decision, but a further
discussion is out of range for this small article.
The three new functions will be implemented by three new commands, "update", "delete" and "create".
The "update" command will do this:
- check if: 1) we have data available for a customer, and 2) the customer ID matches the ID in the
form
if not, then update is not permitted (see the rule above)--else
- get the user's data from the form (name and address)
- invoke a bean to update the customer data
- setup an appropriate message ("Data successfully updated")
The controller
The controller is therefore expanded with this code:
if (command.equals("update")) {
// Update is only valid if the ID in the form matches
// the ID of the current customer
String fid = cString(request.getParameter("id"));
if (!customer.isDataFound() || !(customer.getId()).equals(fid)) {
session.putValue("customerinfo.message",
"Please search customer data first");
} else {
// Update is allowed--get new name and address from form
String name = cString(request.getParameter("name"));
String address = cString(request.getParameter("address"));
// Do the update through the bean
customer.setName(name);
customer.setAddress(address);
if (customer.updateCustomer() == 1) {
session.putValue("customerinfo.message",
"Data successfully updated");
} else {
session.putValue("customerinfo.message",
"System error - Data was not updated");
}
}
}
What is important is that this is the only change you have to do in the
controller. When a program
modification is isolated to one single spot it minimizes the risk of changing existing, working code
by mistake.
The delete function works like update, but is somewhat simpler since you don't have to fetch and set
the name and address.
The create function is more like the update function, but to see if the request is valid, we'll first
have to check that the ID is unused. If not, we fetch the name and address from the form, and call our
bean to create the customer. So it starts like this:
if (command.equals("create")) {
String nid = cString(request.getParameter("id"));
customer.setId(nid);
if (customer.readCustomerInfo() == 1) {
session.putValue("customerinfo.message",
"Customer ID already used");
} else {
// ID not used yet
(continue like "update")
. . .
The source for the complete controller is here: ctrlcustomerinfo.jsp.
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.
|