Saturday, November 15, 2008

JSP

Key Acronyms         

JSP  = JavaServer Pages
JSTL = JSP Standard Tag Library
TLD  = Tag Library Descriptor
WAR  = Web-application
 ARchive
EL = Expression Language

Page Lifecycle

1. Page Translation
2. Page Compilation
3. Load Class
4. Create Object
5. Call jspInit()
6. Call _jspService()
7. Call jspDestroy()

BASIC SYNTAX

Directive Elements (translation phase)

<%@ page ... %>                                      - <jsp:directive.page .. /> in XML
<%@ include file="filename" %>                       - <
jsp:directive.include .. /> in XML
<%@
 taglib prefix="shortName" uri="tldUniqueName" %> - xmlns:prefix="tldUniqueName" in XML

Scripting Elements

<%  ...; %> - script-let   - in XML

<%= ...  %> - expression   - in XML

<%! ...; %> - declaration  - in XML

<%-- ... --%> - comment    - in XML

${ }       - JSTL 1.0/JSP2.0

Implicit objects (note not available in declarations)

application  - ServletContext
config       - ServletConfig
exception    -
 Throwable (only for pages with page directive isErrorPage="true")
request      -
 HttpServletRequest
response     -
 HttpServletResponse
out          -
 JSPWriter
page         - Object
pageContext  - PageContext
session      -
 HttpSession (only for pages with page directive session="true")

Page Directive Attributes

autoFlush    - Boolean indicating whether buffer should flush when full (true)
buffer       - buffer size in kilobytes, e.g. "32kb"
contentType  - MIME type and character encoding of output (text/html;ISO-8859-1)
errorPage    - URL of a JSP page capable of handling errors for this page (null)
extends      - sub-class of
 javax.servlet.jsp.JspPage to use
import       - comma-separated list of packages/classes (
java.lang.*, etc..)
isErrorPage  - boolean indicating page can accept errors (false)
isThreadSafe - Boolean indicating whether page is thread-safe (true)
language     - scripting language (java)
pageEncoding - character encoding of output (ISO-8859-1)
session      -
 boolean indicating whether takes part in a session (true)

Action Elements/Tags (request processing phase)

<jsp:useBean id="beanName" class="com.classes.BeanClass" [scope="page"]/>
<
jsp:useBean id="beanName" class="com.classes.BeanClass" type="com.classes.BeanSuperClass"/>
<
jsp:useBean id="beanName" beanName="serializedName" type="com.classes.BeanSuperClass"/>
<
jsp:getProperty name="beanName" property="propertyName"/>
<
jsp:setProperty name=“beanName" property="propertyName" value="propertyValue"/>
<
jsp:setProperty name=“beanName" property="propertyName" [param="requestParamName"]/>
<
jsp:include page="relativeURL"/>
<
jsp:forward page="relativeURL"/>
<
jsp:param name="paramName" value="paramValue"/>
<
jsp:plugin/>

JSTL libraries

http://java.sun.com/jsp/jstl/core (c)

http://java.sun.com/jsp/jstl/xml (x)

http://java.sun.com/jsp/jstl/fmt (fmt)

http://java.sun.com/jsp/jstl/sql (sql)

http://java.sun.com/jsp/jstl/fn (fn)

JSTL functions (JSTL 1.1)

c:out(value)

c:param(name, value)

c:forEach(var, items, varStatus)

c:forToken

c:url

c:import(url)

 

fmt:formatNumber(value, pattern)

fmt:parseNumber

fmt:formatDate

fmt:parseDate

fmt:timeZone

fmt:setTimeZone

fmt:setLocale(value)

fmt:bundle(basename)

 

fn:contains(string, substring)

fn:containsIgnoreCase(string, substring)

fn:endsWith(string, suffix)

fn:escaleXml(string)

fn:indexOf(string, substring)

fn:join(array, separator)

fn:length(item)

fn:replace(string, before, after)

fn:split(string, separator)

fn:startsWith(string, prefix)

fn:substring(string, begin, end)

fn:substringAfter(string, substring)

fn:substringBefore(string, substring)

fn:toLowerCase(string)

fn:toUpperCase(string)

fn:trim(string)

 

sql:setDataSource(var,driver,url)

sql:query(var,datasource)

sql:update

sql:transaction(dataSource)

 

x:parse(source, var)

x:out(select)

x:transform(source, xslt)

PageContext methods

setAttribute(name, valueObject)
getAttribute(name)
setAttribute(name, valueObject, scope)
getAttribute(name, scope)
getAttributeNamesInScope(scope)
removeAttribute(name, scope)
getServletContext()
getSession()
getResponse()
getRequest()

