0% found this document useful (0 votes)
17 views25 pages

Introduction To Javaserver Pages Technology: Section 1. Tutorial Tips Should I Take This Tutorial?

Uploaded by

gjorgjiev
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views25 pages

Introduction To Javaserver Pages Technology: Section 1. Tutorial Tips Should I Take This Tutorial?

Uploaded by

gjorgjiev
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 25

Introduction to JavaServer Pages

technology

Section 1. Tutorial Tips

Should I take this tutorial?


Do I need experience?

This tutorial contains introductory information for


users with a technical background, and should take
you 1 to 1-1/2 hours to complete. It assumes
either:

* You have some experience with Java(tm) or


some other programming language.
* You work in a related field, such as technical
documentation or marketing.

We've broken the tutorial material into topics. You


can take the topics in any order. If this is your first
time through the material, we suggest you go
through them in order.

Navigation
Navigating through the tutorial is easy:

* Use the Next and Previous buttons to move


forward and backward through the tutorial.
* Use the Menu button to return to the tutorial
menu.
* If you'd like to tell us what you think, use the
Feedback button.
* If you need help with the tutorial, use the Help
button.

Introduction to JavaServer Pages technology Page 1


Presented by developerWorks, your source for great tutorials ibm.com/developer

Getting help
If you have questions as you go through this
tutorial, you can send them directly to the support
team.

Introduction to JavaServer Pages technology Page 2


Presented by developerWorks, your source for great tutorials ibm.com/developer

Section 2. Overview

Introduction to JavaServer
Pages technology
In today's environment, most Web sites want to
display dynamic content based on the user and the
session. Most content, such as images, text, and
banner ads, is most easily built with HTML editors.
So we need to mix the "static" content of HTML
files with "directives" for accessing or generating
dynamic content.

JavaServer Pages technology meets this need. It


provides server-side scripting support for
generating Web pages with combined static and
dynamic content.

Introduction to JavaServer Pages technology Page 3


Presented by developerWorks, your source for great tutorials ibm.com/developer

Introduction to JavaServer
Pages technology(continued)
In this lecture, we'll talk about key aspects of
JavaServer Pages(tm) (JSP) technology. We'll
consider several key concepts as they apply to
JSP technology:

* Servlets
* JavaBeans(tm) components
* Syntax

Let's get started!

Java and all Java-based trademarks and logos are


trademarks or registered trademarks of Sun
Microsystems, Inc. in the United States and other
countries.

(c) Copyright International Business Machines


Corporate, 2000. All rights reserved.

US Government Users Restricted rights-Use,


duplication or disclosure restricted by GSA ADP
Schedule Contract with IBM Corporate.
Legal

Introduction to JavaServer Pages technology Page 4


Presented by developerWorks, your source for great tutorials ibm.com/developer

Requests and responses


When a user keys a URL into a browser, a request
is created and sent to a server. Depending on the
request, the server's response may be in the form
of static HTML pages, stored on that server, or
dynamic content that is compiled from various
sources.

Up to now, the most common way of generating


dynamic content was through the Common
Gateway Interface, or CGI. CGI programs (typically
written in C or Perl) interact with the user by
reading the user's input, HTML forms, and
returning custom HTML pages. But, CGI has a
drawback: for each user request, the CGI script
must be loaded, run, and unloaded. This is
inefficient.

Static and dynamic responses


Another way to generate dynamic content is to use
Java servlets. Servlets are Java programs that are
loaded into an application server, such as a
WebSphere Application Server. Servlets do the
same thing as CGI scripts, but they reside in server
memory. So, servlets are more responsive to user
requests.

JSP technology provides a way to combine the


worlds of HTML and Java servlet programming.
JSP pages are text files that look very much like
HTML pages. The HTML is enhanced with new
tags that specify the programming of a servlet to
control the generation of dynamic content.

Introduction to JavaServer Pages technology Page 5


Presented by developerWorks, your source for great tutorials ibm.com/developer

Static and dynamic responses


(continued)
If the user has requested information that is
contained in static pages residing on the HTTP
server, the response will be an HTML version of
the stored page. For dynamic responses, a call will
go from the HTTP server to WebSphere
Application Server, or any other application server,
where JSP pages and servlets are managed. The
application server can be configured to preload
Java servlets to enable a quick response to even
the first user's request.

JSP pages are compiled and loaded once. If a


newer version of the JSP page is developed, the
application server will compile the newer one and
load its version of the servlet.

Why use JSP technology?


