Java Reference
In-Depth Information
Tutorial: Defining the Tag Handler Class
Time to define the tag handler class:
1.
In CrtEmpBean, add the following import statement after the existing import statement:
import javax.servlet.http.HttpServletRequest;
2.
In CrtEmpBean, add the following statements to create a class Employee variable called
emp and both an EmpExtractor object and EmpExtractor class variable called ee.
private Employee emp;
private EmpExtractor ee = new EmpExtractor();
3.
In CrtEmpBean, click Source, then Override/Implement Methods. . .
4.
At the Override/Implement Methods window, click on the doStartTag() checkbox to select it.
5.
Make sure the insertion point option is After 'ee” and click the OK button.
RAD creates a doStartTag method. Notice the return statement. Even though we are overriding the superclass'
doStartTag method (to create and populate the Employee bean), the superclass' doStartTag method is still executed.
This is because the superclass' doStartTag method performs many functions we still want to occur. For instance, the
doStartTag method returns a value indicating success or failure of the method. This still must occur for the tag to work.
Therefore, the superclass' doStartTag method is executed and its return value is returned as our doStartTag method's
return value.
6.
Before the return statement, add the following two statements to create the Employee
object, populate its properties from the data in the request, and define the Employee
object as a bean called EmpBean:
emp = ee.getEmployeeInstance((HttpServletRequest)pageContext.getRequest());
pageContext.getRequest().setAttribute("EmpBean", emp);
The code should look like the following:
package c11;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;
import javax.servlet.http.HttpServletRequest;
public class CrtEmpBean extends TagSupport {
private Employee emp;
private EmpExtractor ee = new EmpExtractor();
public int doStartTag() throws JspException {
emp = ee.getEmployeeInstance((HttpServletRequest)
pageContext.getRequest());
pageContext.getRequest().setAttribute("EmpBean", emp);
return super .doStartTag();
}
}
7.
Save CrtEmpBean.
 
Search WWH ::




Custom Search