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.
The code for the EmailServlet class package email5;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import business.*;
import data.*;
public class EmailServlet extends HttpServlet{
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException{
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String firstName = request.getParameter("firstName");
String lastName = request.getParameter("lastName");
String emailAddress = request.getParameter("emailAddress");
User user = new User(firstName, lastName, emailAddress);
UserIO.addRecord(user, "../webapps/murach/WEB-INF/etc/UserEmail.txt");
out.println(
"<!doctype html public \"-//W3C//DTD HTML 4.0 Transitional//EN\">\n"
+ "<html>\n"
+ "<head>\n"
+ " <title>Chapter 5 - Email List application</title>\n"
+ "</head>\n"
+ "<body>\n"
+ "<h1>Thanks for joining our email list</h1>\n"
+ "<p>Here is the information that you entered:</p>\n"
+ " <table cellspacing=\"5\" cellpadding=\"5\" border=\"1\">\n"
+ " <tr><td align=\"right\">First name:</td>\n"
+ " <td>" + firstName + "</td>\n"
+ " </tr>\n"
+ " <tr><td align=\"right\">Last name:</td>\n"
+ " <td>" + lastName + "</td>\n"
+ " </tr>\n"
+ " <tr><td align=\"right\">Email address: </td>\n"
+ " <td>" + emailAddress + "</td>\n"
+ " </tr>\n"
+ " </table>\n"
+ "<p>To enter another email address, click on the Back <br>\n"
+ "button in your browser or the Return button shown <br>\n"
+ "below.</p>\n"
+ "<form action=\"/murach/email5/ join_email_list.html\" "
+ " method=\"post\">\n"
+ " <input type=\"submit\" value=\"Return\">\n"
+ "</form>\n"
+ "</body>\n"
+ "</html>\n");
}
}
Figure 5-2 The code for the EmailServlet class
Next: How to create a servlet >>
More Java Articles
More By Murach Publishing
|
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). Check it out today at your favorite bookstore. Buy this book now.
|
|