Java Reference
In-Depth Information
6.
Change the Action property to the servlet EmpServlet.
7.
Run EnterEmpInfoForm on the server, select Gross, and click the Submit button.
A blank page will be displayed in the browser pane but the function and number of exemptions will be displayed
in the console as follows:
[3/9/12 12:40:42:994 EDT] 0000002c SystemOut O Gross
[3/9/12 12:40:42:994 EDT] 0000002c SystemOut O 2
(Of course, the date/time information displayed at the beginning of each line will be different for you.) This
proves that the Web page is tied to the servlet, that the doPost is invoked, and that the information entered in the
new page components can be accessed. Notice that even though the exemption value names (one, two, three, etc.)
are displayed on the Web page, the values (1, 2, 3, etc.) are returned in the request. Please remember that the
values (1, 2, 3, etc.) are still strings and will have to be parsed into primitives before being used in calculations.
We will now change doPost to invoke the correct JSP based on the selected function.
8.
Replace the last three statements in doPost with the following:
if (function.equals("Display")) {
resp.sendRedirect("DispEmpInfoJSP.jsp");
} else {
if (function.equals("Gross")) {
resp.sendRedirect("DispEmpGrossJSP.jsp");
} else {
if (function.equals("TaxAmt")) {
resp.sendRedirect("DispEmpTaxAmtJSP.jsp");
}
}
}
The response's sendRedirect method tells the browser to submit a new request, in this case, for a particular JSP
based on the function selected by the user. In other words, the name of the JSP file is inserted into the response and
the server sends the response back to the browser. The browser then creates another request (for the JSP) and sends
the new request to the server.
This is not an efficient way to access the JSPs but it is syntactically simple. In the next section we will use a more
“system efficient” technique that is, unfortunately, more complicated for the programmer.
In addition, we have some work to do because these JSPs don't exist.
Tutorial: Creating JSPs
At first, we will code the three JSPs to print static text. This will allow us to test/prove that the browser is requesting
the JSPs (i.e., this will prove that the servlet's redirects work). We will then use a RequestDispatcher object to
forward the request and response directly to the JSPs rather than have the browser make a new request for the JSPs.
Click on the TutorialsWeb project to select it, and then click File , New , and Web Page .
1.
2.
At the New Web Page frame, specify DispEmpInfoJSP as the file name, JSP as the Template,
and click the Finish button.
3.
In the Design view, specify “Got to DispEmpInfoJSP.” as the text to be displayed.
4.
Create two more JSPs called DispEmpGrossJSP and DispEmpTaxAmtJSP.
 
Search WWH ::




Custom Search