Generating Dynamic Content

advertisement
Chapter 5
Generating Dynamic Content
Creating a JSP Page

A Sample JSP Page: easy.jsp
<%@ page contentType="text/html" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
<title>JSP is Easy</title>
</head>
<body bgcolor="white">
<h1>JSP is as easy as ...</h1>
<%-- Calculate the sum of 1 + 2 + 3 dynamically --%>
1 + 2 + 3 = <c:out value="${1 + 2 + 3}" />
</body>
</html>
Installing a JSP Page

Create a Web Application directory
f9406000


easy.jsp
WEB-INF



web.xml
classes
lib



war
jstl.jar
lib\standard.jar
Running a JSP Page

http://localhost:8080/f9406000/easp.jsp
Using JSP Directive Elements

page

<%@ page contentType="text/html" %>

<%@ page contentType="text/html;charset=ms950"%>

contentType





text/plain, text/xml, text/vnd.wap.wml
errorPage, isErrorPage, session, pageEncoding, buffer,
and autoFlush
import, language
include
taglib
JSP Comments

<%-- Calculate the sum of 1 + 2 + 3 dynamically --%>
Using Template Text
<%@ page contentType="text/html" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
<title>JSP is Easy</title>
</head>
<body bgcolor="white">
<h1>JSP is as easy as ...</h1>
<%-- Calculate the sum of 1 + 2 + 3 dynamically --%>
1 + 2 + 3 = <c:out value="${1 + 2 + 3}" />
</body>
</html>
Using JSP Action Elements

<prefix:action_name attr1="value1" attr2="value2">
action_body
</prefix:action_name>


<prefix:action_name attr1="value1" attr2="value2" />
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
...
<c:out value="${1 + 2 + 3}" />
Standard action elements
Action element
<jsp:useBean>
<jsp:getProperty>
Description
Makes a JavaBeans component available in a page
Gets a property value from a JavaBeans component and adds it to
the response
<jsp:setProperty> Set a JavaBeans property value
<jsp:include>
Includes the response from a servlet or JSP page during the request
processing phase
<jsp:forward>
Forwards the processing of a request to a servlet or JSP page
<jsp:param>
Adds a parameter value to a request handed off to another servlet
or JSP page using <jsp:include> or <jsp:forward>
Standard action elements
Action element
Description
Generates HTML that contains the appropriate browser-dependent
<jsp:plugin>
elements (OBJECT or EMBED) needed to execute an applet with the
Java Plugin software
<jsp:attribute> Sets the value of an action attribute based on the body of this element
<jsp:body>
Sets the action element body based on the body of this element.
Required when the action element body contains <jsp:attribute>
action elements
Dynamically generates an XML element, optionally with attributes
<jsp:element>
<jsp:text>
and a body defined by nested <jsp:attribute> and <jsp:body>
actions
Used to encapsulate template text that should be used verbatim;
typically only needed in JSP pages written as XML documents
JSP Standard Tag Library

Core


XML processing


Processing of XML data, such as transforming and accessing individual elements
Internationalization (I18N) and formatting


Conditional processing and looping, importing data from external sources, etc.
Format and parse localized information, insert localized information in a page
Relational database access (SQL)

Read and write relational database data
The JSP Expression Language

1 + 2 + 3 = <c:out value="${1 + 2 + 3}" />

1 + 2 + 3 = ${1 + 2 + 3}
Expression Language operators
Operator
Operation performed
.
Access a bean property or Map entry
[]
Access an array or List element
( )
Group a subexpression to change the evaluation order
? :
Conditional test: condition ? ifTrue : ifFalse
+
Addition
-
Subtraction or negation of a value
*
Multiplication
/ or div
Division
% or mod
Modulo (remainder)
Expression Language operators
Operator
Operation performed
== or eq
Test for equality
!= or ne
Test for inequality
< or lt
Test for less than
> or gt
Test for greater than
<= or le
Test for less than or equal
>= or ge
Test for greater than or equal
&& or and
Test for logical AND
|| or or
Test for logical OR
! or not
Unary Boolean complement
empty
func(arg)
Test for empty variable values (null, an empty String or an array, Map, or
Collection without entries)
A function call, where func is the function name and arg is a function
argument
Implicit EL variables
Variable name
Description
pageScope
A collection (a java.util.Map) of all page scope variables
requestScope
A collection (a java.util.Map) of all request scope variables
sessionScope
A collection (a java.util.Map) of all session scope variables
applicationScope A collection (a java.util.Map) of all application scope variables
param
paramValues
A collection (a java.util.Map) of all request parameter values as
a single String value per parameter
A collection (a java.util.Map) of all request parameter values as
a String array per
Implicit EL variables
Description
Variable name
header
A collection (a java.util.Map) of all request header values as a single
String value per header
headerValues
A collection (a java.util.Map) of all request header values as a
String array per header
cookie
A collection (a java.util.Map) of all request cookie values as a single
javax.servlet.http.Cookie value per cookie
initParam
A collection (a java.util.Map) of all application initialization
parameter values as a single String value per value
pageContext
An instance of the javax.servlet.jsp.PageContext class, providing
access to various request data
EL Variable Name



<c:out value="${param.userName}" />
<c:out value="${param['user-name']}" />
Naming Convention rules
 namingConventionRules
Download