JSP Directives and Implicit Objects

JSP Directives

Directives are labels used in a JSP page, identified by the @ symbol in their syntax:

<%@ Directive_Name [attribute_i = "value"]%>

Their scope is typically page, meaning their action is limited to the page where they are used. They do not return values to the client like expressions or scriptlets, except as generated by their implementation. The JSP 1.1 specification defines three standard directives: page, include, and taglib.

Page Directive

The page directive defines global attributes for the entire JSP page and any included files (except for dynamic content included with the include directive or jsp:include action). Its syntax is:

<%@ page [language = "java"] [extends = "package.class"] [import = "(package.class | .*),..."] [session = "true | false"] [buffer = "none | 8kb | size kb"] [autoflush = "true | false"] [isThreadSafe = "true | false"] [info = "message"] [errorPage = "relativeURL"] [contentType = "(mimeType [; charset = characterSet] | text/html; charset = ISO-8859-1)"] [isErrorPage = "true | false"] [pageEncoding = "(characterSet | ISO-8859-1)"]%>

Include Directive

The include directive allows including HTML, Java code, text, or other JSP pages at compile time. The final page processed by the JSP engine is formed by the base page and the content of the included resource. This directive can be placed anywhere in the JSP page. When using this directive, be careful with HTML tags like <html>, <body>, etc., as the included file might conflict with similar tags in the original JSP file. Ensure they are properly nested and not duplicated.

JSP Implicit Objects

JSP provides implicit or embedded objects that are always available to developers. These objects do not require instantiation and are available within the JSP container, simplifying JSP programming.

Object page

An instance of the java.lang.Object class, with a page scope. It represents an instance of a class generated from the current JSP page. Use the keyword this to refer to this JSP page.

Config Object

An instance of the javax.servlet.ServletConfig class, handling servlet configuration for the JSP page. Its scope is page.

Request Object

An instance of javax.servlet.ServletRequest, containing data sent to the server via a web page. For example, the getParameter() method retrieves values sent through a form or URL. Example:

<%
 String name = request.getParameter("name");
%>

Session Object

An instance of the javax.servlet.http.HttpSession class, managing user session activities. Its scope is session. A session is automatically created (unless specified otherwise) when a user requests a JSP page, allowing storage of user-related information. Example:

<%
 HttpSession unaSesion = request.getSession();
 unaSesion.setAttribute("user", "pancho_lopez");
%>

Application Object

An instance of the java.servlet.ServletContext class, with an application scope. It represents the web application running the JSP.