The request and response objects belong to classes in the javax.servlet.http package. A servlet receives information about how it was run by calling methods of the HttpServletRequest class. For example, when a web form is submitted to a servlet, each field on the form is stored as a string by the HttpServletRequest class. You can retrieve these fields in a servlet by calling the getParameter(String) method with the name of the field as an argument. This method returns null if no field of that name exists. A servlet communicates with the user by sending back an HTML document, a graphics file, or another type of information supported by a web browser. It sends this information by calling the methods of the HttpServletResponse class. The first thing you must do when preparing a response is to define the kind of content the servlet is sending to a browser. Call the setContentType(String) method with the content type as an argument.
The most common form for a response is HTML, which is set by calling setContentType(“text/html”). You also can send a response as text (“text/plain”),
graphics files (“image/gif”, “image/jpeg”), and application-specific formats such as “application/msword”.
To send data to a browser, you create a servlet output stream associated with the browser and then call the println(String) method on that stream. Servlet output streams are represented by the ServletOutputStream class, which is part of the javax.servlet package. You can get one of these streams by calling the response object’s getOutputStream() method. The following example creates a servlet output stream from an HttpServletResponse object called response and then sends a short web page to that stream:
Listing 21.1 contains a Java servlet that receives data from the form displayed in Figure 21.1.After saving the servlet, compile it with the Java compiler. The Rot13 servlet receives text from a web form, translates it using ROT-13, and then displays the result in a new web form. ROT-13 is a trivial method of encrypting text through letter substitution. Each letter of the alphabet is replaced with the letter that’s 13 places away: A becomes N, N becomes A, B becomes O, O becomes B, C becomes P, P becomes C, and so on. Because the ROT-13 encryption scheme is easy to decode, it isn’t used when information must remain secret. Instead, it’s used casually on Internet discussion forums such as Usenet newsgroups. For example, if someone on a movie newsgroup wants to share a spoiler that reveals a plot detail about an upcoming movie, she can encode it in ROT-13 to prevent people from reading it accidentally.
To make the ROT-13 servlet available, you must publish its class files in a folder on your web server that has been designated for Java servlets. Tomcat is organized so that servlets, other classes, and JSP pages are placed in subfolders of the software’s webapps folder. One way to deploy a servlet’s class file is to store it in a WEB-INF\classes subfolder somewhere in the webapps hierarchy of folders. If you chose to install them during installation, Tomcat 5.5 includes several sample servlets in the servlets-examples and jsp-examples folders inside webapps. You can deploy the ROT-13 servlet in the servlet-examples folder by storing Rot13.class in webapps\servlet-examples\WEB-INF\classes (Windows) or webapps/servletexamples/ WEB-INF/classes (Linux). If you place the Rot13.class file in this folder, edit the web.xml file in its parent folder and add the following lines:
<servlet>
<servlet-name>Rot13</servlet-name>
<servlet-class>Rot13</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Rot13</servlet-name>
<url-pattern>/servlet/Rot13</url-pattern>
</servlet-mapping>
The web.xml file configures a web application such as the group of servlet examples stored in wepapps\servlet-examples. These lines must be placed somewhere after the opening <web-app> tag and before the closing </web-app> tag. After adding the class file and editing web.xml, restart Tomcat and run the servlet by loading its address with a web browser. The address of the servlet depends on where it was stored in the webapps folder. If you used the preceding configuration, it’s in /servlets-examples/servlet/Rot13, as in http://localhost:8080 /servlets-examples/servlet/Rot13.