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).
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:
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:
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 6The ch01.AdderBean Class
package ch01
; public class AdderBean { public int add(int a, int b) { return (a + 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 7Calling a Bean from a JSP Page (callBean.jsp)
<jsp:useBean id="theBean" class="ch01.AdderBean"/> <html> <head></head> <body> <% int x = 4; int y = 5 int result = theBean.add(x, y); 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=9
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.
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!