Java Reference
In-Depth Information
10.
Add the following to define a class variable and Employee object:
Employee emp = new Employee();
11.
After the null constructor, add the following to define a new method called
getEmployeeInstance:
public Employee getEmployeeInstance(HttpServletRequest request){
return emp;
}
Notice that the method expects a request object. We will now add the code to retrieve the information from the
request object and set emp's properties.
12.
Before the return statement in getEmployeeInstance, add the following statements:
emp.setEmpName(request.getParameter("empNameTF"));
emp.setEmpStreet(request.getParameter("streetAddrTF"));
emp.setEmpCity(request.getParameter("cityTF"));
emp.setEmpState(request.getParameter("stateTF"));
emp.setEmpZip(request.getParameter("zipTF"));
try {
emp.setPayRate(Double.parseDouble(
request.getParameter("hPRTF")));
} catch (NumberFormatException Error) {
System.out.println("Pay rate must be a numeric" +
"value: 8 or 7.25");
} catch (InvalidValue Error) {
System.out.println(Error.getMessage());
}
emp.setExemptions(Integer.parseInt(
request.getParameter("exmpDDM")));
Does this source code seem familiar? This is the same logic that's in EnterEmpInfo's setEmployeeProperties
method. The difference is that setEmployeeProperties retrieved the information from a frame's text fields, whereas
getEmployeeInstance is retrieving the information from a request object. Even though EmpExtractor and EnterEmpInfo
are very similar, we had to create them because the client and server-side applications use two different interfaces to
gather the Employee information: AWT (for the client-side application) versus a Web page (for the server-side application).
Notice that regardless of the interface, however, the core logic of the two applications (i.e., the Employee class)
is the same. Separating the client-based application's core logic (creating the Employee class) from the interface
(i.e., the frame classes) was a very wise decision. Because of this, we are able to reuse the Employee class with a
different interface (e.g., the Web page in our server-based application). As a matter of fact, if we wanted to provide
alternative interfaces for PDAs, cell phones, or holograms (just kidding), Employee could still be used. We will talk
more about this “application architecture” later in the chapter.
Tutorial: Enhancing the JSPs
The JSPs must be modified to call EmpExtractor and then use the returned Employee object to perform the requested
functions. We will modify DispEmpInfoJSP first.
 
Search WWH ::




Custom Search