Title: Using JavaServer Pages
1Using JavaServer Pages
- Harry R. Erwin, PhD
- CIT304/CSE301
2Review
- Last lecture, we examined the use of Java
servlets as a way to provide dynamic content. - Problems
- Requires fully-qualified Java programmers ()
- Tedious and error-prone ()
- Time-consuming and requires frequent update ()
- Adding a new servlet involves editing XML and
restarting the application () - Mixing static HTML with servlets does not scale
(). - This lecture, we will examine the use of
JavaServer Pages to overcome these problems.
3Resources
- Farley, Crawford, and Flanagan, 2002, Java
Enterprise in a Nutshell, second edition,
OReilly, ISBN 0-596-00152-5. For programmers. - Bergsten, 2002, JavaServer Pages, second edition,
OReilly, ISBN 0-596-00317-X. Covers JSP 1.2,
for both programmers and web page authors. Has
lots of worked examples. Recommended. If you have
to manage web application development, it wont
hurt you to know something about the process.
4JavaServer Pages (JSP)
- Allow you to embed the dynamic elements into the
static content. - Can be edited using a text editor or HTML
authoring tool. - Can be previewed live in the various browsers
that you plan to support.
5Template Engines
Resources
Template Processor
Web Server
Web Browser
Instruction Template
6Java and JSP
- JSP pages are compiled into Java servlets before
being executed. - Benefits
- More efficient lifecycle
- Integration with Java
- Easy use of JavaBeans without writing code
- Java code can be completely avoided.
- Avoids programming.
7JSP Basics
- Individual JSP pages are text files stored on the
web server. - When a page is first requested, the JSP engine
uses the page to generate a servlet. - The compiled servlet is saved and used to handle
later requests. - When a page is modified, the servlet is
recreated. - Precompilation of pages is also feasible.
8A Simple JSP (from Java Enterprise in a Nutshell)
ltHTMLgt ltBODYgt Hello, sailor, it is now lt new
java.util.Date().toString() gt lt/BODYgt lt/HTMLgt
The only non-self-evident element is the
new java.util.Date().toString() This
creates a date object and converts it to a String
that can be displayed. The lt gt tag is are used
to display a Java variable. XML syntax can also
be used, if you want to be open to the future.
9Points to Note
- More generally, the lt gt elements (HTML/XML
comments) can be used to insert regular Java code
to control the flow of a page. - We will discuss this in more detail next lecture.
- The next slide gives an example of an if, then,
else flow control construct used in a JSP page.
This is also from Java Enterprise in a Nutshell.
10Java Flow Control
ltHTMLgt ltBODYgt lt java.util.Date theDate new
java.util.Date() gt lt int theHour
theDate.getHours() gt lt if(theHourlt12) gt
Good morning, lt else gt Good
afternoon, lt gt sailor. It is now lt
theDate.toString() gt. lt/BODYgt lt/HTMLgt
11Java APIs Available
- JDBC (ODBC)
- RMI and CORBA
- Java Naming and Directory Interface
- Enterprise JavaBeans
- Java Message Service
- Java Transaction API
- JAXP
- JavaMail
12JSP Directives
- Affect the entire page.
- Begin with lt_at_ or lt _at_
- lt _at_include filedata.htmlgt
- Used to include static content
- lt _at_page gt
- Used to set page parameters
- lt _at_taglib gt
- Declares a tag library (later)
13Important Page Directives
- contentType
- MIME type of the page, default text/html
- extends
- JspPage class to be used if not default.
- import
- Java classes or packages to import.
- info
- A description of JSP page.
- session
- Indicates whether page participates in a user
session.
14Declaration Elements
- This is another type of tag used to create
variables. - lt! global variable declarationvalue gt
- These are defined at the level of the JSP rather
than at the session level. They persist across
invocations. - They can be used to count hits and detect
hacking. Not all that useful. Use other
approaches, if the data need to persist whenever
the JSP is rebuilt and reloaded.
15Built-ins
- Your web server provides classes that the JSP can
use. These include - Configuration data
- The standard output stream
- The request that created the invocation
- The response being generated
- The current users session record.
16Whats in it for the Web Designer?
- JSP supports action tags.
- These are regular HTML tags of two types
- Built-in functions
- Custom tags
- Built-in functions use XML namespace-enabled tag
syntax. They work with JavaBeans, too.
17Standard Action Tags
- ltjspuseBeangt imports a JavaBeans component.
- ltjspgetPropertygt gets a property value and adds
it to the response. - ltjspsetPropertygt sets a property value.
- ltjspincludegt includes the response from a
servlet or JSP page that is called for the
request. - ltjspforwardgt forwards the call to a servlet or
JSP page for service. - ltjspparamgt adds a parameter to a request.
- ltjspplugingt generates the OBJECT or EMBED tag
for an applet.
18taglib Directives
- If the web site architect wants to extend the JSP
tag syntax, she can also define custom action
tags. These are organized into Tag Libraries and
can be loaded using the taglib directive. - This allows her to remove almost all Java code
from the JSPs. Her web page developers use these
custom tags to replace the calls to Java
functions, beans and programs. - The programmers can then do their jobs separately
from everyone else.
19Conclusions
- JSP is a solution to providing dynamic content.
- Equivalent to ASP.NET but currently more popular.
- Can be used to avoid Java scripting.
- Next lecture
- Java programming basicsjust enough to let you
organize your JSP pagesthere is no requirement
to learn class or method syntax unless you want
to provide programming support to a web design
organization.