Java Reference
In-Depth Information
3.
Change the second statement so that a new out variable is not created. The getStarted
source should look like the following:
public void getStarted(HttpServletResponse resp) throws
IOException {
resp.setContentType("text/html");
out = new PrintWriter(resp.getOutputStream());
out.println("<HTML>");
out.println("<HEAD>");
}
Errors will be displayed saying that out cannot be resolved. We need to create out as a class variable.
4.
At the beginning of the class, add the following statement to create out as a class variable:
PrintWriter out;
5.
In doPost and doGet, replace the first four statements with the following statement:
getStarted(resp);
That's enough coding, let's test a little.
6.
Close the any browser windows that are open and run MyServlet on the server.
The text showing that the doGet was executed will be displayed.
7.
In the browser address field, specify EnterEmpInfoForm.html and press the Enter key.
8.
On the EnterEmpInfoForm Web page, click the submit button.
In the console, the text indicating that the doPost was executed will be displayed. This means that you
successfully tidied up the class. Now, we'll add code to retrieve and display the form data. The request (passed to the
servlet) contains the form's text field values as parameters. The values can be retrieved (as strings) using the request's
getParameter method. The getParameter method simply requires that you specify which component's text to retrieve.
9.
In doPost, replace the statement that displays the static text with the following:
out.println("Employee Number " + req.getParameter("empNumTF"));
10.
Save the source code and run the EnterEmpInfoForm page on the server.
11.
On EnterEmpInfoForm, enter 123 as the employee number and click the submit button.
The text “Employee Number 123” will be displayed in the browser.
Think of the implications to our application! If the servlet created an employee object and the Web page offered
options to calculate gross salary, tax amounts, and so on, then:
A.
All of the employee functions could be available from any computer that had access to the
Web.
B.
We would not have to install the application on multiple (thousands? millions?) of PCs,
thereby saving a good deal of time, effort, and confusion.
C.
Modifications would be made to a single set of programs on the server, not to multiple
copies of the programs on each PC.
Search WWH ::




Custom Search