JSP Notes for OCEJWCD Exam

  • Posted on: 4 August 2013
  • By: Zhijun Chen

Here are the main notes for JSP part of Oracle Certified Expert, Java EE 6 Web Component Developer Exam.

Directive: <%@page attrList %>

  • import = “JAVA PACKAGE”
  • session = true | false
  • contentType = TYPE;[charset=CHARSET]
  • isELIgnored = true | false

Directive: <%@taglib attrList %>

  • uri = “URI”
  • tagdir = “the directory within the WAR of the library of tag files”
  • prefix = “prefix of the tag library”

Note: Translation error if BOTH of uri and tagdir are declared

Directive: <%@include file = “directory” %>

Declaration: <%! variable or method %>

Note: Content is put in global scope of translated JSP code, outside _jspService method

Scriptlet: <% content with semicolon %>

Expression: <%= expression without semicolon %>

Note: Content is put in _jspService method

Comments:

  • <!-- comment text here --> (Returned to client)
  • <%-- JSP comment here --> (Completely ignored)
  • <% /** Java comment here */ %> (Comment in translated file)

Implicit Objects:

  • request (Class implements javax.servlet.ServletRequest)
  • response (Class implements javax.servlet.ServletResponse)
  • pageContext (Class extends javax.servlet.jsp.PageContext)
  • session (Class implements javax.servlet.http.HttpSession)
  • application (Class implements javax.servlet.ServletContext)
  • out (Class extends javax.servlet.jsp.JspWriter)
  • config (Class implements javax.servlet.ServletConfig)
  • page (Class extends Object)
  • exception (Class java.lang.Throwable)

javax.servlet.jsp.JspContext (Class)

  • Object findAttribute(String name) [all scopes]
  • Object getAttribute(String name) [page scope]
  • Object getAttribute(String name, int scope)
  • Enumeration getAttributeNamesInScope(int scope)
  • int getAttributesScope(String name)
  • void removeAttribute(String name) [all scopes]
  • void removeAttribute(String name, int scope)
  • void setAttribute(String name, Object value) [page scope]
  • void setAttribute(String name, Object value, int scope)
  • JspWriter getOut()
  • JspWriter pushBody(Writer writer)
  • JspWriter popBody()
  • ExpressionEvaluator getExpressionEvaluator()
  • VariableResolver getVariableResolver()

javax.servlet.jsp.PageContext (Class extends JspContext)

  • Exception getException()
  • Object getPage()
  • ServletRequest getRequest()
  • ServletResponse getResponse()
  • ServletConfig getServletConfig() [retrieve config implicit object]
  • ServletContext getServletContext() [retrieve application implicit object]
  • HttpSession getSession()
  • JspWriter getOut()
  • void forward(String relativeURL)
  • void include(String relativeURL)
  • void include(String relativeURL, boolean flush)
  • ErrorData getErrorData()
  • void handlePageException(Exception e)
  • void handlePageException(Throwable t) 

JSP documents (XML view)

  • Root element: <jsp:root xmlns:jsp = “http://java.sun.com/JSP/Page” version = “2.0”>, version is mandatory, should be 1.2 or 2.0, this element is optional in JSP 2.0.
  • Directive: <jsp:directive.page attrList>
  • Directive: <jsp:directive.include attrList>
  • There is no <jsp:directive.taglib />
  • Expression: <jsp:expression>content</jsp:expression>, need to use <jsp:text> and CDATA to insert scripting elements in attribute values, e.g.

<jsp:text><![CDATA[<img src = “]]></jsptext>

<jsp:expression>request.getAttribute(“image_source”)</jsp:expression>

<jsp:text><![CDATA[” />]]>

Can also use Expression Language.