Let's talk about how JSP technology simplifies the
task of writing dynamic Web pages.

First, the Java programming language is known for


its "Write Once, Run Anywhere(tm)" characteristic.
JSP pages are entirely platform independent, both
in dynamic Web pages and underlying server
components. You can write the dynamic Web
pages on any platform, run them on any Web
server, and access them from any Web browser.

Introduction to JavaServer Pages technology Page 6


Presented by developerWorks, your source for great tutorials ibm.com/developer

Why use JSP technology?


(continued)
Second, JSP technology encourages the use of
reusable, cross-platform server components called
JavaBeans components. This saves development
time, while giving you the power and flexibility of
the Java programming language. Because
components do much of the processing work,
component development is more cleanly separated
from Web design, improving the productivity of
cross-functional teams.

Third, JSP pages are an integral part of the Java


Platform for the Enterprise, which brings Java
technology to enterprise computing. You can now
develop powerful enterprise-wide or middle-tier
server applications, using a JSP technology Web
site as a front end. When you want to update your
application, you can update the components and
dynamic Web pages that reside on the server,
updating all your users at once.

JSP page contents


Let's talk for a moment about what JSP pages
contain. We know that JSP pages are a new type
of file that combines standard HTML and new tags
for scripting in the Java language.

One of the new tags allows JSP pages to call


reusable components called JavaBeans
components.

JSP pages look like HTML. But, JSP pages are


code that gets compiled into Java servlets. The
resulting servlet generates a user page. That page
combines the HTML from the JSP page with the
embedded dynamic content specified by the new
tags. In this way, a JSP page describes a
representative HTML response. The dynamic
content will be specific to a given HTTP request.

Introduction to JavaServer Pages technology Page 7


Presented by developerWorks, your source for great tutorials ibm.com/developer

JSP technology model 1


There are two ways to invoke a JSP page.

In Model 1, a user working in a browser makes a


request that is sent to a JSP page. After receiving
the client request, the servlet that is compiled from
the JSP page requests information from a
JavaBean. The JavaBean can, in turn, request
information from an Enterprise JavaBean(tm),
database, or another back-end service. Once the
JavaBean accesses the requested content, the
JSP servlet can query and display the information
as HTML in the user response.

JSP technology model 2


Model 2 shows another way to use JSP pages, by
sending a request to a Java servlet.

In this example, the client makes a request that is


handled by a Java servlet. The servlet, like the JSP
page in Model 1, will request dynamic content from
a JavaBean or Enterprise JavaBean.

The resulting dynamic content is then wrapped in a


JavaBean. The servlet invokes the JSP servlet,
which accesses the dynamic content from the bean
and delivers the content to the client Web browser.

Introduction to JavaServer Pages technology Page 8


Presented by developerWorks, your source for great tutorials ibm.com/developer

What the models have in


common
In both models, the JSP page is identified to the
server by the .jsp extension. This tells the server
that special handling is required.

The first time a request is made for such a file,


either directly from a client browser or from a
servlet, the .jsp file is compiled into a servlet. For
that reason, there is a slight delay on the first
request for a .jsp page.

The output from the servlet is standard HTML that


the browser interprets and displays as usual.

What the models have in


common (continued)
After compilation, the servlet is stored in memory
on the server. On subsequent requests for that
page, the server checks to see if the .jsp file has
changed. If it has not changed, the server uses the
servlet stored in memory to generate the response
to the client. Because the compiled servlet is
stored in memory, the response is very fast. If the
.jsp file has changed, the server automatically
recompiles the file and replaces the servlet in
memory.

Conclusion
In this overview, we learned that JavaServer
Pages technology provides server-side scripting
support for generating Web pages with combined
static and dynamic content.

Click Menu to return to the list of tutorial topics and


select the next topic.

Introduction to JavaServer Pages technology Page 9


Presented by developerWorks, your source for great tutorials ibm.com/developer

Section 3. Servlets

What are JSP servlets?


In this topic, we'll discuss JSP servlets.

Servlets are standard, server-side Java


applications that extend the capabilities of a Web
server.

Servlets run in the context of an existing Java


environment, the Java Virtual Machine, or JVM.
Servlets are loaded into an application server, such
as a WebSphere(tm) Application Server.

What are Java servlets?


(continued)
Because a servlet is invoked by a user request,
servlets represent the system's response for a
single location in a Web site. An enterprise Web
site may have several servlets for individual
requirements, such as registration, shopping, and
purchasing.

Because servlets run inside servers, users never


