Home arrow Java & J2EE arrow Page 11 - Developing JavaServer Pages

Class Location - Java

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.

  1. Developing JavaServer Pages
  2. The Code for the HTML Page that Calls the JSP
  3. Imitating HTML
  4. How to Create a JSP
  5. How to Use the Methods of the Request Object
  6. Retrieving Multiple Values
  7. How to Request a JSP
  8. Using Get and Post Methods
  9. Using the Post Method
  10. Managing Java Classes
  11. Class Location
By: Joel Murach
Rating: starstarstarstarstar / 65
May 12, 2004

print this article



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

  <title>Chapter 4 - Email List application</title>

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

<p>To enter another email address, click on the Back <br>
button in your browser or the Return button shown <br>

<form action="join_email_list.html" method="post">
  <input type="submit" value="Return">



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.


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.

>>> More Java & J2EE Articles          >>> More By Joel Murach

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- More Java Bugs Lead to More Attacks
- Oracle's Java One Brings News, Surprises
- Oracle Patches Java Runtime Environment
- Apple Syncs Java Update with Oracle
- Spring 3.1 Java Development Framework Compat...
- Jelastic Java PaaS Availability and Pricing ...
- NetBeans 7.1 Released, Supports JavaFX 2
- SolarWinds Releases Newest Version of Java M...
- Free Monitoring Tool for Java Apps on Heroku
- Heroku Adds JCloud Platform Support, Java 7 ...
- Java SE 8 Speculation in Full Swing
- Java SE 7 Now Available
- New JVM Language and Java Reporting Tool
- Java 7 Release Update and New Eclipse Toolkit
- The Best Java Netbeans IDE Plugins

Developer Shed Affiliates


Dev Shed Tutorial Topics: