Java Reference
In-Depth Information
This servlet should now be compiled in the same way as any other Java program,
either by using a development environment or by opening a command window and
executing the Java compiler as follows:
javac FirstServlet.java
8.6
Testing a Servlet
In order to test our Web page and associated servlet, we fi rst need to set Tomcat
running. This may be done either by double-clicking on fi le
startup.bat
(in
<CATALINA_HOME>\bin
) or by entering the following command into an MS
DOS command window (assuming that
startup.bat
is on the
PAT H
):
startup
Four lines of output should appear in the current command window and a second
command window will begin to fi ll up with output. When the line commencing
INFO: Server startup
appears in the second window, Tomcat is running.
Assuming that our initial Web page has been given the name
FirstServlet.html
,
we can now open up our browser and enter the following address:
Figure
8.1
shows what this initial Web page looks like under the Firefox browser.
Upon clicking on the page's button, the servlet is executed and the output shown in
Fig.
8.2
is produced.
8.7
Passing Data
The previous example was very artifi cial, since no data was passed by the initial
form and so there was no unpredictability about the contents of the page generated
by the servlet. We might just as well have had two static Web pages, with a hyper-
link connecting one to the other. Let's modify the initial form a little now, in order
to make the example rather more realistic…
<FORM METHOD=GET ACTION="PersonalServlet">
Enter your fi rst name:
<INPUT TYPE="Text" NAME="FirstName" VALUE="">
<BR><BR>
<INPUT TYPE="Submit" VALUE="Submit">
</FORM>
Now our form will accept the user's fi rst name and, once the 'Submit' button is
clicked, will pass the value entered to the servlet. The servlet may then make use of
this value when constructing the page to be returned by the Web server.
It is now appropriate to consider the methods of
HttpServletRequest
that are
responsible for handling values/parameters received by servlets. There are three
Search WWH ::
Custom Search