actually see them and servlets do not need a
graphical user interface. Servlets are Java
components that are loaded, on demand, as the
system needs them.

Introduction to JavaServer Pages technology Page 10


Presented by developerWorks, your source for great tutorials ibm.com/developer

How servlets relate to JSP


pages
In the Overview, we discussed where servlets fit in
the JSP technology picture. Let's review that now.

You recall that JSP pages are text pages that have
all the information needed to generate servlets.

Servlets are compiled applications that respond to


the user's request with an HTML reply.

Typically, most of the JSP page looks like HTML to


be returned to the user. New tags specify the Java
code to produce dynamic content. A servlet
compiled from a JSP page generates an HTML
response like that of the JSP page with
appropriate, dynamic HTML according to the Java
code specified by the new JSP tags.

Typical uses of servlets


Use servlets to:

* Create dynamic content.


* Conduct online commerce.
* Provide thin-client connectivity to a large
enterprise application. Because servlets
reside on the server, they can act as a
middleware gateway to legacy systems.
* Provide controlled access to a number of
different multi-user applications, such as chat
servers, news and file services, discussion
databases, and search engines. User services
that do not have Web interfaces can be
bridged using servlets. The servlets can
provide protocol support to those user
services. Servlets can directly communicate
with applets on browsers.

Introduction to JavaServer Pages technology Page 11


Presented by developerWorks, your source for great tutorials ibm.com/developer

Security
There are a number of advantages for using
servlets rather than such related technologies as
CGI-based applications and proprietary Web
server extensions. Let's go over them, one by one.

The first advantage is security. Servlets are called


within the server context, so servlets have all the
security of the server itself. They're hidden from
view and transmission, giving less chance for
secure information to fall into the wrong hands.

Security (continued)
Servlets provide secure connections to the
back-tier with more control than previous
applications and may use native security features.
But the privileges of servlets can be restricted
using Access Control Lists that permit only certain
users or groups of users to have access to them.

Introduction to JavaServer Pages technology Page 12


Presented by developerWorks, your source for great tutorials ibm.com/developer

Robustness
Rich application programming interfaces (APIs) are
available to Java programs, thus making it easier
to build sophisticated applications in a short length
of time. These APIs include connectivity services
to back-end services, such as:

* Relational Databases (JDBC(tm))


* Transaction services (JTS, CICS®,
TXSeries(tm), and others)
* Enterprise JavaBeans components
* Distributed Objects (RMI, IIOP)
* Messaging Services (such as JMS and
MQSeries®)

Resulting Web-based systems are easy to


maintain and administer via remote administration
interface.

Performance
Java servlets provide better performance than
CGI-based applications. CGI-based applications
require process creation overhead for each user
request.

Servlets run in the same context as the application


server and can be preloaded or loaded on
demand. Because servlets execute and remain in
memory, they incur load overhead only once.

Additionally, servlets maintain sessions across


HTTP requests, reducing activity to back-end
systems. They're multi-threaded to scale with
multiprocessors and heterogeneous systems.

Introduction to JavaServer Pages technology Page 13


Presented by developerWorks, your source for great tutorials ibm.com/developer

Portability
Use of servlets increases portability. Servlet
technology replaces the family of server plugins
such as Netscape API or Gateway API, which are
not standardized like the JavaServer API.

Because servlets are written in the Java language,


they have the "Write Once, Run Anywhere"
capability and are platform independent. They are
able to exploit reusable Java components called
JavaBeans components.

Additionally, if the WebSphere Application Server


servlet engine plugin is used, it provides a
consistent servlet environment across different
Web servers such as Apache, IBM HTTP Server,
Domino Server, Lotus Domino Go, Netscape, and
IIS.

Conclusion
In this section, we discussed what servlets are,
typical uses of servlets, and advantages to using
servlets.

If you would like practice coding servlets, take the


Introduction to Servlets and IBM WebSphere
Application Server online tutorial.

Click Menu to return to the list of tutorial topics and


select the next topic.

Introduction to JavaServer Pages technology Page 14


Presented by developerWorks, your source for great tutorials ibm.com/developer

Section 4. JavaBeans components

Introduction
One of the most powerful features of JSP
technology is the ability to access reusable
components, such as JavaBeans components,
from within a JSP page.

JavaBeans components, portable,


platform-independent components written in the
Java programming language, were developed as a
collaborative effort among industry leaders.
JavaBeans components display the platform
independence found throughout Java programs.
Developers write reusable components once and
run them anywhere.

