22-01-2013, 12:19 PM
SERVLET BASICS
1SERVLET BASICS.pdf (Size: 368.71 KB / Downloads: 87)
As discussed in Chapter 1, servlets are programs that run on a Web or application
server and act as a middle layer between a request coming from a Web browser or
other HTTP client and databases or applications on the HTTP server. Their job is to
perform the following tasks, as illustrated in Figure 3–1.
Basic Servlet Structure
Listing 3.1 outlines a basic servlet that handles GET requests. GET requests, for those
unfamiliar with HTTP, are the usual type of browser requests for Web pages. A
browser generates this request when the user enters a URL on the address line, follows
a link from a Web page, or submits an HTML form that either does not specify a METHOD or specifies METHOD="GET". Servlets can also easily handle POST
requests, which are generated when someone submits an HTML form that specifies
METHOD="POST". For details on the use of HTML forms and the distinctions
between GET and POST, see Chapter 19 (Creating and Processing HTML Forms).
A Servlet That
Generates Plain Text
Listing 3.2 shows a simple servlet that outputs plain text, with the output shown in
Figure 3–2. Before we move on, it is worth spending some time reviewing the process
of installing, compiling, and running this simple servlet. See Chapter 2 (Server
Setup and Configuration) for a much more detailed description of the process.
First, be sure that you’ve already verified the basics:
• That your server is set up properly as described in Section 2.3
(Configure the Server).
• That your development CLASSPATH refers to the necessary three
entries (the servlet JAR file, your top-level development directory,
and “.”) as described in Section 2.7 (Set Up Your Development
Environment).
• That all of the test cases of Section 2.8 (Test Your Setup) execute
successfully.
Second, type “javac HelloWorld.java” or tell your development environment
to compile the servlet (e.g., by clicking Build in your IDE or selecting Compile
from the emacs JDE menu). This step will compile your servlet to create
HelloWorld.class.
Third, move HelloWorld.class to the directory that your server uses to store servlets
that are in the default Web application. The exact location varies from server to
server, but is typically of the form install_dir/.../WEB-INF/classes (see Section 2.10 for
details). For Tomcat you use install_dir/webapps/ROOT/WEB-INF/classes, for JRun
you use install_dir/servers/default/default-ear/default-war/WEB-INF/classes, and for
Resin you use install_dir/doc/WEB-INF/classes. Alternatively, you can use one of the
techniques of Section 2.9 (Establish a Simplified Deployment Method) to automatically
place the class files in the appropriate location.
A Servlet That Generates HTML
Most servlets generate HTML, not plain text as in the previous example. To generate
HTML, you add three steps to the process just shown:
1. Tell the browser that you’re sending it HTML.
2. Modify the println statements to build a legal Web page.
3. Check your HTML with a formal syntax validator.
You accomplish the first step by setting the HTTP Content-Type response
header to text/html. In general, headers are set by the setHeader method of
HttpServletResponse, but setting the content type is such a common task that
there is also a special setContentType method just for this purpose. The way to
designate HTML is with a type of text/html, so the code would look like this:
response.setContentType("text/html");
Although HTML is the most common kind of document that servlets create, it is
not unusual for servlets to create other document types. For example, it is quite common
to use servlets to generate Excel spreadsheets (content type application/
vnd.ms-excel—see Section 7.3), JPEG images (content type image/jpeg—see
Section 7.5), and XML documents (content type text/xml). Also, you rarely use
servlets to generate HTML pages that have relatively fixed formats (i.e., whose layout
changes little for each request); JSP is usually more convenient in such a case.
JSP is discussed in Part II of this book (starting in Chapter 10).
Don’t be concerned if you are not yet familiar with HTTP response headers; they
are discussed in Chapter 7. However, you should note now that you need to set
response headers before actually returning any of the content with the PrintWriter.
That’s because an HTTP response consists of the status line, one or more headers, a
blank line, and the actual document, in that order. The headers can appear in any
order, and servlets buffer the headers and send them all at once, so it is legal to set the
status code (part of the first line returned) even after setting headers. But servlets do
not necessarily buffer the document itself, since users might want to see partial results
for long pages. Servlet engines are permitted to partially buffer the output, but the
size of the buffer is left unspecified. You can use the getBufferSize method of
HttpServletResponse to determine the size, or you can use setBufferSize to
specify it. You can set headers until the buffer fills up and is actually sent to the client.
If you aren’t sure whether the buffer has been sent, you can use the isCommitted
method to check. Even so, the best approach is to simply put the setContentType
line before any of the lines that use the PrintWriter.