Debugging Servlets - Perspective (Page 4 of 4 ) The goal of this chapter has been to teach you the basics of coding, saving, and testing servlets. So at this point, you should be able to develop simple, but practical, servlets of your own. In addition, you should have a basic understanding of how servlets are executed. Note, however, that you usually don’t use servlets to send the HTML code back to the browser as shown in this chapter. Instead, you structure your web applications so servlets do the processing that’s required and JSPs send the HTML code back to the browser. In that way, you combine the best features of servlets with the best features of JSPs, and that’s what you’ll learn how to do in the next chapter. Summary -------------------------------------- - A servlet is a Java class that runs on a server, and a servlet for a web application extends the HttpServlet class.
- When you write servlets, you override the doGet and doPost methods to provide the processing that’s required. These methods receive the request object and the response object that are passed to them by the server.
- After you use the setContentType method of the response object to set the content type of the response that’s returned to the browser, you use the getWriter method to create a PrintWriter object. Then, you can use the println and print methods of that object to send HTML back to the browser.
- The class files for servlets must be stored in the WEB-INF\classes directory of an application or in a subdirectory that corresponds to a package name.
- To request a servlet from a URL, you include the word “servlet” after the document root directory in the path. This is followed by the package name, a dot, and the servlet name.
- You can override the init method of a servlet to initialize its instance variables. These variables are then available to all of the threads that are spawned for the one instance of the servlet.
- When you code a thread-safe servlet, you prevent two or more users from accessing the same block of code at the same time.
- To print debugging data to the server console, you can use the println method of the System.out or System.err object. An alternative is to use the log methods of the HttpServlet class to write debugging data to a log file.
Terms -------------------------------------- servlet request object response object content type instance variable thread-safe log file stack trace Objectives -------------------------------------- - Code and test servlets that require any of the features presented in this chapter.
- Provide debugging data for a servlet by writing messages to either the console or a log file.
- Describe the directory structure that must be used for servlet classes.
- Describe the difference between the URL for a JSP and the URL for a servlet.
- Describe the use of the init, doGet, and doPost methods in a servlet.
- Describe the execution of a servlet, and explain its effect on local and instance variables.
- Explain what is meant by a thread-safe servlet and describe what you have to do to develop one.
Exercise 5-1 Modify the EmailServlet class - Enter a URL in your browser that requests the EmailServlet class that’s located in the murach/WEB-INF/classes/email5 directory and sends two parameters to it.
- Run the HTML document named join_email_list.html in the email5 directory so it accesses and runs the EmailServlet class.
- Add the accessCount instance variable to the EmailServlet class as described in figure 5-7. Then, run the servlet from the HTML document. This will test whether servlet reloading has been turned on as described in chapter 2. If it isn’t on, you will have to stop and restart Tomcat before the changes to the servlet show up. (You may have to refresh your browser for the changes to take effect.)
- Modify the HTML document so it uses the Post method instead of the Get method, and modify the servlet so it works properly.
- Print a debugging message to the console that shows the value of the accessCount variable. Then, run the servlet two or more times to see how this message appears in the console.
- Repeat step 5, but use a log file this time.
Exercise 5-2 Create a new servlet In this exercise, you’ll modify the HTML document for the Email List application, and you’ll create a new servlet that responds to the HTML document. This is comparable to what you did for exercise 4-2, but the details are repeated here. - Modify the HTML document named join_email_list.html that’s in the email5 directory so it has this line of text after the Email address box: “I’m interested in these types of music.” Then, follow this line with a list box that has options for Rock, Country, Bluegrass, and Folk music. This list box should be followed by the Submit button, and the Submit button should link to a new servlet named MusicChoicesServlet.
- Create a new servlet named MusicChoicesServlet that responds to the changed HTML document. This servlet should respond with an H1 line that looks like this:
Thanks for joining our email list, John Smith.
And this line should be followed by text that looks like this:
We’ll use email to notify you whenever we have new releases for these types of music:
Country Bluegrass
In other words, you list the types of music that correspond to the items that are selected in the list box. And the entire web page consists of just the heading and text lines that I’ve just described.
- Test the HTML document and the new servlet by running them. Note the parameter list that is passed to the servlet by the HTML document. Then, test the new servlet by using a URL that includes a parameter list.
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |
| 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.
|
| |