Java Reference
In-Depth Information
1.
In CrtEmpBean's doStartTag method, after the Employee object is assigned to emp but
before EmpBean is defined, add the following statement:
emp.getEmpInfo(pageContext.getRequest().getParameter("empNumTF").toString());
This statement retrieves the employee number entered in the empNumTF field using the request object's
getParameter method. Just as when page context was used to get the Employee bean, a parameter is returned as an
object of type Object . In this case, the Object object is converted to a String using its own toString method (rather
than casting it, as was done earlier). Finally, the employee number is passed to the Employee class's getEmpInfo
method. This method retrieves employee information from the database and populates all the Employee object's
properties. If there is no employee record, the information entered on the page will not be overwritten.
This is not the most efficient way to perform this function. For instance, the Employee object may be written to
twice - once with the form data and then possibly again with the database information. For efficiency, the database
should be checked before the form information is added to the employee object. Then, only if there is no information
for the employee in the database, should the object be populated with the form information. However, this inefficient
solution is simpler to code because only one Java statement is needed.
Notice how easy it was to access the database: one statement. Remember, this was easy because the client-based
application was implemented using the MVC architecture. Following the MVC architecture not only standardizes
application design, it also makes applications more flexible and easier to modify.
Tutorial: Deleting Records Using a Custom Tag
DispEmpInfoJSP needs two new fields to display the gross salary and tax amounts along with all the other employee
information. All the fields will be included on a form with a delete button. When pressed, the delete button will
forward the request to a confirmation JSP (ConfirmEmpDel, see Figure 11-18 ). If the user clicks the confirm button
on the page, the request will be forwarded to a delete employee JSP (EmpDel) where the record will be deleted and a
message displayed confirming the delete (see Figure 11-19 ). A new delete tag (delEmp) will be defined and added to
the EmpDel JSP.
1.
In JavaSource/src/c11, create a new Java class called DeleteEmp with a super class of
TagSupport and no method stubs.
2.
In DeleteEmp, add the following statements to define an Employee class variable.
private Employee emp;
3.
In DeleteEmp, click Source, then Override/Implement Methods. . .
4.
At the Override/Implement Methods window, click on the doStartTag() checkbox to select
it, and then click the OK button.
5.
In the doStartTag method, before the return statement, add the following statement:
emp = (Employee) pageContext.getRequest().getAttribute("EmpBean");
6.
Add the following statement to invoke the Employee delete function and save the file:
emp.doDelete();
 
Search WWH ::




Custom Search