In this excerpt from chapter 4 of Joel Murach's Java Servlets and JSP, you'll learn how to develop a web application that consists of HTML pages and JavaServer Pages (JSPs). As you will see, JSPs work fine as long as the amount of processing that's required for each page is limited. When you complete this chapter, you should be able to use JSPs to develop simple web applications of your own.
Since the User class contains a package statement that corresponds to the business directory, it must be located in the WEB-INFclassesbusiness directory. In contrast, if the package statement specified "murach.email", the compiled classes would have to be located in the WEB-INFclassesmurachemail directory.
Since TextPad is designed for working with Java, you can use it to compile regular Java classes. However, you may need to configure your system as described in appendix A of the text before it will work properly. In particular, you may need to add the appropriate WEB-INFclasses directory to your classpath.
If you use the DOS prompt window to compile your classes, you can do that as shown in this figure. Here, a DOS prompt is used to compile the User class. To start, the cd command changes the current directory to the WEB-INFclasses directory. Then, the javac command is used with "businessUser.java" as the filename. This compiles the User class and stores it in the business package, which is what you want.
A JSP that Uses the User and UserIO Classes
Figure 11 shows the code for the JSP in the Email List application after it has been enhanced so it uses the User and UserIO classes to process the parameters that have been passed to it. In the first statement of the body, a special type of JSP tag is used to import the business and data packages that contain the User and UserIO classes. You'll learn how to code this type of tag in the next figure. For now, though, you should focus on the other shaded lines in this JSP.
Figure 11:A JSP that uses the User and UserIO classes
The code for a JSP that uses the User and UserIO classes
<!doctype html public "-//W3C//DTD HTML 4.0 Transitional//EN"> <html>
<head> <title>Chapter 4 - Email List application</title> </head>
This JSP uses a scriptlet to create a User object and add it to a file, and it uses JSP expressions to display the values of the User object's instance variables.
Since the User and UserIO classes are stored in the business and data packages, the JSP must import these packages.
In the scriptlet of the JSP, the getParameter method is used to get the values of the three parameters that are passed to it, and these values are stored in String objects. Then, the next statement uses these strings as arguments for the constructor of the User class. This creates a User object that contains the three values. Last, this scriptlet uses the addRecord method of the UserIO class to add the three values of the User object to a file named UserEmail.txt that's stored in the WEB-INFetc directory. Since the WEB-INFetc directory isn't web-accessible, this prevents users of the application from accessing this file.
After the scriptlet, the code in the JSP defines the layout of the page. Within the HTML table definitions, the JSP expressions use the get methods of the User object to display the first name, last name, and email address values. Although these JSP expressions could use the String objects instead, the code in this figure is intended to show how the get methods can be used.
This tutorial is an excerpt from chapter 4 of Murach's Java Servlets and JSP. If you had any trouble understanding the HTML presented in this excerpt, or if you had any trouble getting the Email List application to run under Tomcat, you may want to download chapters 1 through 3 of this book. Within those chapters, you'll find an HTML tutorial, and you'll find detailed instructions on how to install and configure the Tomcat servlet/JSP container.
On the other hand, if you want to learn more about working with JSPs, you may want to download chapter 4 in its entirety. The remainder of the chapter shows how to use three more types of JSP tags and how to work with JSP errors. And if you want to know more about servlets, you may want to download chapter 5 of this book. This chapter shows how to implement the Email List application using servlets. Once you learn how JSPs and servlets work separately, you can learn how to use the best features of each by using them together within the MVC pattern.
Remember: this is from chapter four ofJoel Murach's Java Servlets and JSP (Mike Murach & Associates, ISBN 1890774189, 2003). Grab a copy at your favorite book store today!