Java Reference
In-Depth Information
5.
Change the text of the two JSPs to “Got to DispEmpGrossJSP.” and “Got to
DispEmpTaxAmtJSP.”
6.
Save all the changes to the servlet and JSPs and run the EnterEmpInfo Web page on
the server.
When a new JSP is created, RAD updates the server's directory and the JSP can be accessed immediately. This is
not the case with servlets. When creating a servlet you have to restart the server to get the servlet information into
the directory. Advantage JSP. More about this later.
7.
In the Function list box, select Display and click the submit button.
Verify that “Got to DispEmpInfo JSP” is displayed.
8.
Use the browser pane's “back arrow” to redisplay EnterEmpInfo.
9.
Select the other functions and confirm that the other JSPs are accessed.
You might have noticed a little wait for the JSP text to be displayed. One of the reasons for this delay is that
we are taking the “long way around” to the JSPs by using the redirect command. It is more efficient to have the servlet
get a RequestDispatcher object and use its forward method to send the request and response directly to the JSP.
This eliminates the “round trip” between the client and server.
10.
Display the EmpServlet source code.
Add the following import statement:
11.
import javax.servlet.RequestDispatcher;
Add the following statement to create a class RequestDispatcher variable:
12.
RequestDispatcher dispatcher;
We now need to create a RequestDispatcher object and associate it with the variable dispatcher. Like a
number formatter, a RequestDispatcher object is retrieved, not created. The ServletContext class provides a
getRequestDispatcher method to do this. ServletContext has many useful methods that allow the servlet to
communicate with the servlet container (i.e., the server). In addition, the getRequestDispatcher method allows you
to associate the RequestDispatcher with a particular JSP. Of course, you must first get a ServletContext object
before executing its getRequestDispatcher method. Fortunately, servlets inherit a getServletContext method from the
HTTPServlet superclass.
The syntax to create the RequestDispatcher object is as follows:
dispatcher = getServletContext().getRequestDispatcher("xxx.jsp");
There is quite a bit happening with this statement, so we'll go through the statement step by step.
The getServletContext method is the first part of this statement executed. The getServletContext method, as its
name implies, returns a ServletContext object that is not assigned to a variable. Instead, its getRequestDispatcher
method is invoked and passed the JSP name. The getRequestDispatcher method returns a RequestDispatcher object
and that object is assigned to the variable dispatcher.
In each if statement, replace the redirect statement with a statement that creates a Request
Dispatcher object for each JSP and assigns it to the dispatcher such as the following:
13.
dispatcher =
getServletContext().getRequestDispatcher("DispEmpInfoJSP.jsp");
Search WWH ::




Custom Search