Tags that Define Script Variables

Now that you know the basics of custom tags, you can start developing more functional tags for use in your applications. The first feature you will study is that of creating scripting variables that can be used on the Web page.

A scripting variable can be added to the page context using the setAttribute() method of the instance variable called pageContext that points to the javax.servlet.jsp.PageContext object of the Web page. The setAttribute() method takes the following parameters:

  • The name of the scripting variable. This name is used to refer to the variable on the Web page.

  • The object reference to the variable to be added to the page.

  • The scope of the variable that can be one of the following constants defined in PageContext:

    • APPLICATION_SCOPE Available until the context is reclaimed

    • PAGE_SCOPE Available until the current page processing completes (this is the default)

    • REQUEST_SCOPE Available until the current request completes allowing the variable to be used by other pages should this page forward the HTTP request

    • SESSION_SCOPE Available to all pages in the current session

The following example adds a String variable to the context under the name "title" and available to this page and all forwarded pages in this request:

String s = "Example Title";
pageContext.setAttribute("title", s, PageContext.REQUEST_SCOPE);

To use scripting variables, you will also need to define each scripting variable in the TLD file. Every scripting variable must have a <variable> tag with the sub-components shown in Table 14.5.

Table 14.5. TLD Tags for Defining Variables
Tag Description
name-given Defines the name for the scripting variable as a fixed value (cannot be specified if name-from-Attribute is defined)
name-from-attribute Specifies the attribute that is used to define the scripting variable name (cannot be specified if name-given is defined)
variable-class Specifies the class of the scripting variable
declare Specifies if the variable is a new object (defaults to true)
scope Defines the scope of the variable—must be one of NESTED, AT_BEGIN, or AT_END and refer to where in the Web page the variable can be accessed

Using the example String variable would require the following entry in the <tag> section of the TLD:

<variable>
  <name-given>title</name-given>
  <variable-class>java.lang.String</variable-class>
  <declare>true</declare>
  <scope>AT_BEGIN</scope>
</variable>

Variable components must be defined before any attribute components for the tag.

You can use scripting variables in the Job Agency case study pages you developed on Day 13, “JavaServer Pages,” to remove one of the perceived weaknesses of defining beans that require initialization parameters.

As a first step, consider the following code fragment from the advertise.jsp:

<jsp:useBean id="cust" class="web.CustomerBean" scope="request" >
  <jsp:setProperty name="cust" property="login" param="customer"/>
</jsp:useBean>

This fragment creates a JavaBean for the customer information. This code looks clumsy. Ideally, you should be able to create the bean and pass in the initialization properties in a single tag. The JSP idiom of setting the bean properties inside the useBean start and end tags is a contrived solution forced on you by the way beans are used in the JSP. With custom tags, you can provide a much cleaner solution. The following getCust tag creates the required bean and adds it to the page context:

<agency:getCust login='<%=request.getParameter("customer")%>'/>

Looking back at the bean (CustomerBean.java) used to access the customer details, you will see it is a simple adapter for the Advertise Session bean. Its only purpose is to adapt the Session bean interface to the JavaBean semantics required on the Web page. The following code fragment shows the key points of the Bean class:

public CustomerBean () throws NamingException {
    InitialContext ic = new InitialContext();
    advertiseHome = (AdvertiseHome)ic.lookup("java:comp/env/ejb/Advertise");
}
public void setLogin (String login) throws Exception {
    this.login = login;
    advertise = advertiseHome.create(login);
}
public String getName() throws RemoteException    {
    return advertise.getName();
}

  • The bean constructor obtains the home object for the Session bean.

  • The setLogin() method creates the bean as a side effect of setting the login attribute.

  • All the other methods (such as getName()) simply delegate to the appropriate method in the Session bean.

The advertise Session EJB was developed to conform to the JavaBean semantics for accessing and setting attributes. Using a custom tag, you can use the Session EJB directly without the need for a bean wrapper class. Listing 14.7 shows the customer tag that creates the Advertise Session bean for the advertise.jsp page.

Listing 14.7. Full Text of GetCustTag.java
 1: package web;
 2:
 3: import javax.naming.*;
 4: import javax.servlet.jsp.*;
 5: import javax.servlet.jsp.tagext.*;
 6: import agency.*;
 7:
 8: public class GetCustTag extends TagSupport {
 9:     private String login;
10:
11:     public String getLogin() {
12:         return login;
13:     }
14:
15:     public void setLogin(String login) {
16:         this.login = login;
17:     }
18:
19:     public int doStartTag() throws JspException {
20:         try {
21:            InitialContext ic = new InitialContext();
22:            AdvertiseHome advertiseHome = (AdvertiseHome)ic.lookup("java:comp/env/ejb
/Advertise");
23:            Advertise advertise = advertiseHome.create(login);
24:            pageContext.setAttribute("cust", advertise, PageContext.REQUEST_SCOPE);
25:         }
26:         catch (Exception ex) {
27:             throw new JspTagException("CustomerTag: "+ex);
28:         }
29:         return SKIP_BODY;
30:      }
31:
32:     public int doEndTag() {
33:         return EVAL_PAGE;
34:     }
35: }
					

The doStartTag() method finds and creates the Advertise Session EJB using the login name passed as an attribute to the tag. The created bean is added to the page context (line 24) using the name cust, and its scope is set to the current request. After the getCust tag has been defined, the rest of the page can refer to the cust bean and use the jsp:getProperty and jsp:setProperty tags. Because the session bean uses the same properties as the CustomerBean wrapper class, there are no additional changes required to the JSP code.

Listing 14.8 shows the getCust TLD entry with the variable and attribute tags. Note that the login attribute must have an rtexprvalue of true to allow the Web page to pass in the value from the HTTP request parameter.

Listing 14.8. getCust Tag Entry in agency.tld
 1: <tag>
 2:     <name>getCust</name>
 3:     <tag-class>web.GetCustTag</tag-class>
 4:     <body-content>empty</body-content>
 5:     <variable>
 6:       <name-given>cust</name-given>
 7:       <variable-class>agency.Advertise</variable-class>
 8:       <declare>true</declare>
 9:       <scope>AT_BEGIN</scope>
10:     </variable>
11:     <attribute>
12:       <name>login</name>
13:       <required>true</required>
14:       <rtexprvalue>true</rtexprvalue>
15:     </attribute>
16: </tag>
					

To keep the example code simple, it always stores the bean against the name cust. To be more flexible, you could add an additional parameter (such as beanName) to be used to allow the customer bean variable name to specified by the JSP developer. In this case, you would set the TLD <variable> information to use this parameter using the <name-from-attribute>beanName<name-from-attribute/> tag.

You can update the other Web pages in the case study that use the CustomerBean class in a similar manner, and develop a GetJobTag.java to remove the need for the JobBean wrapper around the AdvertiseJob Session bean. The worked case study example includes these updates; later, your task, should you choose to accept it, will be to update the applicant registration pages to use custom tags.

The new versions of the Web pages can be deployed now, or you can wait and learn more about custom tags to address the restrictions of JSP when processing repetitive data.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.138.120.187