JSP elements – directive, action, and scripting

There are three types of JSP elements you can use: directive, action, and scripting.

Directive elements

The directive elements specify information about the page itself that remains the same between requests–for example, if session tracking is required or not, buffering requirements, and the name of a page that should be used to report errors, if any.
1)    <%@ page … %>
Legal attributes, with default values in bold, are:
import=”package.class” or import=”package.class1,…,package.classN”. This lets you specify what packages should be imported.

For example:
<%@ page import=”java.util.*” %>

The import attribute is the only one that is allowed to appear multiple times.
contentType=”MIME-Type” or
contentType=”MIME-Type; charset=Character-Set”
This specifies the MIME type of the output. The default is text/html. For example, the directive

<%@ page contentType=”text/plain” %>

has the same effect as the scriptlet
<% response.setContentType(“text/plain”); %>

•    isThreadSafe=”true|false”. A value of true (the default) indicates normal servlet processing, where multiple requests can be processed simultaneously with a single servlet instance, under the assumption that the author synchronized access to instance variables. A value of false indicates that the servlet should implement SingleThreadModel, with requests either delivered serially or with simultaneous requests being given separate servlet instances.

•    session=”true|false”. A value of true (the default) indicates that the predefined variable session (of type HttpSession) should be bound to the existing session if one exists, otherwise a new session should be created and bound to it. A value of false indicates that no sessions will be used, and attempts to access the variable session will result in errors at the time the JSP page is translated into a servlet.

•    buffer=”sizekb|none”. This specifies the buffer size for the JspWriter out. The default is server-specific, but must be at least 8kb.

•    autoflush=”true|false”. A value of true, the default, indicates that the buffer should be flushed when it is full. A value of false, rarely used, indicates that an exception should be thrown when the buffer overflows. A value of false is illegal when also using buffer=”none”.

•    extends=”package.class”. This indicates the superclass of servlet that will be generated. Use this with extreme caution, since the server may be using a custom superclass already.

•    info=”message”. This defines a string that can be retrieved via the getServletInfo method.

•    errorPage=”url”. This specifies a JSP page that should process any Throwables thrown but not caught in the current page.

•    isErrorPage=”true|false”. This indicates whether or not the current page can act as the error page for another JSP page. The default is false.

•    language=”java”. At some point, this is intended to specify the underlying language being used. For now, don’t bother with this since java is both the default and the only legal choice.Defines page-dependent attributes, such as session tracking, error page, and buffering requirements

2)    <%@ include … %>
A file on the local system to be included when the JSP page is translated into a servlet.
3)    <%@ taglib … %>
Declares a tag library, containing custom actions, that is used in the page

Standard action elements

Action elements typically perform some action based on information that is required at the exact time the JSP page is requested by a browser. An action can, for instance, access parameters sent with the request to do a database lookup. It can also dynamically generate HTML, such as a table filled with information retrieved from an external system.

Makes a JavaBeans component available in a page.    This usually means “instantiate an object of the class specified by class, and bind it to a variable with the name specified by id.” However, as we’ll see shortly, you can specify a scope attribute that makes the bean associated with more than just the current page.

Gets a property value from a JavaBeans component and adds it to the response. Retrieve and output bean properties.
Legal attributes are

Sets a JavaBeans component property value. Set bean properties, either explicitly or by designating that value comes from a request parameter.

Includes the response from a servlet or JSP page during the request processing phase. A file on the local system to be included when the JSP page is translated into a servlet.

Forwards the processing of a request to servlet or JSP page
<jsp:forward page=”/utils/errorReporter.jsp” />

Adds a parameter value to a request handed off to another servlet or JSP page using or

Generates HTML that contains the appropriate browser-dependent elements (OBJECT or EMBED) needed to execute an applet with the Java Plug-in software

Custom action elements and the JSP Standard Tag Library

In addition to the standard actions, the JSP specification includes a Java API a programmer can use to develop custom actions to extend the JSP language. The JSP Standard Tag Library (JSTL) is such an extension, with the special status of being defined by a formal specification from Sun and typically bundled with the JSP container. JSTL contains action elements for processes needed in most JSP applications, such as conditional processing, database access, internationalization, and more.
If JSTL isn’t enough, programmers on your team (or a third party) can use the extension API to develop additional custom actions, maybe to access application-specific resources or simplify application-specific processing.

Scripting elements

Scripting elements allow you to add small pieces of  Java code in a JSP page, such as an if statement to generate different HTML depending on a certain condition. Like actions, they are also executed when the page is requested. You should use scripting elements with extreme care: if you embed too much code in your JSP pages, you will end up with the same kind of maintenance problems as with servlets embedding HTML.

<% … %>
Scriptlet, used to embed scripting code. Code is inserted in service method.

<%= … %>
Expression, used to embed scripting code expressions when the result shall be added to the response. Also used as request-time action attribute values. Expression is evaluated and placed in output. The Java expression is evaluated, converted to a string, and inserted in the page. This evaluation is performed at run-time (when the page is requested)

<%! … %>
Declaration, used to declare instance variables and methods in the JSP page implementation class. Code is inserted in body of servlet class, outside of service method.

<%– comment –%>
Comment; ignored when JSP page is translated into servlet.

Gopal Das
Follow me

Gopal Das

Founder at GopalDas.Org
He is a technology evangelist, Salesforce trainer, blogger, and working as a Salesforce Technical Lead. After working in Java based project implementation, he jumped to the Salesforce system on a whim and never looked back. He fell in love with Salesforce’s flexibility, scalability, and power. He expanded his knowledge of the platform and became a Certified App Builder, Administrator, Platform Developer I, SalesCloud Consultant while leading the Salesforce implementation and technology needs. He has worked in a wide variety of applications/services like desktop, web and mobile applications.
Gopal Das
Follow me

Leave a Reply

Your email address will not be published. Required fields are marked *