Java Reference
In-Depth Information
8.4
The Servlet URL and the Invoking Web Page
Before we consider the structure of a servlet, recall that a servlet will be executed
on a Web server only in response to a request from a user's browser. Though the
servlet may be invoked directly by entering its URL into the browser (an example
of which is shown at the end of the previous chapter), it is much more common for
a servlet to be called from a preceding HTML page. This is usually achieved by the
use of an HTML form, with the form's METHOD attribute specifying either 'GET'
or 'POST' and its ACTION attribute specifying the address of the servlet. As noted
in the previous section, each servlet must be held in folder <CATALINA_HOME>
\webapps\<WebAppName>\WEB-INF\classes . The URL for such a servlet has the
following format:
http://localhost:8080/<WebAppName>/<ServletName>
For example:
http://localhost:8080/MyWebApp/FirstServlet
Note the use of localhost above to refer to the current machine and 8080 to
indicate that Tomcat uses port 8080. Usually, of course, client and server programs
will be on separate machines, but this gives us a convenient test bed for our programs.
The servlet above may then be invoked via the ACTION attribute of a FORM tag in
a preceding HTML page as follows:
<FORM METHOD=GET ACTION="FirstServlet">
Note that the URL for the servlet is relative to the Web application that contains
both the servlet and the HTML page.
To keep things as simple as possible for the time being, we shall start off with a
Web page that calls up a servlet without actually sending it any data. The code for
this simple Web page is shown below.
Example
<HTML>
<HEAD>
<TITLE>A First Servlet</TITLE>
<STYLE>
body{text-align:center;}
</STYLE>
</HEAD>
<BODY>
<BR><BR><BR>
<FORM METHOD=GET ACTION="FirstServlet">
<INPUT TYPE="Submit" VALUE = "Click me!">
</FORM>
</BODY>
</HTML>
Search WWH ::




Custom Search