Action Tags

In addition to the four tags seen in the previous sections, there is another special set of tags called the action tags. The use of the action tags was introduced primarily to make JSP more interactive with other components of a Web application. The use of action tags in your JSP makes writing a complete Web application easier.

There are six action tags available. The following sections discuss each of these in detail.

The usebean Action Tags

The usebean action tag is the most important action tag available:

 <jsp:usebean attribute1name=attribute1value          attribute2name=attribute2value ... /> 

Using the usebean tag, you can embed entire Java objects within the JSP file. This makes the JSP cleaner, since you can use the embedded Java class like a bean and extract or set properties to the bean using accessor/mutator methods.

The embedding of a Java bean within the JSP is an example of Model 1 architecture. In Model 1 architecture the JSP acts as the controller, handling browser requests, directing the flow of the application, and generating the output to the browser. The business logic, that is, the model in this architecture, is a set of Java classes that do the actual request processing and package the response for the view. The Java classes embedded in the usebean tag perform the same function.

The id attribute is used to give a unique identifier to the Java bean embedded in the JSP. The value of the id attribute is the unique value that the developer defines.

The scope attribute defines the scope of the Java bean embedded in the JSP. The scope attribute can have the following values:

  • page The page value for scope restricts the use of the Java bean to the current instance of the JSP page. If the JSP forwards a request to a different JSP or servlet, then the Java bean goes out of scope, that is, cannot be referenced in the JSP or the servlet.

  • request The request value limits the Java bean instance to the current request of the browser. After a new browser request is received, that is, after a new HttpServletRequest object is created, the Java bean goes out of scope.

  • session The session value for scope maintains the Java bean reference until the user session is destroyed. The Java bean can be referenced at any point in the browser's HTTP session until a new HTTP session is created.

  • application Defining the scope of the Java bean as application keeps the bean alive as long as the JSP application is running in the server. The application scope is the widest scope available and allows the embedded bean to be referenced by any JSPs or servlets within the Web application.

The class attribute is used to define the actual Java class of which the bean is an instance. An instance of the Java class given as the value of this attribute is created by the JSP engine within WebLogic Server. If the class attribute is not used, no bean instance can be created.

The type attribute is used to override the Java class type used when the bean was instantiated. Therefore, you can assign only a Java class type that is the superclass of the class used to instantiate the bean. If any other value is defined for the type attribute, the bean class will not be instantiated.

The beanName attribute is used to define a Java class data type for the bean. The value of this attribute is a fully qualified package.class name for the Java class. This attribute is equivalent to the class attribute.

The setProperty and getProperty Action Tags

The setProperty and getProperty tags are used in conjunction with the usebean tag to retrieve and set values in the embedded Java bean class.

The setProperty tag is used as follows:

 <jsp:setProperty name="beanName" property="propertyname" value="propertyvalue"/>  

It has three attributes:

  • The name attribute is used to define the name of the bean whose property needs to be set.

  • The property attribute denotes the actual property that is to be set in the bean. Multiple property attributes can be placed in the setProperty tag.

  • The value attribute is used to define the value for the propertyname defined in the property attribute. This value is set in the bean's property. Multiple values can be placed in the setProperty tag.

The getProperty tag is used as follows:

 <jsp:getProperty name="beanName" property="propertyname"/>  

It has two attributes:

  • The name attribute is used to define the name of the bean whose property needs to be retrieved.

  • The property attribute denotes the actual property that is retrieved from the bean. Multiple property attributes can be placed in the getProperty tag.

The include Action Tag

The include action tag is the XML equivalent of the include directive that you saw earlier.

 <jsp:include file="nameoffile"/>  

The forward Action Tag1

The forward tag is used to perform communication between the JSP and a servlet or another JSP. The forward tag has one attribute: the page attribute. You can set a JSP filename or a servlet name as the value for the page attribute. The JSP engine uses this tag to delegate processing to the JSP or servlet specified as the value in the page attribute.

 <jsp:forward page="pagetobecalled"/>  

The plugin Action Tag

A less frequently used action tag is the plugin tag. The plugin tag is used to specify any embedded applets in the JSP page:

 <jsp:plugin attributename1=attributevalue1 attributename2=attributevalue2 .../>  

You can refer to the JSP specification 1.2 available on the java.sun.com Web site to find more information on JSP tags.



Sams Teach Yourself BEA WebLogic Server 7. 0 in 21 Days
Sams Teach Yourself BEA WebLogic Server 7.0 in 21 Days
ISBN: 0672324334
EAN: 2147483647
Year: 2002
Pages: 339

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net