|
|
![]() |
|
|
How to Interact Directly with JavaBeans from JSPs by Richard Shaler [The code for this article is available for download.]
JavaServer Pages are powerful. However, because of their power, it's easy to use them for more than their intended purpose--presentation. It is often easier to embed some of your business logic in a JSP rather than in a JavaBean, where it belongs. You can resist the temptation by knowing how to interface JSPs with JavaBeans. Here, I show you how to create a JavaBean and how to interact directly with that JavaBean from a JSP. Powerful, But Is It Reusable? The fact that you can embed Java code in a JSP makes JSPs very powerful. But too much Java code in a JSP can quickly decrease the reusability of your application components, because you're embedding functions in a JSP that can't really be tapped by other application components. Ideally, JSPs are used only for presentation logic. Business logic should be contained in JavaBeans or possibly in Enterprise JavaBeans. In some of my previous JSP articles in Guild Companies I showed you how to componentize a JSP-only application by creating additional components, namely servlets and JavaBeans. Here, I present a componentized application, but I eliminate the servlet, letting the JSP interact directly with the JavaBean. Of course, the application is simple enough that it eliminates the need for a servlet. However, even with complex applications there may be times when a JSP can interact directly with a JavaBean. To illustrate JSP/JavaBean interaction, I present a sample application made up of two components: a JSP and a JavaBean. The application (see Figure 1) displays the name and e-mail address for a contact.
By examining this application, you learn how to link a JavaBean to a JSP and how to set and retrieve the properties of a JavaBean. This application source code is available in the download package for this article. It will be helpful for you to have the source code available as you read the article. Before we look at the application, I'll describe JavaBeans and why they are so useful to a well-designed application. JavaBeans A JavaBean is a standard Java class that conforms to some simple design and naming conventions. The idea behind conforming to a standard is to promote reusability--a JavaBean is by nature reusable. For example, a customer contact would be represented by a JavaBean. Any user needing information about the contact would retrieve it through the JavaBean's properties and methods. Your application's component (in this case a JSP) interaction with a contact will always be through the JavaBean class. Assuming the JavaBean is well-written and conforms to JavaBean standards, manipulation of the contact object will be easy, consistent, and predictable. In the case of a JSP, JavaBean interaction occurs through several standard JSP statements that use a simple XML syntax. Even an HTML programmer who might work with your JSP should be able to understand them. JavaBean Conventions The following material describes four basic conventions that are accepted standards for designing your JavaBeans:
To illustrate the conventions mentioned above, I've included the complete source code for the JavaBean used by the sample application. The JavaBean's name is ContactBean (contained in the file ContactBean.java). If you study the source code you can see that the JavaBean adheres to all of the conventions mentioned above. The JavaBean is extremely simple but serves well as an illustration of JavaBean conventions. Normally, the JavaBean would contain some code to retrieve data from a persistent relational database, such as DB2, in order to set its properties. Now, I'll show how easy it is to link a JSP to a JavaBean to access its properties. Interacting with a JavaBean from a JSP Even though you could use standard Java code to interact with a JavaBean from a JSP, you wouldn't want to, because the Java code would unnecessarily add complexity to your JSP. Instead of using Java code, you can use special JSP action tags to either link to a JavaBean, set its properties, or get its properties:
In the following two sections, I'll describe the special JSP action tags and how to use them to exploit JavaBeans from your JSPs. Linking to a JavaBean To link a JSP to a JavaBean, you use the <jsp:useBean> action tag. To link the DisplayContact JSP in my sample application to the ContactBean, I used the following JSP statement: <jsp:useBean id="contact" scope="page" class="com.itechtutor.ContactBean" /> The action in this statement is useBean, which, of course, indicates the JSP page is to use a JavaBean. The id attribute creates a scripting variable that can be used to reference the JavaBean, in this case contact. Any reference to the JavaBean can be made through this id and is similar to other Java variables, in that it's case-sensitive. The scope of the id is dependent on the scope attribute. The scope attribute is important because it indicates how long the JavaBean's properties will be available to your application. There are four possible values: page, session, request or application. Page has the most limited scope and application has the greatest scope. If you only needed a JavaBean property for a specific page (as is the case in my sample), page scope will work. If you wanted a JavaBean property to be available as long as your application is running, even if someone starts a new browser session, you would use application (if, for example, you were using connection pooling in your JSP where the pooling was being handled by a JavaBean). (To learn more about scope, see the JSP specification on Sun Microsystems' Web site. From Sun's home page, perform a search on the phrase JavaServer Pages Technology. From the JavaServer Pages Technology home page, you should find a link to the JavaServer Pages Specification, which can be downloaded in a PDF format.) The class attribute is used to specify the literal name of the JavaBean and its package name. In the sample application presented here, the package name is com.itechtutor and the JavaBean name is ContactBean. Accessing JavaBean Properties Because my sample application is simple, my JSP sets the properties of the JavaBean before it gets them. (As mentioned earlier, the JavaBean would normally set its own properties, most likely from a persistent database.) To set a property in a JavaBean from a JSP, use the setProperty action: <jsp:setProperty name="contact" property="name" value="Richard Shaler" /> The code above is from the DisplayContact JSP used by the sample application. This statement pretty much describes itself. It's obvious that the contact's name property is being set to "Richard Shaler." This illustrates how using JSP tags can make a JSP intuitive to a non-JSP programmer. To get a JavaBean property, use the getProperty action: <jsp:getProperty name="contact" property="name" /> You can use a JSP expression tag as an alternate method to display JavaBean information to a browser. It uses the JSP expression element: < %= contact.getName()%> The plus in using the JSP expression is that it is brief. The negative is that it may not be intuitive to an HTML programmer. The XML syntax of the getProperty action is more intuitive to a non-JSP programmer. All three action tags are easy to understand and use. Even an HTML programmer with no experience with JSP could probably understand what the statements are doing. Keep Them Simple JSPs need to stay simple and, ideally, should not do more than present content to a browser. Since a JSP's main purpose is presentation, it would be beneficial to turn over your JSP development and maintenance to the presentation specialist, the HTML programmer. Minimizing Java code in your JSPs by using JavaBeans should provide you with that option. Here, I described the JSP action tags used to exploit JavaBeans. But JSP tags can be used for a lot more. You can create your own JSP tags to access any Java code you want, while making that code portable. In my next article, I'll describe how to create custom JSP tags.
|
Editors
Contact the Editors |
|
Last Updated: 7/18/02 Copyright © 1996-2008 Guild Companies, Inc. All Rights Reserved. |