7.1 Understand the origins, benefits, and weaknesses of JSPs

As per 2.2 Describe essentials of JSPs

7.2 Describe JSP technology, the conversion of JSPs to servlets, and the lifecycle of JSPs

JspPage interface

  • jspInit() - can be overriden
  • jspDestroy() - can be overriden

HttpJspPage interface (extends JspPage)

  • _jspService(HttpServletRequest, HttpServletResponse) - cannot be overriden (remember the _)



  1. translation: convert .jsp to .java source code for a servlet
  2. compilation: compile .java file into .class file
  3. load class: load servlet class
  4. create instance: create instance of servlet
  5. call the jspInit() method: now ready to accept client requests
  6. call the _jspService() method: containers creates new thread to handle client's request
  7. call the jspDestroy() method

Note: translation and compilation happens only once, which can create a slight delay for the client, depending on container implementation


  • If the <scripting-invalid> is specified as true, then a jsp file must not contain any scripting elements otherwise the file is not even translated to the corresponding servlet.
  • If the <el-ignored> is specified as true, you can still have EL expression in the jsp file. They are ignored and passed as they are in the output. There is no translation error.
        <scripting-invalid>true</scripting-invalid> <!-- disable scripting, can only be done from here -->
        <el-ignored>true</el-ignored> <!-- disable el, or use isELIgnored="true" with page directive -->

7.3 Understand JSP scripting elements, declarations and directives

Scripting elements (scriptlets and expressions)

Scripting variables have a 'servlet scope' which is different to the four other scopes. You can access the other scopes through PageContext or implicit variables.

<%-- comment --%>
<%-- all scriptlet code is compiled into _jspService() (unlike declarations) --%>
<%-- don't forget the semi-colon --%>
<% out.println(mybean.getName()); %>
<%-- an 'expression' is like out.print(value). no space is allowed between % and =, otherwise compile error --%>
<%= value %>
<%-- expression languages are covered in 6.4 --%>


In the generated servlet, variables are declared at the class level.

