
JSP Tutorial Objectives:
• Learn the importance and reason for JSPs.
• Understand the advantages of JSPs over servlets.
• Examine the basics of a JSP file and what the container does with it.
• Explore JSP implicit objects.
• Learn JSP scripting element syntax.
• Understand why JSP comments should be used in place of standard HTML comments.
• Understand the purpose of JSP directives.
• Explore the include and page directives.
Chapter Overview
JSP is an easier way, compared to servlets, of generating dynamic content for the Web. A JSP provides a
more HTML-centric way of creating a Web application. Unlike servlet technology a JSP doesn’t require a
Web developer to put HTML code into a Java program. Furthermore, JSP writers don’t need to create or
work with XML mappings. Thus, JSPs become an easier way to create a Web page and JSPs are typically
used for the presentation layer of a Web application. While JSPs are simpler to create than servlets, they do
introduce the developer to a new syntax.
In this chapter, you learn the basic principles of JSPs and how they relate to the underlying servlet
architecture. You also become familiar with the JSP scripting elements.
Why JSPs?
Servlets had advantages over other dynamic Web technology. Servlets are compiled code so they run faster
than scripting languages. Servlets stay in memory, as opposed to loading the script for each request, again
providing faster execution. A single servlet object is used by many requests as the container threads multiple
requests through the object. This allows servlet Web sites to scale very well. Servlets run in a virtual machine
and can be added to just about any Web server running on any platform (platform independence). Because
servlets run in the virtual machine, they are more secure. A collapse of a servlet does not affect the entire
Web site. Servlets, as Java code, allow access to the massive set of Java code libraries.
But servlets also present some challenges. Do you like to code HTML inside of out.println()’s? Can you
imagine a graphic artist or HTML expert jumping to be a hardcore Java servlet expert? Working through
servlets presents difficulties in development shops where front end developers are not server-side,
middleware developers. Do you like configuring web.xml for every new servlet? Most IDE’s help with web.
xml changes, but when small quick changes need to be affected in production, this creates problems.
JavaServer Pages (JSP) is a very successful approach to making servlet development easier. JSP was extracted
from a very similar technology called JHTML that was part of ATG Dynamo. JSP is like ASP (not only in
name) because ASP proved a very easy server-side language for HTMLers to learn. JSP is like ColdFusion
because ColdFusion created extra tags that hid the server-side programming from HTML coders.
TEXT NOTE: You may note, up to this point, references to both HTML and
XHTML have been used to refer to standard Web pages. XHTML, is a markup
language that offers HTML functionality, but also conforms to XML syntax.
XHTML is particularly useful today in Web sites that offer more rich
inteactive user experiences; such as those using Ajax. Going forward, the
text will only refer to HTML for simplicity. Substitute HTML/XHTML
whenever the term HTML is used. In fact, the code examples adhere to
XHTML standards.
What is a JSP?
JavaServer Pages are an extension to the Java servlet API. To create a JSP just make an HTML file and save it
with the .jsp extension. JSPs are stored with the HTML pages of your Web site. JSPs are a mix of HTML
and Java code. They allow you to add Java processing to standard HTML Web pages. The Web container
takes a JSP and turns it into a servlet, generating and compiling the Java code for you.
What are the advantages of JSPs? JSPs allow layout (HTML) professionals to ignore the compilation, and
even the Java code. HTML developers (and Java coders) can utilize Web page development tools to build the
pages. JSP provides server-side developers with the full power of the Java programming language in HTML.
This Java is not a subset of Java or new scripting language. JSP is built directly on top of the servlet API. So
essentially, every time a JSP is run, a servlet is actually executing behind the scenes. Unlike servlets, no
configuration of JSPs is required. JSPs are not registered or mapped in the web.xml file.
JSP Tutorial
Table of Contents
Copyright (c) 2008. Intertech, Inc. All Rights Reserved. This information is to be used exclusively as an
online learning aid. Any attempts to copy, reproduce, or use for training is strictly prohibited
Courseware
Training Resources
Tutorials
Services