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 :
Contents
Introduction
The "Customer Lookup" application
The "view"

The "view"

The purpose of the view is to present the data acquired by the bean called from the controller. This means that when the view is called, all the "critical" operations have taken place. It is important that such operations have taken place since errors that occur when HTML is being sent to the browser can result in corrupted pages--meaning unreadable pages with strange error messages in the browser.

Generally speaking "nothing should go wrong in the view". Well, any experienced programmer knows that anything can go wrong when a computer is involved, but you should use the "nothing should go wrong"-phrase as a guideline when you "code" the view. Don't attempt to access any databases, and avoid any lengthy Java code. In the view you gently pick up the data in the beans and maybe from the Session object and mix them with your HTML.

In practice it's difficult to avoid inserting Java code in the view, but if you have to, then always consider if this code could be moved to the controller--or into a bean. Java code can go wrong when executed, so try to avoid it. Another possibility outside the scope of this article would be to use tag libraries to hide Java code.

The structure of the JSP-view

Let's see what our view looks like:

	
	<%@ page import="java.util.*" %>
	
	<%@ page errorPage="error.jsp" %>
	
	<jsp:useBean id="customer" class="hansen.playground.Customer" scope="request" />
	
	<%@ include file = "util.inc" %>
	
	<html>
	 <head>
	 <title>Customer info</title>
	 </head>
	 <body>
	 <h3>CUSTOMER SEARCH</h3>
	 <b><%=cString(session.getValue("customerinfo.message"))%></b>
	
	 <form name=myform action="ctrlcustomerinfo.jsp"	method=post>
	 Please enter the customer ID: <input type=text name=id value="<%=customer.getId()%>">
	 <input type=submit value="Search">
	 <input type=hidden name=command value="getdata">
	 </form>
	 Name: <%=customer.getName()%><br>
	 Address: <%=customer.getAddress()%><br>
	 </body>
	 </html>

Note the following in the code:

  1. The name and address of the customer is taken from the bean using get-methods. Since the get-methods are simple wrappers of the bean's properties, this is a rather safe operation.
  2. The message text on top of the page is taken from the Session object.
  3. When the use clicks the "Search" button, the form is submitted to the controller. It only contains the ID that the user has entered plus the action "getdata" through a hidden field.
  4. The "post" method is used in stead of "get" simply to avoid showing the data from the form in the browser's address field.
  5. If you like you could check if the user has entered a valid ID before you do the submit. I'd advise to at least to check if the ID-field has been filled in. This is simple to do by some JavaScript code:
    	 <script>
    	 function filled() {
    	   return (myform.id.value != "")
    	 }
    	 </script>
    

    and you'd have to include this in the FORM-tag:

    	
    	 onsubmit="return filled()"

Running the application

To start the application you enter a URL invoking the controller with the command "prompt":

	
	ctrlcustomerinfo.jsp?command=prompt

All files used in the application can be found in this zip-file.

In a forthcoming article I'll show how this architecture facilitates adding new features to the "Customer Search" page.

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.

 Avaya Developer Showcase
 MSDN Spotlight
 PHP for Windows Showcase
XML error: undefined entity at line 34
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%.

IBM Brings Developers Into the Cloud
Apache at 10: You Can't Buy Us
Microsoft's CodePlex Foundation Moving Forward
Apple Claims 100,000 Apps, Google Analyzes Them
Nokia Latest to Play Opera Mobile 10 Browser
PayPal Opens Up Payment Platform to Devs
Ubuntu Linux 9.10 'Karmic Koala' Starts Its Climb
IBM Links Rational Developer Tools, Tivoli Apps
Libraries Give Vista Apps a Windows 7 Look
Ubuntu: The 'Default Alternative' to Windows?

Delivering Web-based Embedded Fonts in CSS 3
Adobe Helps PHP Developers Create Rich Internet Applications
Java Developers Finding a Home at Adobe Flex
Virtualization Delivers a Dynamic Infrastructure
Consuming XML Web Services in iPhone Applications
Build a More Agile Business with IBM
POJO-Based Solutions for LDAP Access: One Good, One Better
IBM Offers Enhanced Measurement and Management for Energy Usage
IBM Helps Transformation to an Information-Based Enterprise
Top Five Touch UI-Related Design Guidelines

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, Reprints, Permissions, Privacy Policy.
Advertise | Newsletters | Shopping | E-mail Offers | Freelance Jobs