Java & J2EE Page 9 - Developing JavaServer Pages |
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
The Code for the UserIO Class
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.
blog comments powered by Disqus |