Home arrow Java & J2EE arrow Page 4 - Overview of Java Web Technologies, Part 2

Calling a Bean from a JSP Page - Java

Today, Budi walks us through a refresher and brief overview of server JSP programming. Today's portion covers JavaServer Pages (JSP), with a thorough overview of JavaBeans and Tags. This excerpt comes from chapter one of JavaServer Faces Programming, by Budi Kurniawan (McGraw-Hill/Osborne, ISBN 0-07-222983-7, 2004).

TABLE OF CONTENTS:
  1. Overview of Java Web Technologies, Part 2
  2. Sun's Solution
  3. JSP and JavaBeans
  4. Calling a Bean from a JSP Page
  5. Accessing Bean Properties
  6. Custom Tags
  7. Developing and Using Custom Tag Libraries
  8. Writing a Tag Handler
  9. Writing and Using Tags
  10. Model 2 Architecture
By: McGraw-Hill/Osborne
Rating: starstarstarstarstar / 9
March 03, 2004

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

Before you can use a bean in your JSP page, you must make the bean available, using the jsp:useBean action element. This element has attributes that you can use to set the bean’s properties. The syntax for the jsp:useBean element has two forms:

 
<jsp:useBean (attribute="value")+/> 

 
<jsp:useBean (attribute="value")+> 
initialization code 
</jsp:useBean

You use the first form if you do not need to write any initialization code, and the second form if you do. The (attribute=“value”)+ part of the code means that one or more attributes must be present. The five attributes that can be used in a jsp:useBean action element are as follows:

  • The id attribute defines a unique identifier for the bean. This identifier can be used throughout the page and can be thought of as the object reference for the bean. The value for the id attribute has the same requirements as a valid variable name in the current scripting language.

  • The class attribute specifies the fully qualified name for the JavaBean class. A fully qualified name is not required if the bean’s package is imported using the page directive, however.

  • If the type attribute is present in a jsp:useBean element, it specifies the type of the JavaBean class. The type of the bean could be the type of the class itself, the type of its superclass, or an interface the bean class implements. Normally, this attribute isn’t often used and you use the class attribute instead.

  • The scope attribute defines the accessibility and the life span of the bean. This attribute can take one of the following values: page, request, session, or application. The default value of the scope attribute is page. The scope attribute values control how long the bean will continue to exist, as follows:

    • page The bean is available only in the current page after the point where the jsp:useBean action element is used. A new instance of the bean will be instantiated every time the page is requested. The bean will be automatically destroyed after the JSP page loses its scope; that is, when the control moves to another page. If you use the jsp:include or jsp:forward tags, the bean will not be accessible from the included or forwarded page.

    • request The accessibility of the bean is extended to the forwarded or included page referenced by a jsp:forward or jsp:include action element. The forwarded or included page can use the bean without a jsp:useBean action element. For example, from a forwarded or included page, you can use the jsp:getProperty and jsp:setProperty action elements that reference the bean instantiated in the original page.

    • session The bean scope applies to the user’s session object. The instance of the bean will continue to exist as long as the user’s session object exists. In other words, the bean’s accessibility extends to other pages. Because the bean’s instance is put in the session object, you cannot use this scope if the page on which the bean is instantiated does not participate in the JSP container’s session management.

    • application This scope lives throughout the life of the JSP container itself. The bean is available from any resource in the application.

  • The beanName attribute represents a name for the bean that the instantiate method of the java.beans.Beans class expects.

Either the class attribute or the type attribute must be present.

For example, here is how you use the jsp:useBean element to instantiate a bean called ch01.LongBean:

 
<jsp:useBean id="theBean" class="ch01.LongBean"/> 

Alternatively, you can import the package ch01 using a page directive and refer to the class using its name, as follows:

 
<%@ page import="ch01" %> 
<jsp:useBean id="theBean" class="LongBean"/> 

NOTE: The bean is available in the page after the jsp:useBean action element. It is not available before that point.

As an example, lets go through the process of using a simple bean on a JSP page. Listing 6 shows a Java class called ch01.AdderBean.

Listing 6 The ch01.AdderBean Class

 
package ch01
public 
class AdderBean 
public int add
(int aint b) { 
return 
(b); 

}

After you create the AdderBean, you need to compile it to obtain the AdderBean.class file. Then you need to copy the bean class file to the classes directory under WEB-INF under your application directory. The deployment must take into account the package name. In this case, you need to create a directory called ch01. Copy your AdderBean.class file to this ch01 directory. Once you have a JavaBean ready and its class file stored in the proper directory, you can use it from a JSP page. Now, you can create a JSP page that calls the bean, as shown in Listing 7.

Listing 7 Calling a Bean from a JSP Page (callBean.jsp)

 
<jsp:useBean id="theBean" class="ch01.AdderBean"/> 
<html
<head></head
<body
<% 
int x 
4
int y 

int result 
theBean.add(xy); 
out
.print("4+5=" result); 
%> 
</body
</html

After you create the JSP page, start or restart Tomcat and direct your browser to the URL and to the JSP page you just wrote. Your browser should display the following string:

 
4+5=

NOTE: You can also use a JAR for your JavaBean. In this case, you must copy the .jar file into the lib directory under the WEB-INF directory of your application directory to make it available to JSP pages in your application.

Buy this book now!Remember: This is part two of the first chapter of JavaServer Faces Programming, by Budi Kurniawan (McGraw-Hill/Osborne, ISBN 0-07-222983). Stay tuned for more chapters of developer books from McGraw-Hill/Osborne.
Buy this book!



 
 
>>> More Java & J2EE Articles          >>> More By McGraw-Hill/Osborne
 

blog comments powered by Disqus
   

JAVA & J2EE ARTICLES

- More Java Bugs Lead to More Attacks
- Oracle's Java One Brings News, Surprises
- Oracle Patches Java Runtime Environment
- Apple Syncs Java Update with Oracle
- Spring 3.1 Java Development Framework Compat...
- Jelastic Java PaaS Availability and Pricing ...
- NetBeans 7.1 Released, Supports JavaFX 2
- SolarWinds Releases Newest Version of Java M...
- Free Monitoring Tool for Java Apps on Heroku
- Heroku Adds JCloud Platform Support, Java 7 ...
- Java SE 8 Speculation in Full Swing
- Java SE 7 Now Available
- New JVM Language and Java Reporting Tool
- Java 7 Release Update and New Eclipse Toolkit
- The Best Java Netbeans IDE Plugins

Developer Shed Affiliates

 



© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap

Dev Shed Tutorial Topics: