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 - The code for the EmailServlet class (Page 2 of 4 )
In chapter 1, you learned that a servlet is just a Java class that runs on a server. You also learned that a servlet for a web application extends the HttpServlet class. In figure 5-2, then, you can see that the EmailServlet class extends this class.
The first six statements for this servlet create the package for the servlet and import all the other classes that the servlet will need. First, the package statement puts the servlet in a package named email5. Then, the next three statements import some packages from the servlet API and from the core Java API. These packages are needed by all servlets. Finally, the last two statements import the User and UserIO classes from the business and data packages.
After the declaration for the class, the doGet method provides the code that’s executed when a browser uses the Get method to request a servlet. This doGet method accepts two arguments that are passed to it from the web server: an HttpServletRequest object and an HttpServletResponse object. These objects are commonly referred to as the request object and the response object. In fact, the implicit request object that you learned about in the last chapter is actually an object of the HttpServletRequest class.
Within the doGet method, the first two statements perform tasks that are common to most servlets that return an HTML document. Here, the first statement sets the content type that will be returned to the browser. In this case, the content type is set so that the servlet returns an HTML document, but it’s possible to return other content types. Then, the second statement returns a PrintWriter object that’s used to return data to the browser later on.
After the first two statements of the doGet method, the shaded statements perform the same processing that was presented in the last chapter. First, the getParameter method of the request object returns the three parameters entered by the user. Then, a User object is created from these three parameters, and the UserIO class adds the User object to the specified file.
The last statement in the doGet method uses the println method of the PrintWriter object to return HTML to the browser. This is the same HTML that was used in the JSP in the last chapter. However, this HTML is more difficult to code and read now that it’s coded as a string argument of the println method. That’s why the next chapter will show how to remove this type of tedious coding from your servlets by combining the use of servlets with JSPs.
Within the println string, only the firstName, lastName, and emailAddress variables are displayed outside of quotation marks. Also, the quotation marks within the string, like the quotation marks around HTML attributes, are preceded by a backslash (\). Otherwise, the Java compiler would interpret those quotation marks as the end of the string. Since the quotation marks within the HTML statements aren’t actually required, you can remove them to simplify the code, but the code is still clumsy.