Java
  Home arrow Java arrow Page 9 - Developing JavaServer Pages
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
VPS Hosting  
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid  
Request Media Kit
Contact Us  
Site Map  
Privacy Policy  
Support  
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
Google.com  
JAVA

Developing JavaServer Pages
By: Joel Murach
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 64
    2004-05-12


    Table of Contents:
  • Developing JavaServer Pages
  • The Code for the HTML Page that Calls the JSP
  • Imitating HTML
  • How to Create a JSP
  • How to Use the Methods of the Request Object
  • Retrieving Multiple Values
  • How to Request a JSP
  • Using Get and Post Methods
  • Using the Post Method
  • Managing Java Classes
  • Class Location

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article

     
     
    ADVERTISEMENT


    Developing JavaServer Pages - Using the Post Method
    ( Page 9 of 11 )

    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 of Joel Murach's Java Servlets and JSP (Mike Murach & Associates, ISBN 1890774189, 2003). Grab a copy at your favorite book store today!

     Buy this book now.



     
     
    >>> More Java Articles          >>> More By Joel Murach
     

       

    JAVA ARTICLES

    - Exception Handling Techniques in Java
    - More About Multithreading in Java
    - The Basics of Multiple Threads in Java
    - Data Access Using Spring Framework JDBC
    - New Object Initialization in Java
    - Adding Images With iTextSharp
    - Adding Columns With iTextSharp
    - Creating Simple PDF Files With iTextSharp
    - The Spring Framework: Understanding IoC
    - Introducing the Spring Framework
    - Java Classes
    - Completing the Syntactic Comparison of Java ...
    - Syntactic Comparison of Java and C/C++
    - Java Statements
    - Conditionals, Expressions and Other Java Ope...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 6 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek