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.
There are two primary reasons for using the Post method. First, since the Post method doesn't append parameters to the end of the URL, it is more appropriate for working with sensitive data. If, for example, you're passing a parameter for a password or a credit card number, the Post method prevents these parameters from being displayed in the browser. In addition, it prevents the web browser from including these parameters in a bookmark for a page. Second, you need to use the Post method if your parameters contain more than 4 KB of data.
For all other uses, the Get method is preferred. It runs slightly faster than the Post method, and it lets the user bookmark the page along with the parameters that were sent to the page.
How to Use Regular Java Classes with JSPs
In this topic, you'll learn how to use regular Java classes to do the processing that a JSP requires. In particular, you'll learn how to use two classes named User and UserIO to do the processing for the JSP of the Email List application.
The Code for the User and UserIO Classes
Figure 9 presents the code for a business class named User and an I/O class named UserIO. The package statement at the start of each class indicates where each class is stored. Here, the User class is stored in the business directory because it defines a business object while the UserIO class is stored in the data directory because it provides the data access for the application.
Figure 9:The Code for the User and UserIO Classes
The Code for the User Class
package business;
public class User{ private String firstName; private String lastName; private String emailAddress;
public User(){}
public User(String first, String last, String email){ firstName = first; lastName = last; emailAddress = email; }
public void setFirstName(String f){ firstName = f; } public String getFirstName(){ return firstName; }
public void setLastName(String l){ lastName = l; } public String getLastName(){ return lastName; }
public void setEmailAddress(String e){ emailAddress = e; } public String getEmailAddress(){ return emailAddress; } }
The Code for the UserIO Class
package data;
import java.io.*; import business.User;
public class UserIO{ public synchronized static void addRecord(User user, String filename)throws IOException{ PrintWriter out = new PrintWriter( new FileWriter(filename, true)); out.println(user.getEmailAddress()+ "|" + user.getFirstName() + "|" + user.getLastName()); out.close(); } }
Note The synchronized keyword in the declaration for the addRecord method of the UserIO class prevents two users of the JSP from using that method at the same time.
The User class defines a user of the application. This class contains three instance variables: firstName, lastName, and emailAddress. It includes a constructor that accepts three values for these instance variables. And it includes get and set methods for each instance variable.
In contrast, the UserIO class contains one static method named addRecord that writes the values stored in a User object to a text file. This method accepts two parameters: a User object and a string that provides the path for the file. If this file exists, the method will add the user data to the end of it. If the file doesn't exist, the method will create it and add the data at the beginning of the file.
If you've read the first six chapters of Murach's Beginning Java 2, you should understand the code for the User class. And if you've read chapters 16 and 17, you should understand the code in the UserIO class. The one exception is the use of the synchronized keyword in the addRecord method declaration. But this keyword just prevents two users from writing to the file at the same time, which could lead to an error.
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!