Expression Language

  • Basic syntax: ${expression}
  • String concatenation is not supported
  • Data types: Boolean, Integer, Floating point, String, Object, Null
  • Implicit objects:
    • pageContext: the PageContext object for the current JSP
    • pageScope: Map of page attributes
    • requestScope: Map of request attributes
    • sessionScope: Map of session attributes
    • applicationScope: Map of application/context attributes
    • param: Map of request parameter names matched against a single String parameter value
    • paramValues: Map of request parameters
    • header:Map of request header names against a single String header value
    • headerValues: Map of request headers
    • initParam: Map of context initialisation parameters
    • cookie: Map of cookie
  • object.string_property or object[“string_property”] for String, collection[“number”] or object[number] for interger
  • Operators: (Ordered)
    • [], .
    • ()
    • -, not, !, empty
    • *, /, div, %, mod
    • +, -
    • <, lt, >, gt, <=, le, >=, ge
    • ==, eq, !=, ne
    • &&, and
    • ||, or
    • ? :
  • Every EL function has an asscociated public static method in a compiled Java class
  • The web container evaluates an attribute/variable that appears in an expression by looking up its value according to the behavior of PageContext.findAttribute(String). For example, when evaluating the expression ${product}, the container will look for product in the page, request, session, and application scopes and will return its value.
  • Evaluation of scripting elements: <scripting-invalid>true</scripting-invalid> inside <jsp-property-group /> element
  • Evaluation of EL expressions
    • <el-ignored>true</el-ignored> in a <jsp-property-group/> element
    • isElIgnored = “true” in a page directive

<jsp:useBean id = “var_name” scope = “var_scope” attrList />

  • class = “class_name”
  • class = “class_name” type = “generalised_class”
  • beanName = “bean_class” type = “var_type”
  • type = “class_type”

Note: scope is default to page. The type attribute can be used without class, but unless an instance exists an exception will be thrown. The beanName attribute of useBean is only used with serialized beans.

<jsp:setProperty name = “var_name” property = “property_name” attrList />

  • property = “*” will iterate over all request parameters, update all matching ones
  • value = “val”
  • param = “req_param” will set value to the same as the request parameter

Note: When a setProperty tag is nested within a useBean tag it will not be executed if the bean already exists

<jsp:getProperty name = “var_name” property = “prop_name”>

<jsp:include attrList /> [body can contain <jsp:param />]

  • page = “url” (mandatory)
  • flush = “false” (optional)

<jsp:forward page = “rel_URL”> [body can contain <jsp:param />]

<jsp:param name = “name” value = “value” />

JSTL Core Library

  • <c:out value = “” escapeXml = true default = “” /> or <c:out>content</c:out>
    • value is mandatory
  • <c:catch var = “page_var_name”>nested actions</c:catch>
  • <c:remove var = “var_name” scope = “var_scope” />
    • var is mandatory
  • <c:set var = “var_name” scope = “var_scope” value = “var_value” /> or

<c:set var = “var_name” scope = “var_scope”>var_value</c:set>

    • var is mandatory
    • value is mandatory when body is empty
  • <c:set target = “obj_target” property = “prop_name” value = “value” /> or

<c:set target = “obj_target” property = “prop_name”>value</c:set> [the same as <jsp:setProperty />]

    • target and property are mandatory, value is mandatory if body is empty
    • obj_target must be a real-time expression, either EL or scripting element.
  • <c:if test = “test_cond” var = “var_name” scope = “var_scope” /> or

<c:if test = “test_cond” var = “var_name” scope = “var_scope”>output</c:if>

    • test is mandatory, var is mandatory if the body is empty
  • <c:choose>

<c:when test = “cond_1”>

       …

</c:when>

<c:otherwise>

       …

</c:otherwise>

       </c:choose>

  • <c:forEach begin = “index1” end = “index2” step = “step_length”>

body_content

</c:forEach>

  • <c:forEach items = “collection_obj”>

       body_content

</c:forEach> [var, begin, end, step can be added as optional attributes]

Special attribute: varStatus [object of type javax.servlet.jsp.jstl.core.TagLoopStatus] and has the following methods:

    • Object getCurrent()
    • int getIndex()
    • int getCount()
    • boolean isFirst()
    • boolean isLast()
    • Integer getBegin()
    • Integer getEnd()
    • Integer getStep()
  • <c:forTokens items = “token_string” delims = “separator_sequence”>

Body content

       </c:forTokens>

       [var, begin, end, step, varStatus can be added as optional attributes]

  • <c:url value = “url_expression” context = “context_url” var = “var_name” scope = “var_scope” /> [body can contain <c:param />]
    • value is mandatory
  • <c:import url = “resource_url” context = “app_context” varReader = “reader_obj”> [body can contain <c:param />]
    • url is mandatory
  • <c:redirect url = “redirect_url” context = “app_context” /> [body can contain <c:param />]
    • url is mandatory
  • <c:param name = “param_name” value = “param_value” /> or

