advertisement
javaboutique
Search Tips
Articles  |   Tutorials  |   Reviews  |   Tools  |   by Category  |   by Date  |   by Name  |   Submit  |   Source  |   Forums  |  
javaboutique
Browse DevX


Partners & Affiliates











advertisement


Tutorials : : A very simple JSP-architecture Part 2
Contents
Introduction
Enhancing the Customer-Lookup application
The View

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

How to Add Java Applets to Your Site

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.

 Microsoft Visual Studio 2010 Showcase
 Avaya Developer Showcase
 MSDN Spotlight
 PHP for Windows Showcase
XML error: undefined entity at line 39
advertisement
Receive Articles via our XML/RSS feed
Receive Articles via our XML/RSS feed

JavaBytes
Internet Cyclone
This powerful, easy-to-use, internet optimizer is for Windows 95, 98, ME, NT, 2000 and XP. It's designed to automatically optimize your Windows settings, boosting your Internet connection up to 200%.

Windows 7: From Beta to Final Code in One Year
Google Shows Off Chrome OS, Releases Source
Microsoft Shows Off Silverlight 4, IE9 Plans
Metasploit Expands Vulnerability Test Framework
HyperCard Reborn?
Fedora 12 Takes Aim at Linux Networking
Top Supercomputer Nearly Doubles in Speed
Fedora 12 Linux Tackles Virtualization
Apple Gives iPhone Developers App Status Tracker
Novell Sets OpenSUSE 11.2 Free

Creating Custom Export Filters for StarOffice with XSLT
WPF Wonders: Using DataTemplates
Crystal Reports Family Offers Options for Developers
Avaya Aura Session Manager video
Avaya Aura Overview video
Exploring HTML 5's Audio/Video Multimedia Support
Overriding Virtual Functions? Use C++0x Attributes to Avoid Bugs.
Understanding the Cloud Computing Security Vulnerabilities
Cisco and IBM Target a Greener World
Upgrade to Visual Studio 2010 with the Ultimate Offer

Advertising Info  |   Member Services  |   Contact Us  |   Help  |   Feedback  |   Site Map  |   Network Map  |   About

internet.commediabistro.comJusttechjobs.comGraphics.com

Search:

WebMediaBrands Corporate Info

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | Shopping | E-mail Offers | Freelance Jobs