JSP Directives, Built-in Objects, and Action Tags

JSP pages support zero or more directives that control how the page is processed by the container.

page Directive

The <%@ page %> directive configures page-level settings. Key attributes include:

  • pageEncoding: Defines the character encoding used to interpret the JSP file during compilation. This affects how the server reads the source file when converting it to a Java servlet.
  • contentType: Sets the HTTP Content-Type header. For example, contentType="text/html;charset=UTF-8" is equivalent to calling response.setContentType("text/html;charset=UTF-8").
  • If only one of pageEncoding or contentType is specified, the other defaults to its value. If neither is set, the fallback encoding is ISO-8859-1.

import: Allows multiple package imports, similar to Java’s import statement.

errorPage: Specifies a destination page to which control is forwarded if an uncaught exception occurs during execution.

isErrorPage: When set to true, this page is designated as an error handler. It gains access to the implicit exception object and automatically receives a 500 status code.

Global error handling can also be configured in web.xml:

<error-page>
    <error-code>404</error-code>
    <location>/error/errorPage.jsp</location>
</error-page>

<error-page>
    <error-code>500</error-code>
    <location>/error/errorPage.jsp</location>
</error-page>

<error-page>
    <exception-type>java.lang.RuntimeException</exception-type>
    <location>/index.jsp</location>
</error-page>

autoFlush: When true (default), the output buffer automatically flushes when full. If false, a IOException is thrown on buffer overflow.

buffer: Sets the size of the output buffer (default: 8KB). Rarely modified in practice.

isELIgnored: Controls whether Expression Lenguage (EL) is processed. Default is false — EL is enabled.

isThreadSafe: When false, the generated servlet implements SingleThreadModel (deprecated).

session: If false, the implicit session object is not available in the page.

extends: Specifies a superclass for the generated servlet class.

include Directive

The <%@ include file="..." %> directive performs static inclusion during translation. The included file is merged into the source before compilation, resulting in a single servlet class.

This contrasts with dynamic inclusion via <jsp:include>, which occurs at runtime and involves separate servlet instances.

Use static inclusion for reusable, unchanging fragments (e.g., headers, footers).

taglib Directive

Imports custom tag libraries using:

  • prefix: A namespace identifier for the tags (e.g., s).
  • uri: The location of the tag library descriptor (TLD).
<%@ taglib prefix="s" uri="/struts-tags" %>
<s:text name="greeting" />

JSP Implicit Objects

JSP provides nine built-in objects accessible without declaration:

Object Type Description
out JspWriter Writes output to the response stream
page Object Reference to the current JSP page instance
config ServletConfig Configuration data for the servlet
pageContext PageContext Central context for managing scope and accessing other objects
request HttpServletRequest Represents the HTTP request
response HttpServletResponse Represents the HTTP response
exception Throwable Available only in error pages; holds the thrown exception
session HttpSession Manages user session state
application ServletContext Represents the web application context

pageContext is the most powerful — it acts as a bridge between all other objects and provides access to four scopes:

  • PAGE_SCOPE: Data visible only within the current JSP page
  • REQUEST_SCOPE: Data available for the duration of a single request
  • SESSION_SCOPE: Data tied to a user session
  • APPLICATION_SCOPE: Data shared across the entire web application

It also supports:

  • Forwarding attributes between scopes: pageContext.setAttribute("key", value, PageContext.SESSION_SCOPE)
  • Searching across all scopes: pageContext.findAttribute("key") (searches PAGE → REQUEST → SESSION → APPLICATION)
  • Accessing the other eight implicit objects via methods like getPage(), getRequest(), getSession(), etc.

JSP Action Tags

Unlike HTML tags, JSP action tags are processed by the server at runtime and execute Java logic.

  • <jsp:forward>: Redirects control to another resource. Behaves identically to RequestDispatcher.forward(). The original request and response objects are passed along.
  • <jsp:include>: Dynamically includes the output of another resource. Differs from the include directive in that inclusion occurs at runtime, not compile time. Each resource is compiled separately.
  • <jsp:param>: Used as a child of <jsp:forward> or <jsp:include> to pass parameters to the target resource.
<jsp:include page="header.jsp">
    <jsp:param name="title" value="Welcome Page" />
</jsp:include>

Use <jsp:include> when the included content varies per request. Use the include directive for static, unchanging components.

Tags: JSP pageDirective includeDirective taglib pageContext

Posted on Thu, 23 Jul 2026 16:07:20 +0000 by cbassett03