Java Reference
In-Depth Information
Because the init() method is called when the servlet is loaded, and the
destroy() method is called when the servlet is removed from service, these
methods are called only once, regardless of the number of threads created
for the servlet.
Processing HTTP Requests in a Servlet
At the heart of servlet processing is receiving a request and sending a
response. As discussed above, the HttpServlet class provides the methods
doGet() and doPost() that are called to process either a Get or a Post, depending
on the method used to send an HTTP request. By overriding either or both of
the doGet() and doPost() methods, a servlet can process Get requests, Post
requests, or both. If both Get and Post requests are to be treated alike, one of
the methods can call the other, rather than duplicating code.
Both the doGet() and doPost() methods have an HttpServletRequest object
and an HttpServletResponse object as parameters. The HttpServletRequest
object encapsulates the request passed from the browser through the Web server
to the servlet. The request object itself is created by the servlet container. The
HttpServletResponse object , also created by the servlet container, provides
HTTP-specific functionality for sending a response.
In sending a response, a servlet can create a Web page dynamically by
writing HTML code as output that the server returns to the browser on the
client machine. As a preliminary step, the setContentType() method must be
called to set the character encoding, or MIME type, of the content being
returned by the server to the browser. MIME ( Multipurpose Internet Mail
Extensions ) is a specification for formatting messages, particularly those using
character sets other than ASCII, so that they can be sent over the Web. Many
e-mail client programs support MIME, enabling them to send and receive
graphics, audio, and video files. Web servers insert a MIME header at the begin-
ning of a Web transmission, allowing receiving clients to select an appropriate
application to process the type of data being sent. Browsers have a built-in ability
to process certain data types — such as HTML, JPG, and GIF files — while other
types may require additional helper applications, such as Adobe Acrobat Reader
or RealPlayer.
Figure 12-19 on the next page, displays lines 70 through 88 of the WebStocks
servlet code. Line 84 sets the setContentType() method to set the MIME type to
text/html, indicating that a text- or html-encoded document will be written.
Line 85 uses the HttpServletResponse response object method, called the
getWriter() method , to return a PrintWriter object that the servlet can use to
send character text to the Web browser on the client machine. Using the
getWriter() method is similar to a console application using the PrintStream
object, out, of the System class, as in System.out.println(); however, the output is
not directed to the console, but to the requesting client through the Web server.
Search WWH ::




Custom Search