Java
  Home arrow Java arrow Page 11 - Developing JavaServer Pages
FaxWave - Free Trial.
Dev Shed Forums 
Administration  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Download TestComplete 
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? 
JAVA

Developing JavaServer Pages
By: Joel Murach
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 61
    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:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb 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

    Dell PowerEdge Servers

    Developing JavaServer Pages - Class Location
    (Page 11 of 11 )

    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>

    <body>
    <%@ page import="business.*, data.*" %>
    <%
        String firstName = request.getParameter("firstName");
        String lastName = request.getParameter("lastName");
        String emailAddress = request.getParameter("emailAddress");

        User user = new User(firstName, lastName, emailAddress);
        UserIO.addRecord(user, "../webapps/murach/WEB-INF/etc/UserEmail.txt");
    %>

    <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><%= user.getFirstName() %></td>
        </tr>
        <tr>
          <td align="right">Last name:</td>
          <td><%= user.getLastName() %></td>
        </tr>
        <tr>
          <td align="right">Email address:</td>
          <td><%= user.getEmailAddress() %></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

    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.

    Conclusion

    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 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.


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

     

       

    JAVA ARTICLES

    - 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...
    - Java Operators
    - Primitive Data Types and Basic Language Rule...
    - Java and Object-Oriented Programming
    - Java Beginning Programming
    - Gaming Development Setup
    - Using RPC-Style Web Services with J2EE
    - Integrating XML with J2EE
    - Taming Tiger: Concurrent Collections

     
    Accelerating Trading Partner Performance
     
    Competing on Analytics
     
    Cost Effective Scaling with Virtualization and Coyote Point Systems
     
    Five Checkpoints to Implementing IP Telephony
     
    Hosted Email Security: Staying Ahead of New Threats
     




    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway