Outline25.1 Introduction25.2 JavaServer Pages Overview25.3 First JavaServer Page Example25.4 Implicit Objects25.5 Scripting 25.5.1 Scripting Components 25.5.2 Scripting Example25.6 Standard Actions 25.6.1 Action 25.6.2 Action 25.6.3 Action25.7 Directives 25.7.1 page Directive 25.7.2 include Directive
25.8 Custom Tag Libraries25.9 Internet and World Wide Web Resources
25.1 Introduction
JavaServer Pages
Extension of Servlet technology
Web content delivery
Reuse existing Java components
Without programming Java
Create custom tags
Encapsulate complex functionality
Classes and interfaces specific to JSP
Package javax.servlet.jsp
Package javax.servlet.jsp.tagext
25.2 JavaServer Pages Overview
Key components
Directives
Actions
Scriptlets
Tag libraries
25.2 JavaServer Pages Overview (cont.)
Directive
Message to JSP container
i.e., program that compiles/executes JSPs
Enable programmers to specify
Page settings
Content to include from other resources
Custom tag libraries used in the JSP
25.2 JavaServer Pages Overview (cont.)
Action
Predefined JSP tags that encapsulate functionality
Often performed based on information from client request
Can be used to create Java objects for use in scriptlets
25.2 JavaServer Pages Overview (cont.)
Scriptlet
Also called “Scripting Elements”
Enable programmers to insert Java code in JSPs
Performs request processing
Interacts with page elements and other components to implement dynamic pages
25.2 JavaServer Pages Overview (cont.)
Custom Tag Library
JSP’s tag extension mechanism
Enables programmers to define new tags
Tags encapsulate complex functionality
Tags can manipulate JSP content
25.2 JavaServer Pages Overview (cont.)
JSPs
Look like standard HTML or XHTML
Used when content is mostly fixed-template data
Small amounts of content generated dynamically
Servlets
Used when small amount of content is fixed-template data
Most content generated dynamically
25.2 JavaServer Pages Overview (cont.)
Some servlets do not produce content
Invoke other servlets and JSPs
JSPs execute as part of a Web server
JSP container
JSP first request
JSP container translates a JSP into a servlet
Handle the current and future requests
Code that represents the JSP
Placed in servlet’s _jspService method
25.2 JavaServer Pages Overview (cont.)
JSP errors
Translation-time errors
Occur when JSPs are translated into servlets
Request-time errors
Occur during request processing
Methods jspInit and jspDestroy
Container invokes when initializing and terminating a JSP
Methods are defined in JSP declarations
Part of the JSP scripting mechanism
25.3 A First JavaServer Page Example
Simple JSP example (Fig. 25.1)
Demonstrates
Fixed-template data (XHTML markup)
Creating a Java object (java.util.Date)
Automatic conversion of JSP expression to a String
meta element to refresh Web page at specified interval
First invocation of clock.jsp
Notice the delay while:
JSP container translates the JSP into a servlet
JSP container compiles the servlet
JSP container executes the servlet
Subsequent invocations should not experience the same delay
Cock.jspLine 10Meta element refershes the Web page every 60 seconds.
A Simple JSP Example13
Simple JSP Example
<%= new java.util.Date() %>
meta element refreshes the Web page every 60 seconds Creates Date object that is converted to a String implicitly and displayed in paragraph (p) element
Clock.jsp
25.4 Implicit Objects
Implicit Objects
Provide access to many servlet capabilities within a JSP
Four scopes
Application scope
Objects owned by the container application
Any servlet or JSP can manipulate these objects
Page scope
Objects that exist only in page in which they are defined
Each page has its own instance of these objects
Request scope
Objects exist for duration of client request
Objects go out of scope after response sent to client
Session scope
Objects exist for duration of client’s browsing session
Objects go out of scope when client terminates session or when session timeout occurs
25.5 Scripting
Scripting
Dynamically generated content
Insert Java code and logic in JSP using scripting
25.5.1 Scripting Components
JSP scripting components
Scriptlets (delimited by <% and %>)
Comments
JSP comments (delimited by <%-- and --%>)
XHTML comments (delimited by )
Java’s comments (delimited by // and /* and */)
Expressions (delimited by <%= and %>)
Declarations (delimited by <%! And %>)
Escape sequences
25.5.1 Scripting Components (cont.)
25.5.2 Scripting Example
Demonstrate basic scripting capabilities
Responding to get requests
welcome.jsp Lines 17-23Line 19 1 4
5
6
7
8
9
10
11
12 Processing "get" requests with data
13
14
15
16
17 <% // begin scriptlet
18
19 String name = request.getParameter( "firstName" );
20
21 if ( name != null ) {
22
23 %> <%-- end scriptlet to insert fixed template data --%>
24
Scriptlets used to insert Java code Use request implicit object to get parameter
Comments