Introduction (continued)
The reason to use JavaBeans components with
JSP pages is to separate code from layout. We've
discussed that concept already in the Overview.
You minimize the code on the JSP page by using
JavaBeans components.

Tags for content access and presentation reside in


JSP pages. Logic and programming code for
content generation reside in reusable components,
or JavaBeans components.

Introduction to JavaServer Pages technology Page 15


Presented by developerWorks, your source for great tutorials ibm.com/developer

Characteristics of JavaBeans
components
A JavaBean component is a reusable software
component that uses a standard "method" naming
convention. This gives JavaBean components
predictable behavior and makes tool-based
manipulation easy.

The public features of JavaBeans components are


properties, methods, and events.

Characteristics of JavaBeans
components (continued)
Properties are the parts of a JavaBean. They might
be other Java objects, primitives. As we will see in
the next panel, properties of JavaBeans
components are defined by pairs of public getter
and setter methods.

A method is an action or service that the JavaBean


can perform.

An event is a notification by the JavaBean that


something of interest has happened.

In the next panels, we'll give more details on bean


functionality.

Introduction to JavaServer Pages technology Page 16


Presented by developerWorks, your source for great tutorials ibm.com/developer

public Date getTime()


public void setTime(Date Beans in the JSP environment
newTime)
One use of JavaBeans components is as a data
encapsulator. A property of a JavaBean is simply
the data (state) of the bean. Properties are
accessible by two methods: the getter and the
setter. The value of the property is accessed by the
getter method. If the property is writable, its value
is changed by the setter method.

Look at the example shown on the left. The


method pair defines a bean property named time
whose access type is a Date (java.util.Date).

The properties and methods of a JavaBean are an


API for exchanging data and providing services.
Access to a bean only requires sending messages
to the bean instance. Most enterprise tools, such
as VisualAge® for Java, generate Java classes
that conform to JavaBean specifications.

Using JavaBeans components


in JSP pages
Let's talk about some of the advantages of using
JavaBeans components with JSP pages.

When used with servlets and JSP pages in an


application server, JavaBeans components
represent the business logic for an application.
They provide:

* Properties that can be set to configure their


behavior
* Methods exposing the services of the
business logic
* Properties that encapsulate the generated
dynamic content

Introduction to JavaServer Pages technology Page 17


Presented by developerWorks, your source for great tutorials ibm.com/developer

Using JavaBeans components


in JSP pages (continued)
When a user sends a request by way of the
browser, it may be for static or dynamic
information. If dynamic, the request is handled by a
servlet in these steps:

1. The servlet locates one or more JavaBeans


component needed to perform the request.
2. It configures the beans using request
parameters and invokes the required business
logic methods.
3. The servlet then stores a reference to the
result bean so it is available to a JSP page.
4. The JSP page uses the dynamic content
available in the result bean to deliver a
response back to the browser.

How JavaBeans components


work
We said that servlets and back-end applications,
such as databases, supply dynamic content in a
result bean.

The JSP page accesses the result bean object via


a tag. The tag specifies how to find or create a
bean.

* If an existing bean is to be accessed, it is


either retrieved from the session or the
request context.
* If a bean needs to be created, it is instantiated
from a serialized bean, or a new bean is
constructed.

We give more details on syntax later in this tutorial.

Introduction to JavaServer Pages technology Page 18


Presented by developerWorks, your source for great tutorials ibm.com/developer

Conclusion
In this section, we discussed what JavaBeans
components are, JavaBean properties, and the
advantages of using JavaBeans components with
JSP pages.

Click Menu to return to the list of tutorial topics and


select the next topic.

Introduction to JavaServer Pages technology Page 19


Presented by developerWorks, your source for great tutorials ibm.com/developer

Section 5. Syntax

JSP tag syntax


JSP pages mix HTML and JSP-specific syntax. A
.jsp file is dynamically compiled into a servlet and
executed. JSP tag syntax is composed of
directives, declarations, scriptlets, expressions,
and beans. In this topic, we're going to take a quick
look at syntax and show you some sample code.

JSP directives
JSP directives set global values such as class
declaration, a method to be implemented, or output
content type. Directive syntax is shown on the left.

Possible variables are language, method, import,


content_type, implements, and extends.

JSP declarations
Declarations define class-wide variables and
methods. Syntax for open tags and closing tags is
shown on the left.

Introduction to JavaServer Pages technology Page 20


Presented by developerWorks, your source for great tutorials ibm.com/developer

