Servlets are the opposite of Java applets. Java applets run from a Web browser on the client side. To write Java programs, you define classes. To write a Java applet, you define a class that extends the Applet class. The Web browser runs and controls the execution of the applet through the methods defined in the Applet class. Similarly, to write a Java servlet, you define a class that extends the HttpServlet class. The servlet container runs and controls the execution of the servlet through the methods defined in the HttpServlet class. Like a Java applet, a servlet does not have a main method. A servlet depends on the servlet server to call the methods. Every servlet has a structure like the one shown below:
import javax.servlet.*; import javax.servlet.http.*; import java.io.*; public class MyServlet extends HttpServlet { /** Called by the servlet engine to initialize servlet */ public void init() throws ServletException { ... } /** Process the HTTP Get request */ public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { ... } /** Process the HTTP Post request */ public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { ... }
/** Called by the servlet engine to release resource */ public void destroy() { ... } // Other methods if necessary }
The servlet engine controls the servlets using init , doGet , doPost , destroy , and other methods. By default, the doGet and doPost methods do nothing. To handle a GET request, you need to override the doGet method; to handle a POST request, you need to override the doPost method.
Listing 34.2 gives a simple Java servlet that generates a dynamic Web page for displaying the current time, as shown in Figure 34.11. The servlet is named CurrentTime . Compile it into c:\jakarta-tomcat-5.5.9\webapps\liangweb\WEB-INF\classes . Run the servlet using the URL
http://localhost:8080/liangweb/CurrentTime
Tip
The destination directory for CurrentTime.class is c:\jakarta-tomcat-5.5.9\webapps\liangweb\WEB-INF\classes . You can compile CurrentTime.java to generate CurrentTime.class and then move it into the destination directory. Or you can use the following command to compile and save the .class directly into the destination directory: javac “d <destination directory> CurrentTime.java |
Note
For every servlet example in this chapter, you have to insert a serlvet entry and a mapping entry into the web.xml file. For simplicity, this book uses /ServletClassName as the URL for the servlet. |
1 import javax.servlet.*; 2 import javax.servlet.http.*; 3 import java.io.*; 4 5 public class CurrentTime extends HttpServlet { 6 /** Process the HTTP Get request */ 7 public void doGet(HttpServletRequest request, HttpServletResponse 8 response) throws ServletException, IOException { 9 response.setContentType( "text/html" ); 10 PrintWriter out = response.getWriter(); 11 out.println( "<p>The current time is " + new java.util.Date()); 12 out.close(); // Close stream 13 } 14 } |
The HttpServlet class has a doGet method. The doGet method is invoked when the browser issues a request to the servlet using the GET method. Your servlet class should override the doGet method to respond to the GET request. In this case, you write the code to display the current time.
Servlets return responses to the browser through an HttpServletResponse object. Since the setContentType("text/html") method sets the MIME type to "text/html," the browser will display the response in HTML. The getWriter method returns a PrintWriter stream ( out ) for sending HTML back to the client.
Note
The URL query string uses the GET method to issue a request to the servlet. The current time may not be current if the Web page for displaying the current time is cached. To ensure that a new current time is displayed, refresh the page in the browser. In the next example, you will write a new servlet that uses the POST method to obtain the current time. |
Note
If you experience problems after Tomcat is successfully started, you may have to shut down and restart Tomcat after new servlet class files are added to the c:\jakarta-tomcat-5.5.9\webapps\liangweb\WEB-INF\classes directory. |