Servlets are Java classes that run on servers. In this article, the first of three parts, you will learn how to create, save and compile a servlet. This article is excerpted from chapter five of the book Murach's Java Servlets and JSP, written by Andrea Steelman and Joel Murach (Murach; ISBN: 1890774189).
How to Develop Servlets - How to create a servlet (Page 3 of 4 )
Now that you have a general idea of how servlets are coded, you’re ready to learn some specific skills for creating a servlet. To start, you need to know how to begin coding the class for a servlet.
How to code a servlet
Figure 5-3 shows the basic structure for a typical servlet that performs some processing and returns an HTML document to the browser. You can use this basic structure for all the servlets you write. For now, that’s all you need to get started, but you’ll learn another way to structure servlets in the next chapter.
Since most servlets are stored in a package, the first statement in this servlet specifies the package for the servlet. This package must correspond to the directory that the servlet is saved in. In the next figure, you’ll learn more about how this works.
The next three statements are the import statements that are required by all servlets. The javax.servlet.http package is required because it contains the HttpServletRequest and HttpServletResponse classes. The javax.servlet class is required because it contains the ServletException class. And the java.io class is required because it contains the IOException class.
After the first four statements, the class declaration provides the name for the servlet and indicates that it extends the HttpServlet class. Although in theory a servlet can extend the GenericServlet class, all servlets for web applications extend the HttpServlet class.
The doGet and doPost methods in this figure accept the same arguments and throw the same exceptions. Within these methods, you can use the methods of the request object to get incoming data, and you can use the methods of the response object to set outgoing data. In this structure, since the doPost method calls the doGet method, an HTTP request that uses the Post method will actually execute the doGet method of the servlet. This is a common programming practice that allows a servlet to use the same code to handle both the Get and Post methods of an HTTP request.
In the doGet method, the first statement calls the setContentType method of the response object. This sets the content type for the HTTP response that’s returned to the browser to “text/html,” which specifies that the servlet returns text or HTML. In chapter 15, you’ll learn how to return other types of data.
The second statement in the doGet method obtains a PrintWriter object named out from the response object. This object can be used to return character data to the client. Once you have this object, you can use one println statement or a series of print and println statements to return HTML or other text to the browser. However, as you learned in the last figure, you must code a backslash before any quotation marks that don’t start or end a string. Otherwise, the servlet won’t compile properly.
All web-based servlets extend the HttpServlet class. To extend this class, the servlet must import the javax.servlet, javax.servlet.http, and java.io packages.
The doGet method overrides the doGet method of the HttpServlet class and processes all HTTP requests that use the Get method, and the doPost method overrides the doPost method of the HttpServlet class and processes all HTTP requests that use the Post method.
The doGet and doPost methods use two objects that are passed to it by the web server: (1) the HttpServletRequest object, or the request object, and (2) the HttpServletResponse object, or the response object.
The setContentType method of the response object sets the content type of the response that’s returned to the browser. Then, the getWriter method of the response object returns a PrintWriter object that can be used to send HTML to the browser.
Before you can create a PrintWriter object, you must set the content type. This allows the getWriter method to return a PrintWriter object that uses the proper content type.