<c:param name = “param_name”>param_value</c:param>

javax.servlet.jsp.tagext.Tag (Interface extends JspTag)

  • void setParent(Tag tag)
  • void setPageContext(PageContext pageContext)
  • Tag getParent()
  • void release()
  • int doStartTag() [return either EVAL_BODY_INCLUDE or SKIP_BODY]
  • int doEndTag() [return either EVAL_PAGE or SKIP_PAGE]

javax.servlet.jsp.tagext.IterationTag (Interface extends Tag)

  • int doAfterBody() [return either SKIP_BODY or EVAL_BODY_AGAIN]

javax.servlet.jsp.tagext.TagSupport (Class implements IterationTag)

  • doStartTag() returns SKIP_BODY
  • doAfterBody() returns SKIP_BODY
  • doEngTag() returns EVAL_PAGE
  • static Tag findAncestorWithClass(Tag from, Class clazz)

javax.servlet.jsp.tagext.BodyTag (Interface extends IterationTag)

  • void setBodyContent(BodyContent content)
  • void doInitBody()

Note: Both of these two methods will only be invoked when the doStartTag method returns EVAL_BODY_BUFFERED.

javax.servlet.jsp.tagext.BodyContent (Class extends JspWriter)

  • void clearBody()
  • void flush()
  • JspWriter getEnclosingWriter()
  • Reader getReader()
  • String getString()
  • void writeOut(Writer writer)

javax.servlet.jsp.tagext.BodyTagSupport (Class extends TagSupport implements BodyTag)

  • doStartTag() returns EVAL_BODY_BUFFERED
  • doAfterBody() returns SKIP_BODY
  • doEndTag() returns EVAL_PAGE

javax.servlet.jsp.tagext.SimpleTag (Interface extends JspTag)

  • void setJspContext(JspContext context)
  • void setParent(JspTag parent)
  • JspTag getParent()
  • void setJspBody(JspFragment fragment)
  • void doTag() 

javax.servlet.jsp.tagext.SimpleTagSupport (Class implements SimpleTag)

  • protected JspContext getJspContext()
  • protected JspFragment getJspBody()
  • static JspTag findAncestorWithClass(Tag from, Class clazz)

javax.servlet.jsp.tagext.JspFragment (Class)

  • JspContext getJspContext()
  • void invoke(Writer writer)

SimpleTags are never pooled, Class tags are pooled so instance variables need to be reset either inside doStartTag() or doEndTag()

javax.servlet.jsp.tagext.DynamicAttributes (Interface)

  • void setDynamicAttribute(String ns, String name, Object value)

javax.servlet.jsp.tagext.TagAdapter (Class implements Tag)

  • Tag getParent()
  • JspTag getAdaptee()

Tag files

<%@ tag attrList %> or <jsp:directive.tag attrList /> [replace page directive in tag files]

  • display-name
  • body-content: [empty, scriptless, tagdependent]
  • dynamic-attributes
  • small-icon
  • large-icon
  • description
  • example
  • language
  • import
  • pageEncoding
  • isELIgnored = true | false

Note: body-content = “JSP” is not permitted for a SimpleTag or a tag file.

<%@ attribute attrList %> or <jsp:directive.attribute attrList /> [used to specify the name and runtime type of all attributes declared by an action]

  • name
  • required = true | false
  • fragment = true | false
  • rtexprvalue = true | false
  • type = “full qualified class name”
  • description 

<%@ variable attrList %> or <jsp:directive.variable attrList />

  • name-given = “output_name”
  • name-from-attribute = “action_attribute_name”
  • alias = “local_name”
  • variable-class
  • declare = true | false
  • scope [AT_BEGIN, AT_END or NESTED(default)]
  • description 

Tag files implicit objects: request, response, jspContext, session, application, out, config

<jsp:doBody attrList />

  • varReader
  • var
  • scope

Note: var xor varReader

<jsp:invoke fragment = “attribute_name” />

  • varReader
  • var
  • scope

Note: var xor varReader

Add new comment

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.