JSP scriptlets
JSP pages use scriptlets to mark Java code
destined for the servlet. The syntax is shown on
the left. HTML tags, interleaved with JSP tags, are
copied unchanged to the HTTP response stream.

JSP expressions
An expression is a short-hand notation for a
scriptlet that outputs a value to the response
stream. When the expression is evaluated, the
result is converted to a string and displayed. The
syntax shown on the left means "take this
expression, convert it to a string, and write it out to
the browser."

Bean syntax
Finally, let's look at bean tags. We have a whole
topic on the use of JavaBeans components in JSP
pages. And you know from our Overview that we
want to clearly separate the work areas of page
authors and producers from the Java scripting
language programmers.

JSP bean tags help to do this by separating page


authors from the details of the dynamic content
generation. Scripting language programmers can
write beans to perform various functions. Within
JSP pages, the bean tag is used to access a
JavaBean.

Introduction to JavaServer Pages technology Page 21


Presented by developerWorks, your source for great tutorials ibm.com/developer

bean tag attributes


If you looked at the bean tag shown on the MORE page last screen, you saw some
bean tag attributes, such as name, varname, type, introspect, beanName, create, and
scope.

Let's talk about what those attributes mean.

name is the value of the key used to look up the bean, for example, key-value for
getValue() or getAttribute().

varname is the name of the local variable that refers to the bean and is available for
use by scriptlets. If the varname attribute isn't specified, the value of the name attribute
will be used.

type is the Java language type of the JavaBean component being accessed. The
default type is java.lang.Object.

When introspect is yes, each property of the bean whose name matches the name of a
request parameter will have its setter method called to set the property value to the
value of the corresponding request parameter. The default value for introspect is yes.

bean tag attributes (continued)


Continuing with bean attributes, when create is yes, the bean is created (via
Beans.instantiate()) if not found in scope specified. If no is specified and the bean is not
found, an error is returned.

If the scope is request, the bean is to be retrieved from request context using the
getAttribute() method. This is default scope. If the bean is created, it is stored in the
current request context. This is used when the bean only needs to be available for the
current HTTP request.

When session, the bean is retrieved from the current session using the getValue()>
method. If the bean is created, it is stored in the current session. This is used when the
bean may be needed in multiple HTTP requests.

beanName is the filename used to create a JavaBean from a serialized bean file.

Introduction to JavaServer Pages technology Page 22


Presented by developerWorks, your source for great tutorials ibm.com/developer

JSP bean example


Let's look at a real-world example. Click MORE to
see bean code. When you're ready to see HTML
template tags, click Next to continue.

HTML template tag insert


One of the JSP technology goals is to separate
layout and content. HTML template tags help
accomplish this by giving layout staff special JSP
syntax for HTML template tags, insert and repeat.

insert tags give layout developers a familiar way to


design. The tags look like HTML but function like
Java code. Click MORE to see an insert code
sample.

HTML template tag repeat


Another special syntax HTML template tag is
repeat.

repeat tags are a way for the layout designer to


specify Java code that loops through indexed
properties. This is a useful function when you're
working with blocks of data, such as rows from a
database.

Introduction to JavaServer Pages technology Page 23


Presented by developerWorks, your source for great tutorials ibm.com/developer

Conclusion
In this section, we discussed JSP syntax for
directives, declarations, scriptlets, expressions,
and beans. We also reviewed the HTML template
tag.

Click Menu to return to the list of tutorial topics and


select the next topic.

Introduction to JavaServer Pages technology Page 24


Presented by developerWorks, your source for great tutorials ibm.com/developer

Section 6. Lab

JSP technology lab


If you've been using the Java programming
language for some time, the material in this tutorial
was a review. For experienced developers, we've
provided a lab for you to try on your own.

* To begin the lab, download labsetup_v2.doc.


* Open labsetup_v2.doc; read and follow the
directions.
* To complete the last step in labsetup, you'll
need jsptutoriallabfiles.zip.
* Finally, download jsp_lab.doc.
* Open jsp_lab.doc and work through the lab
exercise.

Click Menu to return to the list of tutorial topics. If


you went through the topics in order, you have
completed the tutorial. You can go back to review
any of the topics or close your browser to leave the
tutorial.

Colophon
This tutorial was written entirely in XML, using the developerWorks tutorial tag set. The tutorial is converted into a
number of HTML pages, a zip file, JPEG heading graphics, and a PDF file by a Java program and a set of XSLT
stylesheets.

Introduction to JavaServer Pages technology Page 25

You might also like