Beruflich Dokumente
Kultur Dokumente
Legal Notice
Any opinions expressed herein are solely those of the creators of the material and are
not the opinions of BEA Systems, Inc. or its management.
TABLE OF CONTENTS
1. Welcome ....................................................................................................................7
What is Covered and Why ...........................................................................................8
2. Forward......................................................................................................................9
Prerequisites and Audience .......................................................................................11
3. Why Java? ...............................................................................................................12
Benefits of Java on the Web ......................................................................................12
4. Dynamic Web History..............................................................................................14
A Journey from CGI to Today ....................................................................................14
Chapter Goals........................................................................................................15
Common Gateway Interface (CGI) .........................................................................15
Active Server Pages (ASP Classic or ASP 3.0) ......................................................16
Hypertext Preprocessor (PHP) ...............................................................................17
Coldfusion Markup Language (CFM)......................................................................18
Java Server Pages (JSP) .......................................................................................19
ASP.NET................................................................................................................20
Emerging Web Application Frameworks.................................................................21
Ruby on Rails .....................................................................................................21
Django................................................................................................................22
5. Foundations.............................................................................................................23
Chapter Goals........................................................................................................23
Born from Servlets .....................................................................................................24
Application Server Architecture..................................................................................28
Free Java Application Servers................................................................................30
Apache Tomcat ..................................................................................................31
JBoss .................................................................................................................31
GlassFish ...........................................................................................................31
Commercial Java Application Servers ....................................................................32
Acronyms Abound: JDK, SDK, JRE, J2EE, Java EE, J2SE, J2ME and JVM
Demystified................................................................................................................32
JDK (formerly the Software Development Kit) ........................................................33
JRE ........................................................................................................................33
J2EE ......................................................................................................................33
Java EE..................................................................................................................33
J2SE ......................................................................................................................34
J2ME......................................................................................................................34
JVM........................................................................................................................34
Development Tools....................................................................................................34
NetBeans: ..............................................................................................................35
Eclipse ...................................................................................................................36
Another Neat Tool (Ant) .........................................................................................37
The Anatomy of a JSP Application.............................................................................38
A Quick Note on Development............................................................................40
What are WAR Files?.............................................................................................40
What are JAR Files? ..............................................................................................41
Understanding Classpath .......................................................................................41
A Java Server Page Dissected...............................................................................42
Scriptlet <% … %>..............................................................................................43
Directive <%@ … %> ........................................................................................45
Expression <%= … %>.......................................................................................47
Declaration <%! … %> .......................................................................................48
Actions................................................................................................................49
Comment <%-- … --%> ......................................................................................50
Chapter Summary......................................................................................................50
6. Digging In.................................................................................................................51
Chapter Goals........................................................................................................51
The Nuts and Bolts of Java Server Pages .................................................................52
Implicit Objects.......................................................................................................52
Dealing with Data - JDBC.......................................................................................54
Connecting to a Database with JDBC .................................................................54
Retrieving Data from a Database........................................................................56
Adding Data to a Database.................................................................................58
Updating Data within a Database........................................................................59
Deleting Data from a Database...........................................................................59
What are JavaBeans? ...............................................................................................61
Attributes of a Bean................................................................................................64
Accessing a Bean ..................................................................................................65
Building and Deploying a Bean ..............................................................................67
Developing with Tags – JSTL / EL .............................................................................69
Java Server Pages Standard Tag Library (JSTL) ...................................................70
Accessing JSTL..................................................................................................71
Working with Variables .......................................................................................71
Expression..........................................................................................................71
Iteration ..............................................................................................................72
Conditionals........................................................................................................72
URL Management ..............................................................................................73
Exception Handling with Try / Catch ...................................................................74
Expression Language (EL) .....................................................................................74
Tags in Action - Examples......................................................................................75
Developing Custom Tags...........................................................................................79
Sending Email ...........................................................................................................92
File Uploads...............................................................................................................93
Capturing User Input with Forms ...............................................................................95
Validating User Input: ..............................................................................................101
JavaScript ............................................................................................................101
JSTL Form Validation...........................................................................................102
Advanced Validation ............................................................................................103
When Things Go Wrong - Error Handling ................................................................103
Chapter Summary....................................................................................................105
7. Reference Application...........................................................................................106
Chapter Goals......................................................................................................107
The Database ..........................................................................................................107
Scriptlet Sample ......................................................................................................109
Customer Listing ..................................................................................................110
Deleting a Customer ............................................................................................112
JSTL Sample ...........................................................................................................113
Customer Listing ..................................................................................................114
Adding a Customer ..............................................................................................115
Model View Controller (MVC) and JavaBeans .........................................................117
Controller Servlet .................................................................................................118
Customer Listing ..................................................................................................129
Adding a Customer ..............................................................................................130
Updating a Customer ...........................................................................................131
8. Map of Technology................................................................................................132
9. Next Steps..............................................................................................................133
Jakarta Struts ..........................................................................................................134
Java Server Faces...................................................................................................134
Hibernate.................................................................................................................139
Welcome
WELCOME
This primer has been developed to provide a 15,000 foot overview of Java Server Pages
(JSP), the plumbing that supports it and related technologies. If you only have a few
hours one afternoon and need to quickly get up to speed on these technologies, this
collection of material has been designed as an excellent resource.
Feel free to jump directly to a chapter that you are interested in, as this book also
contains supplemental information about competing web technologies, details of Java
application servers and more.
I hope you will find this book to be highly informative, and welcome any feedback that
you may have at jspandbeyond@gmail.com.
FORWARD
Building real world solutions with powerful technology is a challenging and thoroughly
enjoyable endeavor.
After working for leading software technology companies and developing a range of
web-based .NET solutions, I felt compelled to peak over the fence and onto the Java
side of the industry. What I found was a challenging, often difficult to understand, range
of technologies that would supposedly come together to create business solutions. Java
Beans, JSPs, EJBs, Struts, JVM, JDK… what do they all mean? What do they do?
How can I build solutions with these building blocks? I was also certain that there were
probably many other people with these same curiosities.
Fast forwarding through a series of late nights and plenty of espressos later, I began to
wrap my head around what these technologies were all about.
Even though my journey was enjoyable and enlightening, I wished that there would have
been some “jumpstart” guide to get me up and running faster with these technologies. It
was at this point that I gained the inspiration for this book, with the goal of helping others
quickly pickup on these Java-based technologies in an easy to understand manner.
“What tools do I need to create applications?”, “How do I install a Java application server
to run my applications?”, “How can I read data from a database?” and “How can I update
data in a database?” are just some of the questions that I want to answer for those
starting the journey as I did. I am not here to focus on minutia, that can be done at a
later date, but in order to help you start building solutions today in a very real, non-
academic environment.
Even though it seems that Java technologies are generally aimed at a more engineering
oriented crowd, I see no reason why they cannot be leveraged by a wider audience.
Hopefully I can help you bridge that gap so that you can begin to embrace some of the
features of Java technologies that can be used to meet your organization’s business
needs and ease your application development management.
Novice developers new to Java Server Pages, business analysts and technical
managers can all benefit from the architectural information provided in this book. For
experienced developers the goal is to act as a stepping stone to more powerful JSP-
based (JSF, Struts, etc) development systems.
WHY JAVA?
There are a range of application development technologies for the web, each with their
strengths and weaknesses that are discussed in the next chapter. In short, Java-based
technologies offer a modern, robust, cross-platform technology for development.
The biggest draw to this technology is undoubtedly the ability to deploy applications
inside of any Java application server on an extremely wide range of platforms. This
means that you can avoid any sort of vendor lock-in and have an overwhelming choice
of operating systems to deploy your applications on.
Some people might be quick to point out that other technologies are cross-platform and
can be used to develop applications as well. However, Java has a unique advantage as
it has been crafted with the web in mind and offers a mature, enterprise-ready, robust
framework for application deployment.
At the time of this writing there is even more excitement surrounding the technology, as
Sun Microsystems, the creators of Java, are fueling rumors that they may open source
the technology to spread adoption. This direction is uncertain, but without a doubt the
future of this technology is bright.
If you want to learn a technology for developing robust web applications, learning Java
technologies is a sound investment that will pay off in spades.
I am not going to give you the old “in my day we walked to school uphill both ways in a
blizzard” story, but after reading this section you will be glad that the tools have evolved.
Development is now faster and easier than ever before, making it easy to take these
new, more evolved tools for granted and to forget the past.
Chapter Goals
• Gain an understanding of the landscape for server-side technologies that
produce dynamic web pages and content
• Learn how development capabilities have evolved over time to address recurring
needs in application development
You might therefore ask why this is included in the Dynamic Web History section. CGI
was the first major method by which dynamic web content was generated and is still
serving dynamic content on the web for many sites today. Most CGI programs are
written in Perl and there are a wide range of web sites that provide Perl scripts for guest
books, shopping carts and other web application components.
That being said, there are a few drawbacks that make other development options more
attractive for building web-based applications.
Drawbacks of CGI
There is nothing inherently wrong with using this tool to build applications, but CGI does
suffer from some general problems
• Scalability - Each time a request is made to a CGI application the application
needs to be instantiated, respond to the request and then be destroyed. This is a
very high overhead operation for each request and is a major drawback of CGI
technology
• Security - CGI applications are generally run under accounts with permission to
access files and OS related operations. A poorly written CGI program can
inadvertently open security holes in an application.
• Runtime - Any errors that occur through a CGI application have the ability to
harm the operation of the web server, since it is closely tied to the OS.
• Debugging - Debugging a CGI application can be more difficult than in other
languages used for the web (ASP, PHP, ASP.NET and JSP).
• Limited Functionality - More modern languages designed for the web have
access to more information about the environment, whereas CGI is restricted to
the elements from a form post or get.
• Separation of Presentation and Logic – In CGI code, both presentation and
business logic are mixed. That means that anyone who is developing a CGI
application will have to retain specialists in the language used to develop their
application in order to make any changes to the appearance. It also means that
a CGI page can contain a large amount of code that is difficult to maintain.
ASP provided developers with a series of tools to solve common web application needs.
This allowed them to speed application development and focus more on the solution,
rather than building the underlying processes to facilitate communications and functions.
ASP also allows access to COM objects. This allows it to use existing business logic
that has been built using COM objects.
<html><body>
<%
Response.Write('Hello World!');
%>
</body></html>
Drawbacks of ASP
Many web sites use ASP to generate content. However, newer languages that provide
more robust functionality and ASP is becoming a less common solution for building
dynamic web sites.
• Platform – ASP can only run on a Microsoft’s Internet Information Services web
server on a Microsoft operating system*.
• Separation of Presentation and Logic – Using ASP it is difficult to separate
business logic from presentation, however leveraging COM can mitigate this
issue.
• Interpreted at Run Time – ASP code is interpreted line by line with each request.
This means that it is less efficient than compiled code.
*There are some third party tools that will allow ASP to run on a different operating
system and web server.
PHP is known for allowing people to easily and quickly create dynamic web-based
content on a range of platforms such as Windows, Linux, Solaris, and various other Unix
systems. PHP is open source and has very broad community support.
<?php
// Hello World in PHP
echo 'Hello World!';
?>
Drawbacks of PHP
PHP has continued to improve and remains a popular language for developing dynamic
web content, but historically has some drawbacks.
• Not Designed as Object Oriented – PHP has not been designed with object
oriented programming in mind. It was not until version 4 that an object model
was introduced to the language.
• Lack of Exception Handling – PHP does not have a native structure to handle
exceptions. PHP 5 has introduced error handling.
• Separation of Presentation and Logic – PHP lacks the ability to separate
presentation and logic. This means that complex projects can become difficult to
design and maintain.
• Interpreted at Run Time – PHP code is interpreted line by line with each request.
This means that it is less efficient than compiled code. There is, however, a tool
from a commercial organization named Zend that will precompile PHP code.
ColdFusion MX 7 now runs on a Java Application Server, but older versions ran on an
application server developed in C++ which gave Coldfusion a reputation for poor
performance. Coldfusion Markup has the distinction of being a completely tag-based
language. Interestingly enough,it has some similarities to Java Server Tag Language, or
JSTL, allowing developers to quickly build applications with a tag-based framework that
has been specifically designed for web application development. For instance, the
sample below illustrates how an email could be sent from Coldfusion. As you will notice
later in this book, JSTL is very similar to Coldfusion Markup.
<cfmail from="jspbeyond@example.com"
to="recipientemail@jspbeyond.com" subject="An Email from JSP Beyond"
server="mail.jspbeyond.com">
Sample Email Body Here
</cfmail>
Drawbacks of CFM
• Depth – Coldfusion does not have the depth that a more complete stack like
.NET or Java has. However, it is possible to have some interaction with a Servlet
using a special tag.
• Scalability – Coldfusion has received a reputation for scalability problems.
However, now that Coldfusion has been designed to run within a J2EE container,
it has enhanced scalability compared with prior versions.
Beyond the separation of presentation from business logic and data, JSP offers
tremendous advantages around enterprise system integration, as many large scale
systems have integration points that can be accessed with Java. To enable platform
independence these access points, or APIs, have been developed to use Java, thereby
enabling JSPs to access and use these other systems.
ASP.NET
Originally known as ASP+, ASP.NET is Microsoft’s next generation web platform.
Version 1.0 was released to the public in 2002. From a development standpoint the
Microsoft stack is unquestionably powerful. Developers often find the stack easiest to
work with, thereby making it easier for companies to find developers who are familiar
with the technology stack.
One of the somewhat unique distinctions of the ASP.NET technology is the concept of
“controls”, or reusable building blocks that can easily be included into code to provide
calendaring functionality, database results display, result set paging and more. Other
technologies have similar features, but due to its native support control, use in
development is quite popular. This model has so many benefits that the Java world has
created something similar called JSF that uses “components” to provide a similar
mechanism. JSF will be explored in the Next Steps chapter at the end of this book.
Controls also abstract the request and response elements of a web application. For
instance, to retrieve the select value on a calendar, a code block like
“Calendar.SelectedDate.Value” could be used instead of using the request object to
obtain the value as more traditionally done (Request[“CalendarSelectedDate”]).
Drawbacks of ASP.NET
ASP.NET is a powerful platform, but does have some limitations as noted below.
• Platform – ASP.NET applications can only run on a Microsoft Internet Information
Services web server on a Microsoft operating system. Projects like Mono.net
from the open source community are developing solutions to expand the support
that .NET can enjoy, but I have reservations about this being embraced in the
business world.
• MVC – Not perceived by all people as a drawback the ASP.NET model does not
support a true MVC framework out-of-box. Due to the business logic for
ASP.NET applications getting stored inside code-behind layer ASP.NET offers
the business logic and presentation separation, but adds some constraints
around how request routing can be handled.
• Cost – The operating system, database, development tools and application
server are only available commercially through Microsoft.
Ruby on Rails
Out of all of the emerging frameworks few are talked about as much as Ruby on Rails.
Rails is a framework that lets the Ruby language become extended to the web, providing
many built-in tools commonly required by web applications. One of the major
assumptions made by Ruby on Rails is that the web application will be backed by a
database. An object layer automatically created over the database requires a
programmer to write very little SQL, if any at all, since everything is dealt with through
objects. The Next Steps chapter will review a tool called Hibernate that can be used to
provide a similar object layer to abstract interactions with a database. Rails also
provides form validation, error messages and many other common web application
features.
To highlight the momentum behind the Ruby movement it is interesting to note that Ruby
on Rails will be shipping with the latest version of Apples MAC OSX Leopard.
Django
Django is very similar to Ruby on Rails, providing a pragmatic MVC framework with
object modeled database abstraction, but uses Python as its programming language. It
has not received quite as much press as Ruby on Rails, and the community surrounding
it is less mature than the one for Ruby on Rails. In addition there are far less hosting
options which makes production deployment somewhat of a hassle. That said, the
Python language is more prevalent within the enterprise then Ruby. It will be interesting
to see how much traction this framework gains over time.
FOUNDATIONS
Now that we have a good handle on the history of application development on the web,
let us examine what it takes to develop and serve Java Server Pages applications. One
of the biggest challenges in JSP development is the extremely steep learning curve due
to the many options that it offers.
This chapter helps to explore the underpinnings of JSP applications. It will explain how
JSP is derived from Servlets, the nuts and bolts of application servers, acronyms related
to development kits that you will need to know and development tools that will help you
get up and running as quickly as possible for the next chapter, Java Server Pages
Deconstructed.
Chapter Goals
• Gain an understanding of how JSPs are essentially an extension on top of
Servlets
• Learn about how JSPs are processed by application servers, also known as
containers
• Comprehend the distinctions between various “kits” that are needed for JSP
development
• Review the ways in which JSPs incorporate various pieces of markup to
generate dynamic content, and the various ways they can interact with user
requests, format their responses and access information from the container that
the JSP runs in
I hesitate to completely confuse people, but Java Server Pages actually turn into
Servlets within the container when they are requested by the end-user. Java Server
Pages are so powerful for this very reason. They give you the ability to manipulate the
page’s presentation of dynamic data while leveraging HTML without having to do
complex server side coding, and yet you reap the power of the server side coding.
Below I have included two “Hello World” code samples, one using a Servlet to create the
content and another using a JSP to create the content. Both methods produce the same
output. This sample with code comments is designed to allow Servlets and JSPs to
relate to each other, as JSPs are an abstraction layer on top of Servlets. Do not worry
about the details of the Servlet code, as we will dig deeper into the code later in the
book.
HelloWorld Servlet
package org.apache.jsp;
import javax.Servlet.*;
import javax.Servlet.http.*;
import javax.Servlet.jsp.*;
try {
_jspxFactory = JspFactory.getDefaultFactory();
response.setContentType("text/html");
pageContext = _jspxFactory.getPageContext(this, request, response,
null, true, 8192, true);
_jspx_page_context = pageContext;
application = pageContext.getServletContext();
config = pageContext.getServletConfig();
session = pageContext.getSession();
out = pageContext.getOut();
_jspx_out = out;
out.write("<html>\
out.write("<html>\n");
out.write(" <head>\
<head>\n");
out.write(" <title>JSP and Beyond Hello World</title>\
World</title>\n");
out.write(" </head>\
</head>\n");
out.write(" <body>\
<body>\n");
out.write(" Hello World \n");
out.write(" </body>\
</body>\n");
out.write("</html>\
out.write("</html>\n");
} catch (Throwable t) {
if (!(t instanceof SkipPageException)){
out = _jspx_out;
if (out != null && out.getBufferSize() != 0)
out.clearBuffer();
if (_jspx_page_context != null)
_jspx_page_context.handlePageException(t);
}
} finally {
if (_jspxFactory != null)
_jspxFactory.releasePageContext(_jspx_page_context);
}
}
}
<%@page contentType="text/html"%>
<html>
<head>
<title>JSP and Beyond - Hello World</title>
</head>
<body>
<!-- This is our Hello World code -->
<%= out.println("Hello World") %>
</body>
</html>
This is the HTML that is produced from the above Servlet and the JSP code!
<html>
<head>
<title>JSP and Beyond Hello World</title>
</head>
<body>
Hello World
</body>
</html>
Which one do you think took more effort to write? Which one makes it easier to manage
the layout of the page? Even though we do not have to do complex coding in the JSP
the Java Application Server will handle all of the translation to Servlet code for us as
JSPs are transformed into Servlets when they are run.
On the highlighted lines in the code for the Servlet above it is interesting to notice that
the out.write lines produce the HTML lines that we see in the HelloWorld.JSP code. This
is much the equivalent of doing a Response.Write using Microsoft ASP or ASP.NET.
Speaking of Java Application Servers let’s look at how the HelloWorld.JSP page above
was processed to produce the HelloWorld Servlet that is ultimately executed to render
the content above in the Application Server section below.
It is important to understand at a high level how the container processes a request from
a user, as this interaction underlies all JSP operations. The diagram below illustrates
how an application server deals with these requests for dynamic content.
1. The client (i.e. user) makes a request for an item on the server (JSP, JPG, GIF,
etc.).
2. The web container checks to see if the content has been requested before. If so
the container will serve the content from an existing class file which is the result
of a previously requested JSP (step B). If the content has not been requested
before the container will build a class file and then process the request by
executing the class file (step A).
3. The content is then served back to the HTTP server to be sent back to the client.
Regardless of the application server that is used to run your Java application, it will
always process the request in the same nature. For our purposes we will initially work
with a free container like Tomcat.
So… if that is Tomcat, what is this Apache Tomcat thing that I am hearing about? That
is an excellent question!
Although at first a bit confusing, application servers have historically combined with an
HTTP server for maximum performance in a slightly different manner than the sample
above, which used a single application server that acts as both the java application
server and HTTP server.
This has been done because the Apache HTTP server is designed to serve static
content (HTML, GIF, JPG, CSS, JS, etc.) at a high rate under a high load. While
Tomcat, the Java Application Server, by itself is capable of serving this content as well, it
is not as optimized or configurable as an Apache HTTP server for this task. This is why
it is common to hear about “Apache Tomcat”. The process of using a separate HTTP
server and Java Application Server to handle an HTTP request are outlined below.
1. The client (i.e. user) makes a request for an item on the server (JSP, JPG, GIF,
etc.).
2. The HTTP server checks to see if the request is a static item (JPG, GIF, etc.) or
an item that needs further processing (JSP).
3. The web container checks to see if the content has been requested before. If so
the container will serve the content from an existing class file. If the content has
not been requested before the container will build a class file and then process
the request by executing the class file.
4. The dynamic content is then served back to the HTTP server to be sent back to
the client.
Apache Tomcat
Perhaps the most popular of all web containers due to its flexibility and simplicity,
Apache Tomcat has a significant place in the world of Java Server Pages. Apache
Tomcat is the Servlet container that is used in the official Reference Implementation for
the Java Servlet and Java Server Pages technologies. This means that it follows the
design specification exactly from Sun Microsystems.
Apache Tomcat was developed in an open and participatory environment and released
under the Apache Software license. Since Tomcat contains an HTTP server it can also
be used as a standalone web server. Tomcat was developed entirely in Java. This
allows it to run on any platform that has a Java Virtual Machine.
JBoss
Now owned by RedHat, Inc, JBoss is an application server developed purely in Java.
This allows it to run on any platform that has a Java Virtual Machine just as Apache
Tomcat.
JBoss is a free, open source, application server that implements the complete Java 2
Enterprise Edition (J2EE) stack, including Java Server Pages (JSP), Servlets, and
Enterprise JavaBeans (EJB).
GlassFish
GlassFish is a free, open source application server that contains some of the newest
features for Java application servers. It was developed by Sun Microsystems.
Some examples of commercial Java application servers are BEA WebLogic, IBM
WebSphere, Oracle Application Server and Macromedia JRun.
The list below details each acronym and what it is used for. A summary table lies below
the list and can be used for quick reference.
JRE
The Java Runtime Environment (JRE) consists of the barebones components to run
Java applications. It contains a Java Virtual Machine (JVM), core classes and supporting
files. It does not contain components for application development like the JDK.
J2EE
Java 2 Platform Enterprise Edition is a suite of tools that help developers create
connections to enterprise data and systems. Specifically, Enterprise JavaBeans (EJBs),
JavaServer Pages (JSPs) and Java Servlets can be used to access the data through
tools like JDBC, JNDI, JTA, JMS, Java Mail and CORBA. For our purposes we will only
be covering a fraction the J2EE platform abilities, specifically focusing on JSPs, the
presentation layer of the J2EE technology stack.
Java EE
As the J2EE platform has evolved Sun Microsystems has added additional functionality.
Some of the new features that Java EE added are support for Java Server Faces,
enhanced web services capabilities and better ways to interact with Java code brokering
database access. This highlights the Java community’s evolution based on the common
needs that are continually identified by organizations looking for easier ways to develop
software.
J2SE
Think of Java 2 Platform, Standard Edition (J2SE) as JDK plus. It includes a wide range
of components like Swing (a user interface library) for application development on
desktops and servers and for deployment in embedded environments.
J2ME
The Java 2 Platform Micro Edition enables PDAs, TV set top boxes, ovens, printers, cell
phones and other devices to run Java applications. Just as JVMs are specific to a
particular platform each device that is J2ME-capable needs a specific “profile” for J2ME
code to work on it based on a device’s particular capabilities.
JVM
The Java Virtual Machine is the heart of the Java applications. It provides an abstraction
layer on top of the operating system that is capable of running Java applications. This
makes it platform independent, allowing Java code to run on Windows, UNIX, Linux and
any platform that a JVM has been developed for. At a nuts and bolts level the JVM is
able to consume compiled Java code (bytecode) into machine language where it can be
run on a particular platform. This is Java’s claim to fame and gives way to the
expression “write once, run anywhere”.
Development Tools
In order to speed our development process we have the luxury of being able to use an
Integrated Development Environment (IDE). Some purists might say that IDEs hold
people back from learning about what really occurs when you build an application.
Unfortunately, I think that this viewpoint stems from the insecurity that they feel due to
the tremendous power of IDEs today opening the development of applications to a
greater range of users who have little interest in memorizing every class and it methods,
but instead want to focus on solving the business problem at hand.
If I were going to run a marathon, wouldn’t I want to have the best shoes? If I were
going to race cars, wouldn’t I want to have the best automobile? This book is all about
practicality and creating solutions to problems, not spending all day trying to do things in
cryptic, archaic ways.
We are therefore going to be pragmatic and give ourselves the best shoes, the best car
and get building! Here are some powerful IDEs that can help you start to make the most
of your experience building solutions.
NetBeans:
NetBeans is a free IDE from Sun Microsystems. It is a great tool for letting developers
quickly get into Java development, as it comes packaged with a built-in Java Application
Server preconfigured to run your projects, and is aware of syntax for web application
development immediately out-of-the-box. I highly recommend this IDE for working with
the samples that we will review over the course of this book.
You can learn more about this IDE and download it at http://www.netbeans.org/.
Eclipse
Eclipse is a powerful open source IDE. Once you become more familiar with Java
development it might be worthwhile giving it a try. Eclipse has been developed to be
extensible and there are now a wide series of “plugins” that enable various features
within the development environment. A series of examples can be found at
http://www.eclipse.org/projects/. Some organizations offer a preloaded version of
Eclipse with plugins to assist with Java web development that offers a quick, easy
method to get started. One such example is Lomboz which can be downloaded at
http://lomboz.objectweb.org/.
Many large software organizations that used to build their own IDEs (BEA and IBM)
discontinued their tools and now ship components called plugins that run inside of
Eclipse and provide that same functionality.
You can learn more about and download this IDE at http://www.eclipse.org/.
In any medium- or large-scale software project it is not unusual to spend a lot of time
preparing your files to get sent to a server where end users will use or test your
application. Ant excels at providing repeatable build processes, especially for more
complex deployments, when many files may need to be compiled, adjusted, directories
created, moved, sent over FTP, etc. Here is an example of an Ant file from the Apache
Ant help file that demonstrates how the XML file guides the build process.
<target name="init">
<!-- Create the time stamp -->
<tstamp/>
<!-- Create the build directory structure used by compile -->
<mkdir dir="${build}"/>
</target>
<target name="clean"
description="clean up" >
<!-- Delete the ${build} and ${dist} directory trees -->
<delete dir="${build}"/>
<delete dir="${dist}"/>
</target>
</project>
Our purpose here is not to dive into the details of Ant, but to help you understand that it
is a very powerful tool for standardizing deployments and saving time. The above
sample from Apache starts to hint at how much activity can be automated with a single
mouse click.
Each section of the directory hierarchy has been created to serve a particular purpose
as illustrated below. Once you become accustomed to the directory format, it becomes
easily navigable and logically structured.
At the very root of the container’s file structure there is generally a directory to house the
web applications. It is within this directory that you will place your application files. In
the examples below, let us suppose that you have a web application name called
“YourWebApp”.
YourWebApp/
Within this directory all static content should reside. This includes JSP, HTML and
image files.
YourWebApp /WEB-INF/
The Web Application deployment descriptor (web.xml) lives within this directory. This
deployment descriptor maintains all of your application settings that dictate how the
container delivers your application. The following code sample shows a web.xml file that
lets the container know that the default file for the directory is index.jsp and the
application timeout is set at 30 minutes.
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
version="2.4">
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>
index.jsp
</welcome-file>
</welcome-file-list>
</web-app>
YourWebApp /WEB-INF/classes
Java classes and Servlets should reside in this directory. The directory is private and
not externally accessible by end users, so they cannot request the source code for the
files.
YourWebApp /WEB-INF/lib
This is where you can place a JAR fileand tag libraries. The directory is private and not
externally accessible by end users. As above, this inherent safety mechanism stops end
users from directly accessing this content.
For instance, to deploy a WAR file in Tomcat simply place the WAR file in a /webapps
directory and restart the application server (note that in the newer version of Tomcat it is
possible to use a web-based administrative console to deploy WAR files by way of file
upload). This WAR will contain all JSPs and supporting materials like JARs, images and
JavaScript.
Understanding Classpath
Even seasoned developers wrestle with the beast called Classpath. Although this guide
is not meant to teach the basics of Java development, I think that it is nonetheless
important to take a moment to speak about the Classpath environment variable.
To check what references are currently being made in your Classpath, you can type
“echo %classpath%” in Windows or “echo $classpath” on Linux / Unix systems. You
should see something like the following if your Classpath environment variable is set.
As illustrated in the above example, a JAR file to help connect with MySQL is directly
referenced to ensure that an application can use it.
The chart below illustrates how the pieces can come together within a single JSP.
The block of code below demonstrates how a series of scriptlets can be used to display
a list of months. Notice how the scriptlet can contain HTML within a “for loop” to format
how the HTML will be sent to the browser.
<table>
<%
String months[] =
{"Jan", "Feb", "Mar", "Apr", "May", "Jun",
"July", "Aug", "Sep", "Oct", "Nov", "Dec"};
Page
The page directive allows you to include code from other packages within the JSP,
include another block of JSP code, set the current page to act as an “error” page and set
other page level settings.
Directive Description
Import The Import directive easily allows for access to other libraries
that you may be using in your project. Some common examples
might be
More information about the complete range of page directives can be found at
http://java.sun.com/products/jsp/syntax/1.2/syntaxref1210.html.
Include
The include directive allows a JSP to call another JSP within its same container. This is
very helpful to include headers, footers and other repeated elements for a web site or
web application. The syntax to include a file called header.jsp would be as follows:
Taglib
With the introduction of JSP 1.1, a new feature was added that allows custom tag
libraries to be added to JSPs. Using custom tag libraries lets you access rich
functionality that allows its complexity to be masked within a particular tag. This is a
very useful feature to assist in the separation of logic from presentation, following the
JSP best practices, and letting non-developers incorporate tags into HTML templates.
To access a tag library called “mytag” you would use the following line of code within the
JSP to make it aware of the custom tag library that was developed.
The sample below shows how the above line would be incorporated into the JSP and
then the mytag prefix used to display content in the middle of the page.
For example, instead of using the following line within a scriptlet or Servlet
out.write(“Bill Smith”);
You can use this line to directly write a string value to the page
<table>
<tr>
<td>
What time is it? <%= GetCurrentTime() %>
</td>
</tr>
</table>
Actions
Actions are XML-based tags that allow access to common pieces of JSP functionality.
useBean, setProperty, getProperty.include, forward and plugin are all accessed through
user friendly XML-based tags. Think of it as new HTML markup that allows for dynamic,
rich functionality in a page with no development by a page designer.
Action Description
setProperty This XML snippet provides a tag-based method to set the value
of a Bean’s property.
include Merges content from another JSP into the page markup.
plugin The plugin action allows HTML markup to be sent to the user’s
browser for Applets based on the platform they are using.
Chapter Summary
Java Server Pages are essentially an extension of Servlets. JSPs make it much easier
to write dynamic code that runs with an application server, also known as a container.
There are many ways to make JSPs, but it is recommend to use an IDE such as
NetBeans or Eclipse, to help learn JSP and speed development efforts. The IDEs can
also abstract the file structure for an application, but it is always good to know the inner
workings and why certain files are placed within certain spots in the directory structure of
an application deployment. JSP markup contains various elements that all serve a
different purpose. Scriptlets, directives, expressions, declarations, actions and
comments are all added to the HTML template markup to inject dynamic information into
the page that is rendered to end users.
DIGGING IN
So far, so good, right? Now that we have reviewed the underpinnings of JSP it is time to
dig into the specifics of JSP itself, and examine the various pieces that allow us to
directly develop applications to solve business problems. This chapter also begins to
present samples from an application that will be referenced throughout the remainder of
the book as a way to best practice methods for application development with JSP.
Chapter Goals
• Review what implicit objects we have access to throughout a JSP and what that
means to us
• Learn how to interact with databases
• Understand what JavaBeans are and how we can use them
• Review the benefits of tags and how they can help simplify development,
reducing the need for scriptlets in our JSPs
• Demonstrate how to send email from a JSP
• Review how file uploads can be managed in a JSP
Implicit Objects
Within each JSP page there are a series of objects that can be used to access and set
information all with some type of scope. The following table provides a practical
overview of each. It is not necessary to know every single one of these off of the top of
your head, but common objects, such as the request and session objects, will become a
big part of your life when developing JSP pages.
Object Description
application Similar to the session object, the application object allows you to
store generally small amounts of data across many page
requests. Unlike the session object the application object
values will be available to all user sessions.
// Load the driver that will be used for the database connection
Class.forName("com.mysql.jdbc.Driver").newInstance();
import java.sql
This line lets the system know that you will make references to objects pertaining to sql
without having to use a complete namespace reference.
Class.forName()
Class.forName() uses reflection to dynamically load a class on runtime and register it
with the DriverManager class. This allows you to load whatever driver will make sense
for the database that you wish to connect to. For instance, to connect with an Oracle,
DB2 or Postgre database you could use one of the following lines for your respective
database
Class.forName ("oracle.jdbc.driver.OracleDriver");
Class.forName("com.ibm.db2.jdbc.app.DB2Driver");
Class.forName("postgresql.Driver");
For our sample we will be connecting to a MySQL Database and will therefore use
com.mysql.jdbc.Driver supplied by MySQL AB called Connector J. More information
about Connector J can be found at:
http://www.mysql.com/products/connector/j/
jdbc:mysql://databasehost/database?user=username&password=passwor
d
We will now be able to use this object to communicate with the database as the following
illustrations will demonstrate.
// Populate the results object with the data from the SQL statement above
results = stmt.executeQuery("SELECT * FROM tblCustomer ORDER BY
CustomerName");
strResults += "</table>";
ResultSet results;
This instantiates a ResultSet object call that holds the response from the executeQuery
statement with the records returned.
while(results.next()) {
This line uses a method of the ResultSet object called next to iterate through the series
of results. In this example we have created a string called strResults that we will add to
in the ? does this make sense? loop. During the loop we access the results object to get
different parts of the database row. For instance results.getString(“CompanyName”) will
return the company name from the database for the current row in the loop. After the
loop, we add an additional string to the strResults variable to complete an HTML table
containing the database information.
results.close();
This line releases the connection object's database and JDBC resources immediately
instead of waiting for them to be automatically released. Closing ResultSet explicitly
gives a chance for garbage collector to recollect memory as early as possible, because
ResultSet objects may occupy lots of memory depending on the query.
stmt.close();
Close statement object as soon as you finish working with that. It explicitly gives a
chance for garbage collector to recollect memory as early as possible, which in turn
affects performance.
cnDB.close();
Close statement object as soon as you finish working with that. It explicitly gives a
chance for garbage collector to recollect memory as early as possible, which in turn
affects performance.
stmt.close();
Close statement object as soon as you finish working with that, It explicitly gives a
chance for garbage collector to recollect memory as early as possible, which in turn
affects performance.
cnDB.close();
Close statement object as soon as you finish working with that, It explicitly gives a
chance for garbage collector to recollect memory as early as possible which in turn
affects performance.
stmt.close();
Close statement object as soon as you finish working with that. It explicitly gives a
chance for garbage collector to recollect memory as early as possible, which in turn
affects performance.
cnDB.close();
Close statement object as soon as you finish working with that. It explicitly gives a
chance for garbage collector to recollect memory as early as possible, which in turn
affects performance.
stmt.close();
Close statement object as soon as you finish working with that It explicitly gives a
chance for garbage collector to recollect memory as early as possible, which in turn
affects performance.
cnDB.close();
Close statement object as soon as you finish working with that. It explicitly gives a
chance to garbage collector to recollect memory as early as possible, which in turn
affects performance.
Beans are a great way of representing an item such as a user or product. You can think
of a bean as a bucket that you can place information into, take information out of or let it
know that you need it to do some sort of operation and send you the result when it is
done. The beauty is that when you interact with this bucket, you do not need to know
anything regarding how it does its job because the inner workings are abstracted by
“Getter” and “Setter” methods that broker access to the attributes of the bean.
“Getter” and “Setter” methods are created by adding “get” or “set” to the name of an
attribute within the bean that you would like to interact with. For example, the attribute
employeeFirstName would have a setter called setEmployeeFirstName.
Using beans within JSPs can help you separate the presentation (HTML) from the
business logic. Leveraging this type of encapsulation is much better than writing Java
code directly into your JSPs. Beans will make code easier to edit and maintain. They
will also allow for reusability across any number of JSPs which will save you coding and
testing time.
As we embrace more advanced features within the Java development world, beans will
become even more important to the development. More advanced development
frameworks like Struts and Java Server Faces use beans to maintain the state between
requests, and pass data between presentation and business logic layers.
Although beans can be used directly within a JSP, they are generally sent to the JSP
from a Servlet in a MVC type of framework. This chapter will not detail with that, but the
MVC section of our sample application toward the end of the book will demonstrate this
interaction. For the purposes of this chapter, we will only cover the basics by using a
simple bean like the one outlined below.
The above bean contains a few components. The “attributes” hold the person’s first
name, last name, age and email address. It allows developers to access these
attributes and set all of them, with the exception of the person’s age, through “getters”
and “setters”. This is a nice benefit of a bean in that it can be designed to allow updates
to only certain portions of the bean’s contents. In addition to these attributes and
“getters” and “setters”, the bean also provides two methods, one to calculate pay for the
employee and the other to give the employee a pay raise. To put the above example
into the context of a Java class you can review the code for this bean below.
SamplePersonBean.java
package JSPBeyond;
public SamplePersonBean() {
Attributes of a Bean
When accessing beans through a JSP, you need to create an instance of them by
setting a series of attributes. The following block of code provides an example of a bean
that is called “calendar” and will only live for the current request. It is created from the
com.mycompany.Calendar class.
id
The name that we will use to refer to the bean after it is created.
class
The class contains the fully qualified name of the class for the bean. This is the fully
qualified name for the class such as “com.mycompany.Calendar”.
type
This defines the object type that the bean will return (String, Int or another type or
object). Our example above does not return a type.
scope
Each bean lives for a certain amount of time. The value for the scope can reference the
page, request, session or application types.
Accessing a Bean
Beans can be accessed in a variety of ways. Once again, the beauty of using a bean is
that an HTML developer can actually call a bean into their JSP page without having to
know how to code Java, and yet can still access the rich functionality provided by the
JSP. To provide this level of abstraction, JavaBeans have three ways in which you can
interact with them through JSP which are outlined below through the use of XML tags.
1. In order to use a JavaBean it must be instantiated on the JSP page. This is done
using the following code block:
Using this line of code, the container is smart enough to see if the object already exists
and if not creates a new one with the attributes provided.
2. Setting a value for a bean is done through the setProperty action. Whatever code
exists within the method for this attribute will execute upon this set.
3. Getting content from a bean. Whatever code exists within the method for this attribute
will execute upon this set.
Beans can also be accessed through scriptlets, just as though you were directly using a
method from a class. It is not a good practice to return HTML markup from a bean. If
development of tabled structures for lists or other presentation of collections is needed, it
is best practice to return a collection or record set from the bean and deal with displaying
the contents through the use of additional scriptlet markup or JSTL. The example below
outlines one such approach.
<%
// Popultate an array with the values from our bean for the customer
listing
java.util.ArrayList alCustomers =
JSPBeyond.CustomerBean.GetCustomerRecords();
// Set the "CustomerListing" attribute so it can hold the array for access
later within this page
request.setAttribute("CustomerListing", alCustomers);
%>
<html>
<head>
<title>JSP and Beyond - Customer List - Bean Example</title>
</head>
<body>
<%-- Start a loop through the results from the SQL query and mix it
with HTML --%>
<c:forEach var="row" items="${CustomerListing}">
<tr>
<%-- Notice how with each "row" object we can access the
column name by specifying it after the "row" object --%>
<td><a href="CustomerList-Bean.jsp?shownotes=<c:out
value="${row.customerID}"/>">show notes</a> <c:out
value="${row.customerName}"/></td>
<td><a href="CustomerEdit-Bean.jsp?CustomerID=<c:out
value="${row.customerID}"/>">edit</a></td>
<td><a href="NoteAdd-Bean.jsp?CustomerID=<c:out
value="${row.customerID}"/>">add note</a></td>
<td><a href="CustomerDelete-Bean.jsp?CustomerID=<c:out
value="${row.customerID}"/>">delete</a></td>
</tr>
</c:forEach>
</table>
</body>
</html>
Please note that the following code would generally exist in a Servlet and then be sent to
the JSP if you were using an MVC framework, by using the request.setAttribute code as
shown below.
// Popultate an array with the values from our bean for the customer
listing
java.util.ArrayList alCustomers =
JSPBeyond.CustomerBean.GetCustomerRecords();
// Set the "CustomerListing" attribute so it can hold the array for access
later within this page
request.setAttribute("CustomerListing", alCustomers);
The code would remain the same as the one in our scriptlet. This approach is detailed
toward the end of the book.
As stated in the prior segment, beans are just regular Java classes with a bit of special
structure provided by “getters” and ”setters”. This means that by using the Java
Compiler (javac.exe), you can build it from the command line as in the example below,
but the IDE just does it so much better. The IDE below (NetBeans) does an excellent
job of helping you quickly build a project.
Once the bean has been compiled, there are two options as to how to incorporate it into
your project. You can include your newly compiled class file into a JAR and place it in
the lib folder within your application server or place the class file under the WEB-INF
folder.
Depending on what you have named your package, in the above example we used
JSPBeyond, you need to move the class file under WEB-INF in accordance with the
package name. For our sample we would move it into classes -> JSPBeyond under
WEB-INF.
The diagram below highlights the folder structure containing application elements.
After moving the files onto the application server, a restart is generally required in order
to recognize the new files.
That being said, it is important not to go overboard with this technology when developing
applications. JSTL is very powerful and can even connect to databases using certain
tags, but that is not always the best way to include that type of functionality. To get the
most out of JSP, you should keep the majority of logic- and settings-related-markup out
of your JSP pages and in reusable components (i.e. beans or other classes) whenever
possible; and then use JSTL and EL to focus on displaying the contents of those objects
and not retrieving or manipulating the data.
This chapter will focus on the Core library, since once you understand how the libraries
can be used it is relatively trivial to implement the one that will meet your needs.
Accessing JSTL
In order to use JSTL you need to make sure to reference the specific tag set within JSTL
that you would like to use in the body of the JSP.
As “core” would indicate, the core library contains some of the most used tags from the
JSTL. Below is a listing of the core types along with samples.
Expression
JSTL has the ability to display object attribute and variable information very easily. The
example below renders the username from the bean called “customer” using the out tag.
Iteration
One of the biggest challenges that I have encountered with iteration is the understanding
of exactly what objects can be used to iterate through. java.util.Collection, java.util.Map,
java.util.Iterator, java.util.Enumeration, an array of object instances and
javax.Servlet.jsp.jstl.sql.Result can all be used as the object that an iteration can be
based on. In the code sample below, assume that you have an array called
“customerList” containing a list of beans representing customers.
<table>
With the above example, you are able to merge your dynamic elements with the HTML
template. This allows maximum control over the look and feel of the page that is
displayed to users and lets developers and designers focus on their respective
disciplines.
Conditionals
JSTL provides an elegant way to conditionally display content. The following block uses
an “if” conditional.
<c:if test="${user.loggedin}">
<tr>
<td>
<c:out value="${user.password}"/>
</td>
</tr>
</c:if>
The code block below uses a “choose” conditional. The choose conditional is best
applied when various matches are possible.
<c:choose>
<c:when test="${user.team == 'Management'}">
Project is on time!
</c:when>
<c:when test="${user.team == 'Developer'}">
Great job coding!
</c:when>
<c:otherwise>
Not sure why you are here!
</c:otherwise>
</c:choose>
Beyond the examples above, you will see that during form validation, JSTL conditionals
can be very helpful.
URL Management
JSTL provides some innovative ways to interact with page flow and incorporating
content from other JSPs. This is done through three tags: “import”, “redirect” and “url”.
For each one of the tags, it is also possible to specify parameters that are passed along
with the request.
<!-- This example gets content from welcomeheader.jsp and injects it into
the page -->
<c:import url="welcomeheader.jsp" />
<!-- This example requests a URL with the parameter “myparam” set to 1
-->
<c:import url=”mypagewithparameter.jsp”>
<c:param name=”myparam” value=”1” />
</c:import>
The redirect tag works much the same way as the import tag, allowing you to pass along
arguments with the request in the URL.
<c:redirect url="accountView.jsp">
<c:param name="id" value="${id}"/>
</c:redirect>
When you need to ensure that a URL is properly formatted, the “url” tag does a nice job
of handling things such as automatic encoding.
<c:url value="listproducts.jsp">
<c:param name="category" value="outdoor camping gear"/>
</c:url>
<c:catch var="testexception">
<!-- Do something REALLY bad here to cause an error -->
</c:catch>
<c:out value="${ testexception.message}"/>
Looking at many of the examples in the prior section, you can see that there were
instances where we accessed data in a format like ${somedata.here}. This is EL. The
close relationship between JSTL and EL is why many people lump them into a single
category, although they are ultimately different technologies.
That being said, some of the best, most common uses, would be leveraging EL Implicit
Objects directly in the JSP. For instance, the following section of EL retrieves the query
string variable called username.
Beyond accessing information from objects, EL also has the ability to leverage
operators. This allows blocks of code to execute conditionally when combined with
JSTL as illustrated below.
Operators like ==, !=, <, >, >= or <= can all be used to make decisions with EL. For
example, using the above operators lets you perform conditional evaluations directly in
your JSPs
CustomerList-Scriptlet.jsp
<html>
<head>
<title>JSP and Beyond - Customer List - Scriptlet Example</title>
</head>
<body>
<%
Connection conn = null;
while(rs.next())
{
out.println("<tr><td>");
out.println("</tr>");
while(rsDetails.next())
{
rsDetails.close();
stmtDetails.close();
}
}
}
<br>
<a href="CustomerAdd-Scriptlet.jsp">Add a New Customer</a> | <a
href="CustomerList-Scriptlet.jsp?shownotes=all">Show all Notes</a>
</body>
</html>
CustomerList-JSTL.jsp
<%-- Let the system know that we are using JSTL and load the proper
prefixs --%>
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core_rt" %>
<%@ taglib prefix="sql" uri="http://java.sun.com/jstl/sql_rt" %>
<html>
<head>
<title>JSP and Beyond - Customer List - JSTL Example</title>
</head>
<body>
<%-- Let the system know what datasource we will speak with --%>
<sql:setDataSource var="dataSource"
url="jdbc:mysql://localhost/YourDatabaseName"
driver="com.mysql.jdbc.Driver" user="DatabaseUser"
password="Password" />
<%-- Start a loop through the results from the SQL query and mix it
with HTML --%>
<c:forEach var="row" items="${queryResults.rows}">
<tr>
<%-- Notice how with each "row" object we can access the
column name by specifying it after the "row" object --%>
<td><a href="CustomerList-JSTL.jsp?shownotes=<c:out
value="${row.CustomerID}"/>">show notes</a> <c:out
value="${row.CustomerName}"/></td>
<td><a href="CustomerEdit-JSTL.jsp?CustomerID=<c:out
value="${row.CustomerID}"/>">edit</a></td>
<td><a href="NoteAdd-JSTL.jsp?CustomerID=<c:out
value="${row.CustomerID}"/>">add note</a></td>
<td><a href="CustomerDelete-JSTL.jsp?CustomerID=<c:out
value="${row.CustomerID}"/>">delete</a></td>
</tr>
<c:if test="${(row.CustomerID) == (param.shownotes) or
(param.shownotes) == 0}">
<sql:query var="queryDetailResults"
dataSource="${dataSource}">
<%-- Place our SQL query here within the XML tag
"sql:query" --%>
SELECT * FROM tblNote WHERE CustomerID = ?
<sql:param value="${row.CustomerID}" />
</sql:query>
<c:forEach var="rowDetail"
items="${queryDetailResults.rows}">
<tr><td colspan="4"><c:out value="${rowDetail.Note}"/>
- <a href="NoteDelete-JSTL.jsp?NoteID=<c:out
value="${rowDetail.NoteID}"/>">delete</a></td></tr>
</c:forEach>
</c:if>
</c:forEach>
</table>
<br>
<a href="CustomerAdd-JSTL.jsp">Add a New Customer</a> | <a
href="CustomerList-JSTL.jsp?shownotes=0">Show all Notes</a>
</body>
</html>
Not only is the above code easier to read, the code block is also much shorter. It is easy
to see how JSTL can make code much easier to read and maintain.
So far we have examined an inline Java code <% some Java code here %>, JavaBeans
and JSTL as various ways to place dynamic content into the presentation layer. Inline
Java code is messy and breaks JSPs best practices, and JavaBeans are not meant to
return HTML markup to a JSP, but rather to provide objects from which data can be
stored and retrieved.
Custom tags give you the best of both worlds and then some. In this examination we will
cover the JSP 2.0 tags which, without diving into details, are much easier to develop and
use than the first series of tags that JSP introduced.
Custom Tags allow you to place XML-style markup within your HTML which provides
rich functionality, completely separating complex code from your JSP presentation layer.
Custom Tags let developers develop and designers design, strictly isolating both job
functions.
Custom Tags let tag developers access intrinsic objects related to the JSP, unlike a
JavaBean where these objects are not easily accessible.
1. Create a TLD file that tells the application server what your tags do. An example of
this is the description, price and comments for a product that you might purchase on
Amazon. It lets the system know what your tag’s name is, what class contains the code
for it and what inputs it might take.
2. Create a class that implements the javax.Servlet.jsp.tagext.SimpleTagSupport
interface. This allows the class to do all of the heavy lifting behind the scenes in order to
produce HTML output to send back into your page template.
3. Place the tag markup inside the JSP markup along with a reference to the TLD in the
page of your choice, and let it work its magic!
Custom tags are so powerful that it is worthwhile to spend some additional time walking
through an example of building a trivial one. Let’s walk through an example of creating a
“Hello World” custom tag.
Keeping in the spirit of our pragmatic approach to this development, let’s use NetBeans
to make the custom tag. It is an excellent way for you to see what needs to happen to
properly build the tag with minimal effort. Once you have built the tag, we can review the
files that the IDE created in order for you to gain a deeper understanding of the code.
1. Launch NetBeans and Open an existing Web Project (you can use the
JSPandBeyond project)
2. Go to “File” > “New File…”
3. Select “Tag Library Descriptor” and the click “Next >”
4. Fill out the “TLD Name” field. Our example uses “jspandbeyondtags”. Then select a
folder to place the new TLD in. “WEB-INF/tlds” is the default folder, and for
consistency you should place it there. Click “Finish” to complete the wizard and have
the TLD created.
The new TLD should look like the image below. You can see how much work the IDE
was able to do for us, helping to minimize errors and save time.
Now that you have the basic skeleton of the TLD file, let’s create the tag file that will be
added to the TLD to be used within your JSPs.
1. Go to “File” > “New File…”
2. Select “Web” as the Category and “Tag Handler” as the “File Types” as shown
below, then click “Next >”. The “Tag Handler” will represent a tag in your application
and is a java class that implements the SimpleTagSupport interface.
3. Fill in the “Class Name” field. In our example we are using “customtagtest”. Then
select the Package that the new class will go into. For our example we are selecting
“JSPBeyond”. The click “Next >”. Packages are a helpful way to bundle related
class files.
4. The next screen “TLD Information” does a very nice job of setting the contents in the
TLD file automatically for the new tag. Notice the checkbox at the top of the wizard
that says “Add Corresponding Tag to the Tag Library Descriptor”. This will
automatically append / adjust the file with the information about our new tag. We will
also need to browse through the TLD that we want to add the information into. In
this case we are going to select “jspandbeyondtags.tld”.
5. We will also add an “attribute” by clicking “New…” in the attributes section, and then
create an attribute Name called “username” as a String.
6. Click “Finish” to save your new custom tag. Notice how when the wizard closes, you
now have a new class within the JSPBeyond package called “customtagtest”.
Now that the wizard is completed, you have a Java file that can be used to make your
tag function. The bolded area below is where you can write all of the code to send
HTML into your JSP where the tag has been placed. Also notice that the line containing
“private java.lang.String username;” references the attribute that we added.
package JSPBeyond;
import javax.Servlet.jsp.tagext.*;
import javax.Servlet.jsp.JspWriter;
import javax.Servlet.jsp.JspException;
/**
* Initialization of username property.
*/
private java.lang.String username;
*/
public void doTag() throws JspException {
JspWriter out=getJspContext().getOut();
try {
// TODO: insert code to write html before writing the body
content.
// e.g.:
//
// out.println("<strong>" + attribute_1 + "</strong>");
// out.println(" <blockquote>");
JspFragment f=getJspBody();
if (f != null) f.invoke(out);
// TODO: insert code to write html after writing the body content.
// e.g.:
//
// out.println(" </blockquote>");
/**
* Setter for the username attribute.
*/
public void setUsername(java.lang.String value) {
this.username = value;
}
}
Now that you have your custom tag class and TLD file, let’s create a JSP that leverages
it. This is the Directive that needs to be added to the JSP to let it know that you have
custom tags that it can use
Notice the prefix “customtagtest” that indicates that this will be the name of the tag that
will be used to implement your custom functionality. For our sample we have changed
In the above code sample we have added a line of code that references our TLD file,
and NetBeans is able to recognize our custom tag. By recognizing the tag, NetBeans
lets you know what attributes are part of your custom tag and what information you can
send into those attributes. In our sample we are able to set an attribute for “username”
with the following code:
<customtagtest:customtagtest
username="Bob"></customtagtest:customtagtest>
This code within the JSP will result in the following output when our JSP is requested in
a browser.
Here are the underlying files that were created in the process that made it possible to
use our new custom tag. Notice that there is now a tag name called “customtagtest” that
has been added.
jspandbeyondtags.tld
</tag>
</taglib>
TagSample.jsp
<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>
<%@ taglib prefix="customtagtest" uri="/WEB-
INF/tlds/jspandbeyondtags" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-
8">
<title>JSP Page</title>
</head>
<body>
<h1>JSP Page</h1>
<customtagtest:customtagtest
username="Bob"></customtagtest:customtagtest>
</body>
</html>
customtagtest.java
package JSPBeyond;
import javax.Servlet.jsp.tagext.*;
import javax.Servlet.jsp.JspWriter;
import javax.Servlet.jsp.JspException;
/**
*
* @author jbrunswi
* @version
*/
/**
* Initialization of username property.
*/
private java.lang.String username;
JspWriter out=getJspContext().getOut();
try {
/**
* Setter for the username attribute.
*/
public void setUsername(java.lang.String value) {
this.username = value;
}
}
Although there is some initial setup required, once you have the basics for tag creation
established it is easy to quickly add additional functionality.
Sending Email
Sending email from your JSPs is easy, but requires some configuration. In order to send
email from your application you will unfortunately need to add some components to your
application. The Java Mail API (http://java.sun.com/products/javamail/) is the most
common way to include the toolset to send email. The Java Mail API is accessible via
the mail.jar. The latest version of the mail.jar can be downloaded from:
https://maven-repository.dev.java.net/nonav/repository/javax.mail/jars/
Once you have downloaded the jar, it can be placed within the “web-inf/lib” folder within
your application server which makes it accessible to any applications running on that
application server. The code below will then leverage that information in that jar file to
send an email.
Looking at the above code it becomes obvious that a scriptlet is probably not the best
way to handle this, since it can be error-prone and makes it difficult for a designer to use
within an HTML page. This is where using a Customer Tag or JavaBean would be very
effective, and using the mail.jar we can easily create one.
File Uploads
Similar to the email functionality covered in the previous section, you will need to go
some extra distance to enable file uploads from within your JSP-based application. This
is important because most web-based applications will need to accept files at some
point, whether a document or image. When a web page is submitted that contains a file
to be uploaded, the data is sent as a HTTP POST encoded using the MIME-type
multipart/form-data. You will have to manually handle the stream to combine it back into
the original file within your code where it can then be saved on your server.
Thankfully, people have realized this and created pre-packaged beans and classes to
handle file uploads. Some are commercial, but fortunately the Apache Foundation has
developed a library to help manage uploads that is freely available at:
http://jakarta.apache.org/commons/fileupload/
The code sample below highlights the code from Apache foundation.
When we are taking a basic approach to form handling we can use the following
methods from the request object to grab each element from the form that was submitted.
getParameter
getParameter allows you to directly access a parameter from the request if you know the
name of the parameter. An example that would retrieve the value for a form variable
called “username” would be written as follows:
getParameterNames
getParameterNames returns a collection of each parameter name as a string. This is a
good tool to use when attempting to debug a form when you are not entirely sure what is
being sent to the target page. The following code sample will return a listing of all of the
different form items with the parameter names (key) and values (value).
The following modified sample from the reference application uses a very basic,
manually handled request process to complete the action of adding a customer account.
The form is just simple HTML and contains form data that will be posted to
“CustomerAddCommit-Scriptlet.jsp”.
<html>
<head>
<title>JSP and Beyond - Add Customer - Scriptlet Example</title>
</head>
<body>
The following code block will then take all of the form data (post data) from the example
above and store it in your database.
CustomerAddCommit-Scriptlet.jsp
<html>
<head>
<title>JSP and Beyond - Add Customer - Scriptlet Example</title>
</head>
<body>
<%
if (request.getParameter("CustomerName") != null)
{
try {
Connection conn = null;
Class.forName("com.mysql.jdbc.Driver").newInstance();
conn =
DriverManager.getConnection("jdbc:mysql:///DatabaseName","DatabaseUs
er", "Password");
Statement stmt = conn.createStatement();
String sCustomerName =
request.getParameter("CustomerName").toString();
</body>
</html>
Following is the complete example from the reference application where all of the code
that allows a customer to be added is included. This is a slightly different approach that
helps you reduce the number of JSPs required to create an application. You can see
that the form posts to request.getRequestURI(), which is itself! Within the page code,
the JSP will then decide whether it must display a data entry screen (if
(request.getParameter("action") == null)) or if it will store the data from the form post
back to itself (else if (request.getParameter("action").equals("add"))).
CustomerAdd-Scriptlet.jsp
<html>
<head>
<title>JSP and Beyond - Add Customer - JSTL Example</title>
</head>
<body>
One of the nice things about JavaBeans is the ability to leverage them for data storage
from forms. The examples below highlight using a bean.to collect the data from a form
post. This is done by using the following block of code in the FormToStore.jsp shown
after the FormToBean.jsp below. It is interesting to note that in this sample code setting,
the “property” attribute of the bean to a “*”, the bean will automatically map the input of
form fields to attributes with the same names within the bean. It is a huge time saver.
FormToBean.jsp
<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>
<html>
<head>
<title>Account Signup</title>
</head>
<body>
</body>
</html>
FormToStore.jsp
<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>
<html>
<head>
<title>Account Signup</title>
</head>
<body>
<h1>Thanks!</h1>
</body>
</html>
Toward the end of this book I will review a very basic MVC framework that makes
dealing with forms a bit easier, since you are able to handle the processing in a Java
class.
JavaScript
JavaScript is a powerful client side tool that can help to validate forms by understanding
each part of a form and comparing the values submitted with those that you would like to
require. The only issue with JavaScript form validation is that you need to rely on the
client side to process the form correctly. Some users disable JavaScript and malicious
users can circumvent JavaScript-based form validation. This means that although it is
an option (and perhaps the most common option to implement for web applications), it is
not the most secure method of validation.
The sample below illustrates a form that validates a user’s email address to ensure that
it conforms to a standard email address pattern.
<html>
<head>
<script type="text/javascript">
function validate_email(field,alerttxt)
{
with (field)
{
apos=value.indexOf("@")
dotpos=value.lastIndexOf(".")
if (apos<1||dotpos-apos<2)
{alert(alerttxt);return false}
else {return true}
}
}
function validate_form(thisform)
{
with (thisform)
{
if (validate_email(email,"Not a valid e-mail address!")==false)
{email.focus();return false}
}
}
</script>
</head>
<body>
<form action="submitpage.htm"
onsubmit="return validate_form(this);"
method="post">
Email: <input type="text" name="email" size="30">
<input type="submit" value="Submit">
</form>
</body>
</html>
For complete details on a range of JavaScript validation more information can be found
at:
http://www.w3schools.com/js/js_form_validation.asp
<c:choose>
<c:when test='${not empty param.yourName}'>
Thanks <c:out value='${param.yourName}'/>!<br>
</c:when>
<c:otherwise>
*Please submit a value for your name<br>
</c:otherwise>
</c:choose>
Advanced Validation
With all of this being said more intricate frameworks like Struts and JSF provide
additional tools to help with form validation as it is a staple of application development.
These are beyond the scope of this primer, but we encourage you to dig in and check
them out as they are becoming the preferred method for medium to large scale
application development. We provide an overview of these frameworks in our Next
Steps section.
This subsequent page needs to have the Page directive “isErrorPage” set to true to
receive the exception. It can then access all of the attributes of the exception, which is
useful for sending in an email or display in a user friendly way.
ThrowError.jsp
CatchError.jsp
Notice that although we specified the CatchError.jsp to show the error, the end user will
never see CatchError.jsp in the browser window. The error and output are all done
under the covers and are not obvious to the user.
In reality you would probably want to do something more useful with the error, such as
storing the error output in a utility bean that sends an email to a system administrator.
As you can see, it is fairly straightforward to implement some gentle exception handling
within JSP rather than showing an end user a very large, unfriendly stack trace.
Chapter Summary
This chapter has covered a tremendous amount of material about the specifics of
elements that you can place into JSPs. Samples and explanations were provided for
various technologies like JSTL, and custom tags to abstract any serious business logic
or database interactions from our JSPs. You also learned that within your code it is
possible to leverage many implicit objects which allow the retrieval of data from forms,
the request object, user information from the session object and more. The basics of
JDBC were also introduced, providing you with an easy way to connect to and
manipulate databases. Common application functions like sending email and receiving
a file and upload were also covered. For these special items you were made aware that
special libraries need to be added to your projects to provide that functionality. Last, but
in no way least, you also learned about gracefully handling errors within your JSPs.
Given the amount of material in this chapter, I suggest reviewing any of the topics that
might still present some confusion, as these elements comprise the core of JSP.
REFERENCE APPLICATION
The following sections will highlight a lot of the material that was covered throughout the
book, and pull it together in a way that makes sense. The samples leverage a collection
of files separated into a scriptlet sample, JSTL sample and MVC sample. Each one of
these is meant to highlight how the same end user experience can be created with many
different approaches, as they all ultimately create the same web application experience
for end users.
My hope is that from the code below you will have some good template materials to get
started on developing your own web applications with the method that makes the most
sense for your needs.
You can download the complete code set for the examples at:
http://www.jspandbeyond.com/sourcecode
Chapter Goals
• Understand how scriptlets, JSTL and MVC development approaches can all be
leveraged to solve the same business issue and provide the same end user
experience
• Review JSTL samples
• Review scriptlet sample
• Learn about very basic MVC through a sample
The Database
For the reference application database we are using MySQL. You can download a copy
at http://dev.mysql.com/downloads/. For the purpose of the sample application, I have
kept the database very straightforward, only using two tables – one for customer
information and the other to hold notes that you will want to append to the customer
accounts. The following code will reproduce the database within MySQL.
/*!40101 SET
@OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET
@OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET
@OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;
--
-- Create schema DatabaseName
--
--
-- Table structure for table `DatabaseName`.`tblcustomer`
--
--
-- Dumping data for table `DatabaseName`.`tblcustomer`
--
--
-- Table structure for table `DatabaseName`.`tblnote`
--
--
-- Dumping data for table `DatabaseName`.`tblnote`
--
Scriptlet Sample
Scriptlets embed Java code directly within the page markup. Although unquestionably
useful and powerful, this model makes management of the presentation layer difficult
since the program logic becomes interspersed throughout the presentation code.
Customer Listing
In the customer listing below, you can see that database connectivity code in addition to
some HTML markup that is programmatically created, exist within this JSP. This makes
it a bit difficult to read or adjust if you wanted to look directly at the user interface portion
of what was going to be ultimately produced.
CustomerList-Scriptlet.jsp
<html>
<head>
<title>JSP and Beyond - Customer List - Scriptlet Example</title>
</head>
<body>
<%
Connection conn = null;
while(rs.next())
{
out.println("<tr><td>");
out.println("</tr>");
while(rsDetails.next())
{
// Render the customer type
out.println("<tr><td colspan=\"4\">" +
rsDetails.getString("Note") + " - <a href=\"NoteDelete-
Scriptlet.jsp?NoteID=" + rsDetails.getString("NoteID") +
"\">delete</a></td></tr>");
}
rsDetails.close();
stmtDetails.close();
}
}
}
<br>
<a href="CustomerAdd-Scriptlet.jsp">Add a New Customer</a>
</body>
</html>
Deleting a Customer
The following code uses inline Java to connect with the database and delete a customer
account. You can see that the majority of the code is spent dealing with JDBC calls.
CustomerDelete-Scriptlet.jsp
<html>
<head>
<title>JSP and Beyond - Delete Customer - Scriptlet Example</title>
</head>
<body>
<%
if (request.getParameter("CustomerID") != null)
{
try {
Connection conn = null;
String sCustomerID =
request.getParameter("CustomerID").toString();
{
out.println("An Error has Occured: " + ex.toString());
}
%>
<h1>Delete Customer - Customer Deleted</h1>
<br>
<a href="CustomerList-Scriptlet.jsp">Main Menu</a>
<%
}
%>
</body>
</html>
JSTL Sample
In comparison to scriptlets, JSTL provides a more elegant approach to your application.
You should immediately notice that the pages are cleaner and easier to read.
Customer Listing
Notice that at the very top of the JSP, we use a directive to let the container (Tomcat)
know that we are going to use JSTL. Also notice that there is plenty of Expression
Language being used within the JSTL to get values that are passed into actions or
displayed on the page.
CustomerList-JSTL.jsp
<%-- Let the system know that we are using JSTL and load the proper
prefixs --%>
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core_rt" %>
<%@ taglib prefix="sql" uri="http://java.sun.com/jstl/sql_rt" %>
<html>
<head>
<title>JSP and Beyond - Customer List - JSTL Example</title>
</head>
<body>
<%-- Let the system know what datasource we will speak with --%>
<sql:setDataSource var="dataSource"
url="jdbc:mysql://YourDatabaseServer/YourDatabaseName"
driver="com.mysql.jdbc.Driver" user="DatabaseUser"
password="Password" />
<%-- Start a loop through the results from the SQL query and mix it
with HTML --%>
<c:forEach var="row" items="${queryResults.rows}">
<tr>
<%-- Notice how with each "row" object we can access the
column name by specifying it after the "row" object --%>
<td><a href="CustomerList-JSTL.jsp?shownotes=<c:out
value="${row.CustomerID}"/>">show notes</a> <c:out
value="${row.CustomerName}"/></td>
<td><a href="CustomerEdit-JSTL.jsp?CustomerID=<c:out
value="${row.CustomerID}"/>">edit</a></td>
<td><a href="NoteAdd-JSTL.jsp?CustomerID=<c:out
value="${row.CustomerID}"/>">add note</a></td>
<td><a href="CustomerDelete-JSTL.jsp?CustomerID=<c:out
value="${row.CustomerID}"/>">delete</a></td>
</tr>
<c:if test="${(row.CustomerID) == (param.shownotes) or
(param.shownotes) == 0}">
<sql:query var="queryDetailResults"
dataSource="${dataSource}">
<%-- Place our SQL query here within the XML tag
"sql:query" --%>
SELECT * FROM tblNote WHERE CustomerID = ?
<sql:param value="${row.CustomerID}" />
</sql:query>
<c:forEach var="rowDetail"
items="${queryDetailResults.rows}">
<br>
<a href="CustomerAdd-JSTL.jsp">Add a New Customer</a>
</body>
</html>
Adding a Customer
Notice that we are using a single page to add a customer. The single page is able to
handle this process because EL allows us to check the parameters that are being sent
into the page <c:if test="${(param.action) == null}">.
CustomerAdd-JSTL.jsp
<%-- Let the system know that we are using JSTL and load the proper
prefixs --%>
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core_rt" %>
<%@ taglib prefix="sql" uri="http://java.sun.com/jstl/sql_rt" %>
<html>
<head>
<title>JSP and Beyond - Add Customer - JSTL Example</title>
</head>
<body>
<%-- Let the system know what datasource we will speak with --%>
<sql:setDataSource var="dataSource"
url="jdbc:mysql://YourDatabaseServer/YourDatabaseName"
driver="com.mysql.jdbc.Driver" user="DatabaseUser"
password="Password" />
</c:if>
</body>
</html>
When people want to travel from one place to another, they need to see the conductor
with their ticket and baggage. The ticket informs the conductor where they are headed,
and perhaps it is to the city of Paris or London. After checking to make sure that the
person is okay to travel to the requested destination, the conductor helps to load their
baggage to ensure that it is sent with them. Once the person has arrived, the conductor
assists them off the train where they are surrounded by new sights.
The “C” or controller in MVC is the conductor. The controller, which is generally a
Servlet, intercepts all requests that an end user makes and captures the data that is sent
with the requests. In an MVC framework, the requests are sent with an “action” which is
the equivalent of the ticket, and the baggage is the equivalent of the data that is paired
with the request. The controller also ensures that people are authorized to access a
particular action. The controller will then let any business take place as it connects with
the Model or “M” which controls database and external resource access. Once the
processing in the model is completed, the controller will route people to a particular View
or “V”. The view is generally a JSP that correlates to the action requested of the
controller.
In the example below, we take a basic approach to MVC, using a single Servlet to
capture and deal with all requests. In a more formal implementation a framework like
Jakarta Struts would probably be used to manage the application, especially as the size
of the application increases.
Controller Servlet
The controller Servlet is the heart of the MVC version of the reference application. It
takes almost all of the Java code out of the JSPs, leaving only some JSTL and EL to
assist with the presentation. You do not need to memorize the example below, but you
can quickly see that the controller cues off of the “action” that is requested of it such as::
else if ("customerlist".equals(action))
This tells the application that you would like to see the customer list. The controller will
then get an array of customer beans ready to be sent back to the JSP, where you can
render the results to the end user via JSTL and EL.
What you will also notice in the controller Servlet is that it will delegate all database work
to another class, thereby enhancing the readability of the code in your controller.
ServletExample.java
/*
* JSP and Beyond
* Servlet example for simple Model View Controller
*/
package JSPBeyond;
// Below are the default namespaces that are used for Servlets
import java.io.*;
import java.net.*;
import javax.Servlet.*;
import javax.Servlet.http.*;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
if ("customerlist".equals(action))
{
// Create arraylist to house the customer listing
java.util.ArrayList alCustomers;
try
{
alCustomers = JSPBeyond.ServletExampleData.getCustomers();
else if ("addcustomer".equals(action))
{
jspPage = "CustomerAdd.jsp";
}
else if ("deletecustomer".equals(action))
{
if (request.getParameter("CustomerID") != null)
{
try
{
// Call our data method to handle the deletion
JSPBeyond.ServletExampleData.deleteCustomer(Integer.parseInt(request.
getParameter("CustomerID").toString()));
}
catch (Exception ex)
{
// Exception handle
}
}
jspPage = "CustomerDelete.jsp";
}
else if ("savecustomer".equals(action))
{
if (request.getParameter("CustomerName") != null)
{
try {
// Call our data method to handle the deletion
JSPBeyond.ServletExampleData.addCustomer(request.getParameter("Cust
omerName"));
}
catch (Exception ex)
{
// Exception handle
}
}
jspPage = "CustomerAdd.jsp?status=complete";
}
else if ("addnote".equals(action))
{
if (request.getParameter("CustomerID") != null)
{
request.setAttribute("CustomerID",
request.getParameter("CustomerID").toString());
}
jspPage = "NoteAdd.jsp";
}
else if ("customerdetails".equals(action))
{
if (request.getParameter("CustomerID") != null)
{
try {
Integer iCustomerID =
Integer.parseInt(request.getParameter("CustomerID"));
String sCustomerName = "";
sCustomerName =
JSPBeyond.ServletExampleData.getCustomerName(iCustomerID);
}
catch (Exception ex)
{
// Handle exceptions here
}
}
jspPage = "CustomerDetails.jsp";
}
else if ("editcustomer".equals(action))
{
if (request.getParameter("CustomerID") != null)
{
try {
Integer iCustomerID =
Integer.parseInt(request.getParameter("CustomerID").toString());
String sCustomerName =
JSPBeyond.ServletExampleData.getCustomerName(iCustomerID);
else if ("saveeditcustomer".equals(action))
{
if (request.getParameter("CustomerID") != null)
{
try {
Integer iCustomerID =
Integer.parseInt(request.getParameter("CustomerID"));
String sCustomerName =
request.getParameter("CustomerName");
JSPBeyond.ServletExampleData.updateCustomer(iCustomerID,
sCustomerName);
}
catch (Exception ex)
{
else if ("savenote".equals(action))
{
if (request.getParameter("CustomerID") != null)
{
try
{
String sNote = request.getParameter("Note").toString();
Integer iCustomerID =
Integer.parseInt(request.getParameter("CustomerID").toString());
JSPBeyond.ServletExampleData.addNote(sNote, iCustomerID);
}
catch (Exception ex)
{
// Exception handle here
}
}
jspPage = "NoteAdd.jsp?status=complete";
}
ServletExampleData.java
package JSPBeyond;
import java.io.*;
import java.net.*;
import java.sql.*;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
/**
*
* @author jbrunswi
*/
public class ServletExampleData {
try
{
// Reference the JDBC driver
Class.forName("com.mysql.jdbc.Driver").newInstance();
conn =
DriverManager.getConnection("jdbc:mysql:///DatabaseName","DatabaseUs
er", "Password");
}
catch (Exception ex)
{
return null;
}
return conn;
}
{
try {
Connection conn = getConnection();
Statement stmt = conn.createStatement();
stmt.close();
conn.close();
}
catch (Exception ex)
{
}
}
stmt.close();
conn.close();
}
catch (Exception ex)
{
// Exception handling
}
}
stmt.close();
conn.close();
}
catch (Exception ex)
{
// Handle exception
}
}
stmt.close();
conn.close();
}
catch (Exception ex)
{
// Exception handling
}
}
// Create a string that has the SQL statement gets records from
'emp'
String sqlString = "SELECT * FROM tblCustomer ORDER BY
CustomerName";
stmt = conn.createStatement();
rs = stmt.executeQuery(sqlString);
try {
// Add the ResultSet value to an ArrayList. 'ename','job'
while (rs.next())
{
CustomerRecord myCustomerRecord = new CustomerRecord();
myCustomerRecord.setCustomerID(Integer.parseInt(rs.getString("Custom
erID")));
myCustomerRecord.setCustomerName(rs.getString("CustomerName"));
CustomerRecords.add(myCustomerRecord);
}
}
catch (Exception ex)
{
throw ex;
}
rs.close();
stmt.close();
conn.close();
return CustomerRecords;
}
try {
Statement stmt = conn.createStatement();
stmt = conn.createStatement();
try {
while (result.next())
{
NoteRecord myNoteRecord = new NoteRecord();
myNoteRecord.setNoteID(Integer.parseInt(result.getString("NoteID")));
myNoteRecord.setNote(result.getString("Note"));
NoteRecords.add(myNoteRecord);
}
}
catch (Exception ex)
{
throw ex;
}
result.close();
stmt.close();
conn.close();
}
catch (Exception ex)
{
// Handle the exception
}
return NoteRecords;
}
try
{
Connection conn = getConnection();
Statement stmt = conn.createStatement();
if(result.next())
{
sCustomerName = result.getString("CustomerName");
}
}
catch (Exception ex)
{
// Handle exception
}
return sCustomerName;
}
Customer Listing
In the following example you can immediately notice how clean the JSP becomes as all
data work has been abstracted away from anything in the JSP.
CustomerList.jsp
<%-- Start a loop through the contents sent by the controller Servlet
and mix it with HTML --%>
<c:forEach var="row" items="${CustomerListing}">
<tr>
<%-- Notice how with each "row" object we can access the
column name by specifying it after the "row" object --%>
<td><c:out value="${row.customerName}"/> <a
href="ServletExample?action=customerdetails&CustomerID=<c:out
value="${row.customerID}"/>">show details</a> </td>
<td><a
href="ServletExample?action=editcustomer&CustomerID=<c:out
value="${row.customerID}"/>">edit</a></td>
<td><a
href="ServletExample?action=addnote&CustomerID=<c:out
value="${row.customerID}"/>">add note</a></td>
<td><a
href="ServletExample?action=deletecustomer&CustomerID=<c:out
value="${row.customerID}"/>">delete</a></td>
</tr>
</c:forEach>
</table>
<br>
<a href="ServletExample?action=addcustomer">Add a New
Customer</a>
</body>
</html>
Adding a Customer
To add a customer you will still make use of some JSTL to leverage a single JSP. The
code is much cleaner and easy to understand.
CustomerAdd.jsp
<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core_rt" %>
<html>
<head>
<title>JSP and Beyond - Add Customer - JSTL Example</title>
</head>
<body>
</c:if>
</body>
</html>
Updating a Customer
Editing a customer is just as simple as adding the customer. When you begin editing a
customer account, the controller Servlet takes care of fetching the data that you want to
be placed into the form.
CustomerEdit.jsp
<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core_rt" %>
<html>
<head>
<title>JSP and Beyond - Customer Edit - MVC Example</title>
</head>
<body>
</body>
</html>
MAP OF TECHNOLOGY
NEXT STEPS
Hopefully at this point you should have a good handle on the various components that
have been covered in this book. The benefits of Java for the web, the history of the tools
that have been used to generate dynamic web content, the ins and outs of application
servers, how JSPs are processed, an understanding of what development tools can help
deliver solutions, interaction with databases, email and other basics of Java web
application development should now seem familiar.
From here it is possible to become acquainted with and explore additional technologies
that build on top of the foundation that has been laid. These additional technologies
continue to make Java a mainstay in enterprise application development.
The following sections were designed to help bring you up to speed on these additional
tools at a conceptual level. I hope that it acts as a launch pad for you to dive into and
leverage these additional tools in your development efforts.
Jakarta Struts
Jakarta Struts is maintained by the Apache Foundation. The Struts concept is
somewhat similar to the MVC example discussed in the previous chapter, but it
leverages an XML configuration file which maps actions to JSPs and “action” classes.
This makes it a much better choice for managing applications of any real complexity.
<ui:button action="#{MyJSFSample.btnPressMe_action}"
binding="#{MyJSFSample.btnPressMe}" id="btnPressMe" text="Press
Me!"/>
...and based on that event, a label component’s value is changed. The code below
shows a component called “lblMyLabel” that registers the change based on the way the
button press was handled in a special Java class which is detailed in the next code
sample.
In order to make the above JSP page work and the value for “lblMyLabel” change, the
following class assists that page with its processing.
/*
* MyJSFSample.java
*/
package myjsftest;
import com.sun.rave.web.ui.appbase.AbstractPageBean;
import com.sun.rave.web.ui.component.Body;
import com.sun.rave.web.ui.component.Button;
import com.sun.rave.web.ui.component.Form;
import com.sun.rave.web.ui.component.Head;
import com.sun.rave.web.ui.component.Html;
import com.sun.rave.web.ui.component.Label;
import com.sun.rave.web.ui.component.Link;
import com.sun.rave.web.ui.component.Page;
import javax.faces.FacesException;
/**
* <p>Page bean that corresponds to a similarly named JSP page. This
public MyJSFSample() {
}
The line highlighted above manages the lblMyLabel label component and updates its
value.
We are only scratching the surface of JSF here, but the above example illustrates some
very basic elements of its operation. For more details on JSF please visit:
http://java.sun.com/javaee/javaserverfaces/
Hibernate
Remember all of the lines of code that were needed to write in the Dealing with Data –
JDBC section? Hibernate creates an object layer over your database that makes all of
the connection creation, querying, adding, updating, deleting and more a breeze
because it removes the need to write any SQL code. This abstraction methodology is
called Object Relational Mapping. Using Hibernate, you are able to use plain old Java
objects (POJOs) to manipulate any information in the database as Hibernate will
automatically create the POJOs that will map to your database structure.
The example below shows a theoretical situation where we are trying to store a new
user.
With Hibernate
Without Hibernate
After reviewing the above examples, it is obvious that the Hibernate approach is much
more elegant, especially when you start to work with more complex transactions and
data structures.
Declaration.................................................48 J
Development Tools....................................34 J2EE ......................................................... 33
Eclipse ...................................................36 J2ME ......................................................... 34
NetBeans ...............................................35 J2SE ......................................................... 34
Directive.....................................................45 Jakarta Struts.......................................... 134
Include ...................................................46 JAR Files................................................... 41
Page ......................................................45 Java EE..................................................... 33
Taglib .....................................................46 Java Server Faces .................................. 134
Django .......................................................22 Java Server Pages (JSP) ......................... 19
E Java Server Pages Standard Tag Library
(JSTL) ................................................... 69
Eclipse .................... See Development Tools
Conditionals .......................................... 72
Email
Exception Handling ............................... 74
Sending..................................................92
Expression ............................................ 71
Error Handling..........................................103
Iteration ................................................. 72
Expression .................................................47
Taglib Directive ......................................71 R
URL Management..................................73
Reference Application
Variables................................................71
Database............................................. 107
JavaBean...................................................61
JSTL Sample ...................................... 113
Accessing a Bean ..................................65
Model View Controller (MVC) ............. 117
Attributes................................................64
Scriptlet Sample.................................. 109
Building and Deploying..........................67
REFERENCE APPLICATION................. 106
JBoss .........................................................31
Ruby on Rails............................................ 21
JDBC .........................................................54
Adding Data ...........................................58 S
Connecting to a Database .....................54 Scriptlet ..................................................... 43
Deleting Data .........................................59 Servlets ..................................................... 24
Retrieving Data ......................................56 Struts................................See Jakarta Struts
Updating Data........................................59
JDK ............................................................33 U
JRE ............................................................33 Uploading Files ......................................... 93
JVM............................................................34
V
M
Validating User Input .............................. 101
Macromedia JRun......................................32 JavaScript ........................................... 101
Model View Controller (MVC) ..................117 JSTL.................................................... 102
Controller Servlet .................................118
W
N
WAR Files ................................................. 40
NetBeans ................ See Development Tools Web Application Structure ........................ 38
O
Oracle Application Server ..........................32
ABOUT THE AUTHOR
John Brunswick is a Practice Manager within BEA
System’s Business Interaction Division Professional
Services Group. Managing their Enterprise Portal and
Business Process Management practice in Canada and
New England, he helps Fortune 500 companies create
solutions to a wide range of business challenges.
John actively leads the Boston Computing Review user group which he founded in 2005,
educating members about emerging technologies and industry trends. Prior to working
at BEA, John directed Jniche Technology Associates, a small web application
development and networking group. John graduated from Boston University with a
degree in Business Administration with a concentration in Management Information
Systems.