Variable scopes

jsp:usebean  Servlet

===========  =======

page         PageContext.PAGE_SCOPE

request      PageContext.REQUEST_SCOPE

session      PageContext.SESSION_SCOPE

application  PageContext.APPLICATION_SCOPE

Custom tag interfaces

Tag          - base class for simple tags
IterationTag - adds doAfterBody() for iteration processing     (default implementation -> TagSupport)
BodyTag      - adds doInitBody() for handling tags with bodies (default implementation -> BodyTagSupport)

Tag methods (in order of execution)

setPageContext() - called by the container
setParent()      - called by the container
setXXXXXX()      - called for each attribute
doStartTag()     - returns either EVAL_BODY_INCLUDE or SKIP_BODY (or EVAL_BODY_BUFFERED for BodyTag)
setBodyContent() - called by the container (BodyTag only if EVAL_BODY_BUFFERED)
doInitBody()     - return void             (BodyTag only if EVAL_BODY_BUFFERED)
doAfterBody()    - returns either EVAL_BODY_AGAIN or SKIP_BODY (IterationTag and BodyTag)
doEndTag()       - returns either EVAL_PAGE or SKIP_PAGE
release()        - called when tag handler no longer required

 

doStart

doAfterBody

doEndTag

EVAL_BODY_INCLUDE

All

 

 

EVAL_BODY_BUFFERED

BodyTag

 

 

EVAL_BODY_AGAIN

 

Iteration

 

SKIP_BODY

All

Iteration

 

EVAL_PAGE

 

 

All

SKIP_PAGE

 

 

All

Expression Language - Literals

true, false, 5, -5, 5.5, 5.5E5, "string", 'string', null

Expression Language - Operators

+,-,*,/,%,==,!,!=,<,>,<=,>=,&&,||,(,)
condition ?
 ifTrue : ifFalse
empty set

Expression Language - Equivalent JSP expressions

{$list[1]}               == <%= list.elementAt(1) %>

{$map.name}              == <%= map.get("name") %>

{$map['name']}           == <%= map.get("name") %>

{$map[name]}             == <%= map.get(pageContext.findAttribute("name")) %>

{$bean.property}         == <%= bean.getProperty() %>

 

{$pageContext.response}  == <%= pageContext.getResponse() %>

{$pageContext.session}   == <%= pageContext.getSession() %>

{$pageContext.context}   == <%= pageContext.getServletContext() %>

{$pageContext.request.requestURI} == <%= pageContext.getRequest().getRequestURI() %>

 

{$param.name}            == <%= request.getParameter("name") %>

{$paramValues.name}      == <%= request.getParameterValues("name") %>

{$header.name}           == <%= request.getHeader("name") %>

{$headerValues.name}     == <%= request.getHeaderValues("name") %>

 

{$pageScope.cart.count}  == <%= pageContext.getAttribute("cart", PageContext.PAGE_SCOPE).getCount() %>

{$requestScope.cart}     == <%= pageContext.getAttribute("cart", PageContext.REQUEST_SCOPE) %>

{$sessionScope.cart}     == <%= pageContext.getAttribute("cart", PageContext.SESSION_SCOPE) %>

{$applicationScope.cart} == <%= pageContext.getAttribute("cart", PageContext.APPLICATION_SCOPE) %>

 

{$mytl:myfn()}           - calls the myfn() function of the mytl tag library

 

EL functions are code as public static methods and identified using the function element of the TLD.

Tag Library Descriptor (META-INF/taglib.tld)

  • taglib
    • short-name
    • [display-name]
    • [description]
    • [uri] - if implicit taglib URI indirection is used
    • tlib-version
    • jsp-version
    • tag*
      • name
      • [display-name]
      • [description]
      • tag-class   - implementation of Tag
      • [tei-class] - implementation of TagExtraInfo
      • [body-content] - { empty | JSP | tagdependent }
      • [variable]*
      • [attribute]*
        • name
        • [description]
        • [required]    - { yes | no | true | false }
        • [rtexprvalue] - { yes | no | true | false } request time expressions
        • [type]        - request time expression type (default java.lang.String)
      • [example]
    • [function]*
      • name
      • [display-name]
      • [description]
      • function-class
      • function-signature
    • [listener]

WAR structure

/WEB-INF/web.xml
/WEB-INF/classes/
/WEB-INF/lib/

Post a Comment

  © Blogger templates Newspaper III by Ourblogtemplates.com 2008

Back to TOP