Java Reference
In-Depth Information
Tutorial: Adding Function to the Server-Based Application
We will expand the server-side application to provide the same functions as the client-based application. As a matter
of fact, we will reuse the client code that performs these functions. In addition, we will define a new property for
employee number in the Employee class.
1.
Create a new folder in TutorialsWeb/Java Resources/src called c9java.
Because Employee class contains most of the application's functions, wouldn't it be a lot easier to simply include
Employee in the server-based application rather than code a servlet or JSP to perform the same function? I hope you
said, “Yes, it would,” because that's what we're going to do.
2.
Copy Tutorials/c7/Employee.java into c9java.
In c9java/Employee.java, add a new class level private String variable called empNum.
3.
4.
Have RAD generate a getter and setter for empNum and save the source code.
This is all that was needed to add a new property to Employee. If this were a client-based application, making this
change to the application would require that the new Employee class be installed on many PCs. With a server-based
application, only the one class file on the server needs to be reinstalled. This is one of the advantages of a server-based
application.
However, just copying the class into the project does not include Employee in the server-based application.
The application needs to do two things:
A.
Get the data from EnterEmpInfoForm into an Employee object
B.
Have the JSPs access the Employee object
We will solve the first part with a new class called EmpExtractor. EmpExtractor will retrieve the employee data
from the request and create an Employee object whose properties hold the request data.
5.
Click on c9java to select it.
Click File , New , and then Class .
6.
7.
At the New Java Class window, specify EmpExtractor as the class name, then click the
Finish button.
An edit session will be started.
We want EmpExtractor to create an Employee object, retrieve the form information from the request, and set the
Employee object's properties. The Employee object will then be passed to the JSPs. Each JSP will use the appropriate
Employee object's method(s) to perform the requested function (Gross, TaxAmt, or Display) and display the results.
Click Source , Generate Constructors from Superclass.... , and then the OK button.
8.
This will generate a null constructor.
9.
In EmpExtractor, add the following import statements:
import javax.servlet.http.HttpServletRequest;
import org.omg.CORBA.DynAnyPackage.InvalidValue;
The HttpServletRequest class has to be imported because the class is going to receive a request object (from the JSPs)
and create a method variable to reference the request. The InvalidValue class needs to be imported because the
setPayRate method in the Employee class can throw an InvalidValue exception.
 
Search WWH ::




Custom Search