Java Reference
In-Depth Information
</tr>
<tr>
<td>
Username:
</td>
<td>
<input type="text" name="username"><br>
</td>
</tr>
<tr>
<td>
Password:
</td>
<td>
<input type="password" name="password"><br>
</td>
</tr>
<tr>
<td colspan=2> </td>
</tr>
<tr>
<td>
<input type="submit" value="SUBMIT" name="submitButton">
</td>
</tr>
</table>
</form>
</body>
</html>
As you can see from
Listing 12-2
, the
action
method uses the
POST
method to call the LoginServlet.
The two input fields UserName and Password are passed as parameters to the servlet. The
GET
method can work just as well and is, in fact, implemented in the servlet code.
POST
is normally preferred
because it offers more flexibility. The code for the servlet itself is shown in the
next section
.
Creating the Servlet
The login servlet is not much more complex than the simple "Hello World" example of
Listing 12-1
. In
this more practical example, the base class methods that need to be overridden are as follows:
init()
doPost()
doGet()
The code of
Listing 12-3
shows the use of the
init()
method to load the JDBC driver. The
doGet()
and
doPost()
methods are overridden to handle the user request. The
writePage()
method simply exists to separate HTML output from JDBC code.