img
The Ser vletInputStream Class
The ServletInputStream class extends InputStream. It is implemented by the servlet container
and provides an input stream that a servlet developer can use to read the data from a client
request. It defines the default constructor. In addition, a method is provided to read bytes
from the stream. It is shown here:
int readLine(byte[ ] buffer, int offset, int size) throws IOException
Here, buffer is the array into which size bytes are placed starting at offset. The method returns
the actual number of bytes read or ­1 if an end-of-stream condition is encountered.
The Ser vletOutputStream Class
The ServletOutputStream class extends OutputStream. It is implemented by the servlet
container and provides an output stream that a servlet developer can use to write data to a
client response. A default constructor is defined. It also defines the print( ) and println( )
methods, which output data to the stream.
The Ser vlet Exception Classes
javax.servlet defines two exceptions. The first is ServletException, which indicates that a servlet
problem has occurred. The second is UnavailableException, which extends ServletException.
It indicates that a servlet is unavailable.
Reading Ser vlet Parameters
The ServletRequest interface includes methods that allow you to read the names and values
of parameters that are included in a client request. We will develop a servlet that illustrates
their use. The example contains two files. A web page is defined in PostParameters.htm, and
a servlet is defined in PostParametersServlet.java.
The HTML source code for PostParameters.htm is shown in the following listing. It defines
a table that contains two labels and two text fields. One of the labels is Employee and the
other is Phone. There is also a submit button. Notice that the action parameter of the form
tag specifies a URL. The URL identifies the servlet to process the HTTP POST request.
<html>
<body>
<center>
<form name="Form1"
method="post"
action="http://localhost:8080/servlets-examples/
servlet/PostParametersServlet">
<table>
<tr>
<td><B>Employee</td>
<td><input type=textbox name="e" size="25" value=""></td>
</tr>
<tr>
<td><B>Phone</td>
<td><input type=textbox name="p" size="25" value=""></td>
</tr>
</table>
Search WWH :
Custom Search
Previous Page
Java SE 6 Topic Index
Next Page
Java SE 6 Bookmarks
Home