<%-- for declaring variables at the class level for generated servlet, can be static or instance variables and methods --%>
<%! code %>
<%!  int doubleCount() {
    count = count*2;
    return count;


Three of them, starting with <%@ :

  • include
  • page
  • taglib

Any page directive attribute except import can occur at most once in a translation unit unless the values for the duplicate attributes are identical for all occurrences.


The directive happens at translation time (converting to servlet), whereas <jsp:include> happens at runtime using the RequestDispatcher.include(). Note also that:

  • directive uses file attribute
  • action using page attribute
  • both are position-sensitive
<%@ include fle="wickedHeader.html" %>


Important attributes:

  • import: like standard Java import
  • isThreadSafe: only use if setting false in order to use SingleThreadModel (shouldn't do this!)
  • contentType: define MIME type and optional character encoding
  • isELIgnored: ignore EL when translating
  • isErrorPage: makes exception implicit object available is set to true
  • errorPage: which JSP will handle errors
  • There are 13 in total, but the above 6 are most important


Attribute Description
The scripting language used in the JSP. Currently, the only valid value for this attribute is java .
Specifies the class from which the translated JSP can inherit. This attribute must be a fully qualified class name .
Specifies a comma-separated list of fully qualified type namesand/or packages that will be used in the current JSP. When the scripting language is java , the default import list isjava.lang.* , javax.servlet.* ,javax.servlet.jsp.* and javax.servlet.http.* . If multiple import properties are specified, the packagenames are placed in a list by the container.
Specifies whether the page participates in a session. The values for this attribute are true (participates in a sessionthe default) or false (does not participate in a session). When the page is part of a session, implicit objectsession is available for use in the page. Otherwise,session is not available, and using session in the scripting code results in a translation-time error.
Specifies the size of the output buffer used with the implicit object out . The value of this attribute can be none for no buffering or a value such as 8kb (the default buffer size). The JSP specification indicates that the buffer used must be at least the size specified.
When set to TRue (the default), this attribute indicates that the output buffer used with implicit object out should be flushed automatically when the buffer fills. If set to false , an exception occurs if the buffer overflows. This attribute's value must be TRue if the buffer attribute is set to none .
Indicates the level of thread safety implemented in the page. If false then the JSP container shall dispatch multiple outstanding client requests, one at a time, in the order they were received, to the page implementation for processing. If true then the JSP container may choose to dispatch multiple outstanding client requests to the page simultaneously. Page authors using true must ensure that they properly synchronize access to the shared state of the page. Default is true. Note that even if the isThreadSafe attribute is false the JSP page author must ensure that accesses to any shared objects are properly synchronized., The objects may be shared in either the ServletContext or the HttpSession.
Specifies an information string that describes the page. This string is returned by the getServletInfo method of the servlet that represents the translated JSP. This method can be invoked through the JSP's implicit page object.
Any exceptions in the current page that are not caught are sent to the error page for processing. The error-page implicit object exception references the original exception.
Specifies whether the current page is an error page that will be invoked in response to an error on another page. If the attribute value is true , the implicit object exception is created and references the original exception that occurred. If false (the default), any use of the exception object inthe page results in a translation-time error.
Specifies the MIME type of the data in the response to the client. The default type is text/html .
Specifies the character encoding of the JSP page. The default value is ISO-8859-1.
Specifies whether JSP container should evaluate expressions that use the Expression Language (EL) a new feature in JSP 2.0 that allows JSP authors to create scriptless JSP pages. EL is typically used with JSP tag libraries, which are beyond the scope of this book. An EL expression has the form ${ exp } . If the attribute value is true , the EL expressions are ignored, which means that the JSP container does not evaluate the expressions at translation time. If false , the EL expressions are evaluated by the JSP container. For more information on EL, visit java.sun.com/developer/EJTechTips/2004/tt0126.html
<%@ page import="foo.*" session="false" %>
<%@ page session="true" %>


<%@ page errorPage="errorPage.jsp" %>

Error pages make the following request attributes available:

Request Attributes Type
javax.servlet.error.status_code java.lang.Integer
javax.servlet.error.exception_type java.lang.Class
javax.servlet.error.message java.lang.String
javax.servlet.error.exception java.lang.Throwable
javax.servlet.error.request_uri java.lang.String
javax.servlet.error.servlet_name java.lang.String


  • implicit exception object is available
<%@ page isErrorPage="true" %>
exception.printStackTrace(response.getWriter()) <!%-- print stack trace --%>

tag library

  • Prefix is case-sensitive
  • uri can be logical (matching TLD), or path to the TLD file (fallback mechanism)
  • In JSP 2.2, a new attribute named tagdir is also added. This is used in place of uri (i.e. you cannot use uri as well as tagdir both at the same time)  to indicate that this prefix is to be used to identify tag extensions installed in the /WEB-INF/tags/ directory or a subdirectory.
<%@ taglibname prefix="prefix" uri="" %>
// e.g.
<%@ taglib tagdir="/WEB-INF/tags/cool" prefx="cool" %>
// then use

DD configuration

    <!-- not needed for JSP 2.0+ -->
    <!-- optional, disable scripting -->

7.4 Use JSP implicit variables

Standard variables. The bold ones also exist as EL implicit objects.

  • application (ServletContext)
  • config (ServletConfig)
  • cookie
  • exception (JspException, only available to error pages)
  • out (JspWriter)
  • page (this; declared as Object, but dynamic type is servlet class instance generated for the JSP)
  • pageContext (use to access variables from all scopes, not just page)
  • param
  • paramValues
  • request
  • response
  • session

Scope variables:

  • pageScope
  • requestScope
  • sessionScope
  • applicationScope


PageContext extends JspContext and contains static variables for scope:


For more, see 6.1 Understand the four data scopes

Use, for example:

<%-- default scope is PAGE_SCOPE --%>
<% pageContext.setAttribute("foo") %>
<%= pageContext.getAttribute("foo") %>
<% pageContext.removeAttribute("foo") %>
<%-- use PageContext variables to specify other scopes --%>
<%= pageContext.getAttribute("foo", PageContext.REQUEST_SCOPE) %>
<%= pageContext.getAttribute("foo", PageContext.SESSION_SCOPE) %>
<%= pageContext.getAttribute("mail", PageContext.APPLICATION_SCOPE) %>
<%-- search all scopes for the attribute --%>
<%= pageContext.findAttribute("foo") %>

7.5 Understand and use jsp: tags

These are also known as actions or 'JSP standard actions'. Non-standard actions include those from JSTL.

<jsp:attribute> // new since 2.0
<jsp:param> // can be used with include or forward

For JSP XML documents you can use:

<jsp:directive.include file="test.js" />

Example usage


  • If type is used without class, the bean must already exist.
  • If class is used, with or without type, the class must NOT be abstract, and must have a public no-arg constructor.
  • Default scope is page.
  • type => reference type
  • class => object type
<jsp:include page="Header.jsp">
    <jsp:param name="subTitle" value="We take the sting out of SOAP." />
// cannot commit response before forward, e.g. out.flush()
<% if (request.getParameter("userName") == null) { %>
    <jsp:forward page="HandleIt.jsp" />
<% } %>


The body of the element either specifies a value that is evaluated once, or it specifies a "JSP fragment," which represents the body in a form that makes it possible for a tag handler to evaluate it as many times as needed. The <jsp:attribute> action must only be used to specify an attribute value for standard or custom actions.

The following example uses an XML element attribute to define the value of the param1 attribute, and uses an attribute standard action to define the value of the param2 attribute. In this example, the value of param2 comes from the result of a custom action invocation.

<mytag:paramTag param1="value1">
   <jsp:attribute name="param2">
     <mymath:add x="2" y="2"/>


  • default scope is page. The bean will not be found if the wrong scope is used, rather a new bean will be created.
  • the body of useBean tag is executed ONLY if the useBean tag instantiates a bean (as opposed to locate an existing bean)
  • The three attributes: class, type and beanName can occur only as one of the following four combinations and at least one of these four combinations must be there in a useBean tag:
  1. class
  2. type
  3. class and type
  4. beanName and type
<%-- if person did not already exist, then it would be created, kind of like request.getSession() --%>
<jsp:useBean id="person" class="foo.Person" scope="request" />
<%-- the body of the useBean will run only if the bean does not already exist --%>
<jsp:useBean id="person" class="foo.Person">
    <jsp:setProperty name="person" property="name" />
<%-- beanName: instantiates the bean using the java.beans.Beans.instantiate() method. --%>
<jsp:useBean id="user" beanName="myco.User" type="myco.interfaces.IUser" />

<jsp:useBean> with type attribute

  • If type and class are incompatible, the code will not compile.
<%-- use "type" when working with classes that inherit from others or implement interfaces --%>
<jsp:useBean id="person" type="foo.Person" class="foo.Employee" scope="page">
<%-- type without class will work with existing bean, but throw InstantiationException if no bean exists --%>
<jsp:useBean id="person" type="foo.Person" scope="page"/>
<%-- The body will NEVER run! It is pointless to put a body inside a <jsp: useBean > tag if you have only a type and no class!
Remember, the tag body executes ONLY if a new bean is created, which can never happen when only a type (but no class)  --%>
<jsp:useBean id="person" type="foo.Employee" scope="request" >
    <jsp:setProperty name="person" property="name" value="Fred" />
</jsp:useBean >
<%-- The param attribute lets you set the value of a bean property to the value of a request parameter. --%>
<jsp:useBean id="person" type="foo.Person" class="foo.Employee">
    <jsp:setProperty name="person" property="name" param="userName" />
<%-- If the request parameter name matches the bean property name, you do not need to specify param --%>
<jsp:useBean id="person" type="foo.Person" class="foo.Employee">
    <jsp:setProperty name="person" property="name" />
<%-- If all request parameters match, you can use * --%>
<jsp:useBean id="person" type="foo.Person" class="foo.Employee">
    <jsp:setProperty name="person" property="*" />


  • If no scope is specified, getProperty works the same as PageContext.findAttribute(): searches for the named attribute in page, request, session (if valid), and application scope(s) in order and returns the value associated or null.
<jsp:getProperty name="person" property="name" />
// equivalent to
<%= person.getName() %>


<jsp:setProperty name="person" property="name" value="Bob" />
<%-- setProperty within a useBean with a body will set a property only if the bean does NOT exist --%>
<jsp:useBean id="person" class="foo.Person" scope="page" >
    <jsp:setProperty name="person" property="name" value="Fred" />
</jsp:useBean >

Bean notes

Although covered in OCPJP, a few reminder notes about beans:

  • Must have a public, no-arg constructor
  • public getter and setter methods starting with "get" (or "is", for a boolean) and "set", followed by the same word. (getFoo(), setFoo())
  • The setter argument type and the getter return type MUST be identical: int getFoo() void setFoo(int foo)

JSP Documents

This section has details on JSP XML documents, as they are not explicitly listed on the objectives. Chapter 6 of the JSP 2.2 specification has more details.




  • no angle brackets are allowed, so the following must be used:
for(int i=0; i&lt;5; i++)


  • HTML can be used without the jsp:text tags, so long as it is well-formed.


If you have questions, corrections, information or examples to add, please comment below.

Add a New Comment