Databases Reference
In-Depth Information
The only part that remains is somehow to tie the browser and the
server together. We do this with the FortuneClientServlet . This component
will be invoked by the HTML form and will connect to the server on the
client's behalf. Once this is done, all of the actions that were described
above take place. Let's turn our attention to this project's centerpiece,
the FortuneClientServlet code (see Exhibit 34-10), as without it we would be
unable to make any of this happen.
Creating the Client Servlet
The FortuneClientServlet is a subclass of HttpServlet . It contains one and
only one method - doGet() — that redefines the behavior the superclass pro-
vided. When we click the button “I'm daring enough to push this button,” on
the HTML form, a program called servletrunner (part of the servlet SDK) is
executing on the target machine, takes the form request and any informa-
tion the form contains, and acts as a proxy by directing it to the appropriate
servlet. Our FortuneClientServlet gets called, and code execution begins in
the method doGet() doPost() if this were a POST action. Notice that the
FortuneClientServlet attaches to the port the server is listening to; the server
delegates the task of getting a fortune to the RequestProcessor , and this last
component returns the fortune to the servlet. The servlet has initiated a
chain of events that effectively limits its participation in this system to
receiving a fortune, then forwarding it to the client that requested it. The
culmination of this part of the project is shown in Exhibit 34-11.
Searching the Database
Surely one of the more popular tasks today is being able to perform
searches against databases. For that reason, we've developed a QuoteSearch
servlet. The client can enter a keyword, then exhaustively search all
700 fortunes/quotations, and, if the keyword is found, the entry is
returned. This servlet is no more difficult to develop than the former; how-
ever, it does illustrate some things we haven't talked about, e.g., how do we
capture form input from a servlet and how do we use the init() method to
our benefit? Before we continue, take some time to examine the code in
Exhibit 34-12.
Much of the code we see here should look familiar — the process of con-
necting to the database and working with SQL statements remains the
same. We perform the initial resource-intensive operation of connecting to
the database only once — in the init() method. The servletrunner proxy,
which listens for servlet requests, ensures that each servlet's init() is exe-
cuted just once.
After the client enters the keyword and clicks the Submit button, a POST
operation is performed. For this reason, we override the doPost() method
and tailor our response to the client's action with any code we place in
Search WWH ::




Custom Search