The Email List application
This topic introduces you to a simple web application that consists of one HTML page and one JavaServer Page, or JSP. Once you get the general idea of how this application works, you’ll be ready to learn the specific skills that you need for developing JSPs.
The user interface for the application
Figure 4-1 shows the two pages that make up the user interface for the Email List application. The first page is an HTML page that asks the user to enter a first name, last name, and email address. Then, when the user clicks on the Submit button, the HTML page calls the JSP and passes the three user entries to that page.
Figure 4-1: The user interface for the application
The HTML page
The JSP
When the JSP receives the three entries, it could process them by checking them for validity, writing them to a file or database, and so on. In this simple application, though, the JSP just passes the three entries back to the browser so it can display the second page of this application. From this page, the user can return to the first page by clicking the Back button in the web browser or by clicking the Return button that’s displayed on this page.
As simple as this application is, you’re going to learn a lot from it. In this chapter, you’ll learn how enhance this application so it uses regular Java classes to save the user entries in a text file. Then, in later chapters, you’ll learn how to modify this application to illustrate other essential skills that apply to servlet and JSP programming.
The code for the HTML page that calls the JSP
Figure 4-2 presents the code for the HTML page that calls the JSP. If you’ve read chapter 3, you shouldn’t have any trouble following it. Here, the Action attribute of the Form tag calls a JSP named show_email_entry.jsp that’s stored in the same directory as the HTML page, and the Method attribute specifies that the HTTP Get method should be used with this action. Then, when the user clicks on the Submit button, the browser will send a request for the JSP.
Figure 4-2: The code for the HTML page that calls the JSP
<!doctype html public "-//W3C//DTD HTML 4.0 Transitional//EN"> <html> <head> <title>Chapter 4 - Email List application</title> </head> <body> <h1>Join our email list</h1> <p>To join our email list, enter your name and email address below. <br> Then, click on the Submit button.</p> <form action="show_email_entry.jsp" method="get"> <table cellspacing="5" border="0"> <tr> <td align="right">First name:</td> <td><input type="text" name="firstName"></td> </tr> <tr> <td align="right">Last name:</td> <td><input type="text" name="lastName"></td> </tr> <tr> <td align="right">Email address:</td> <td><input type="text" name="emailAddress"></td> </tr> <tr> <td></td> <td><br><input type="submit" value="Submit"></td> </tr> </table> </form> </body> </html>
You should also notice the Name attributes of the three text boxes that are used in the table within this HTML page. These are the names of the parameters that are passed to the JSP when the user clicks on the Submit button. In figure 4-1, the parameter names are firstName, lastName, and emailAddress and the parameter values are John, Smith, and jsmith@hotmail.com.
The code for the JSP
Figure 4-3 presents the code for the JSP. As you can see, much of the JSP code is HTML. In addition, though, Java code is embedded within the HTML code in the form of JSP scriptlets and expressions. Typically, a scriptlet is used to execute one or more Java statements while a JSP expression is used to display text. To identify scriptlets and expressions, JSPs use tags. To distinguish them from HTML tags, you can refer to them as JSP tags.
Figure 4-3: The code for the JSP
<!doctype html public "-//W3C//DTD HTML 4.0 Transitional//EN"> <html> <head> <title>Chapter 4 - Email List application</title> </head> <body> <% String firstName = request.getParameter("firstName"); String lastName = request.getParameter("lastName"); String emailAddress = request.getParameter("emailAddress"); %> <h1>Thanks for joining our email list</h1> <p>Here is the information that you entered:</p> <table cellspacing="5" cellpadding="5" border="1"> <tr> <td align="right">First name:</td> <td><%= firstName %></td> </tr> <tr> <td align="right">Last name:</td> <td><%= lastName %></td> </tr> <tr> <td align="right">Email address:</td> <td><%= emailAddress %></td> </tr> </table> <p>To enter another email address, click on the Back <br> button in your browser or the Return button shown <br> below.</p> <form action="join_email_list.html" method="post"> <input type="submit" value="Return"> </form> </body> </html>
Description
- Although a JSP looks much like an HTML page, a JSP contains embedded Java code.
- To code a scriptlet that contains one or more Java statements, you use the <% and %> tags. To display any expression that can be converted to a string, you use the <%= and %> tags.
- When you code a JSP, you can use the implicit request object. This object is named request. You can use the getParameter method of the request object to get the values of the parameters that are passed to the JSP.
When you code a JSP, you can use the methods of the request object in your scriptlets or expressions. Since you don’t have to explicitly create this object when you code JSPs, this object is sometimes referred to as the implicit request object. The scriptlet in this figure contains three statements that use the getParameter method of the request object. Each of these statements returns the value of the parameter that is passed to the JSP from the HTML page. Here, the argument for each getParameter method is the name of the textbox on the HTML page.
Once the scriptlet is executed, the values for the three parameters are available as variables to the rest of the page. Then, the three expressions can display these variables. Since these expressions are coded within the HTML tags for a table, the browser will display these expressions within a table.
After the table, the JSP contains some HTML that defines a form. This form contains only one control, a submit button named Return. When it is clicked, it takes the user back to the first page of the application. If you have any trouble visualizing how this button or the rest of the page will look when displayed by a browser, please refer back to figure 4-1.
As you read this book, remember that it assumes that you already know the basics of Java programming. If you have any trouble understanding the Java code in this chapter, you may need a refresher course on Java coding. To quickly review the basics of Java coding, we recommend that you use Murach’s Beginning Java 2 because it contains all the Java skills that you’ll need for working with this book.
Excerpted from Chapter 4 of Murach’s Java Servlets and JSP
Click here to sign up for FREE Tech. newsletters from Murdok!
Joel Murach has been writing and editing for more than 10 years. During that time, he sharpened his programming skills as a contract programmer in San Francisco and his instructional skills as a trainer for HarperCollins Publishing. He always brings a vision to his projects that leads to improved effectiveness for his readers.