Main Features of JSP 2.1

This section describes main features of JSP 2.1 including scripting elements, built-in objects, directives, JavaBean objects, JSTL, EL and standard actions.

What Are Main Features of JSP? The current release of JSP is JSP 2.1 which supports following main features:

1. Scripting Elements - JSP 2.1 supports 3 types of scripting elements: declarations, scriptlets and expressions. Scriptlets are most commonly used to enter any code fragments in the default Java language. For example, the following JSP code uses scriptlets to form an if-else flow control with a mix of static data and Java code fragments (not full Java statements):

<% if (Calendar.getInstance().get(Calendar.AM_PM) == Calendar.AM) {%>
Good Morning
<% } else { %>
Good Afternoon
<% } %>

2. Built-in Objects - JSP 2.1 supports a number of built-in implicit objects that provide you easy access to different parts of the application environment. For example, the following JSP code uses the built-in object "response" to set the "Content_Type" header in the HTTP response message:

<% response.setContentType("text/html;charset=UTF-8"); %>

3. Directives - JSP 2.1 supports directives as commands to the JSP engine. Commonly used directives are: page, variable, taglib, and include. For example, the following JSP code uses the "page" directive to import a Java class as a data type to the page scope:

%@ page import="javax.servlet.http.Cookie" %>

4. JavaBean - JSP 2.1 supports JavaBean objects using the "useBean" action element. For example, the following JSP code loads a JavaBean object, sets a property to the object, and retrieves it back later:

<jsp:useBean id="b" class="com.herongyang.CacheBean"/>
<jsp:setProperty name="b" property="text" value="Hello world!"/>
Property from my Bean: <jsp:getProperty name="b" property="text"/>

5. JSTL (JSP Standard Tag Library) - JSP 2.1 supports JSTL core library and custom tag libraries. For example, the following JSP code uses the "forEach" tag in the JSTL core library to generate dynamic data as an execution loop:

<c:forEach xmlns:c="http://java.sun.com/jsp/jstl/core"
var="counter" begin="1" end="3">
<p>${counter}</p>
</c:forEach>

6. EL (Expression Language) - JSP 2.1 supports EL that allows you to build expressions more powerful and complex than the scripting expression with the default Java language. For example, the following JSP code uses an EL expression to retrieve the value of the first cookie from the HTTP request:

<c:out value="Cookie: ${pageContext.request.cookies[0].value}"/>

7. Standard Actions - JSP 2.1 supports a number of standard actions that allows to perform some common tasks. For example, the following JSP code uses the "forward" standard action to redirect users to different pages based on an if condition:

<% if (Calendar.getInstance().get(Calendar.AM_PM) == Calendar.AM) {%>
<jsp:forward page="morning.jsp"/>
<% } else { %>
<jsp:forward page="afternoon.jsp"/>
<% } %>

Last update: 2012.

Table of Contents

 About This Book

JSP (JavaServer Pages) Overview

 What Is JSP (JavaServer Pages)?

Main Features of JSP 2.1

 How JSP Pages Are Processed?

 Popular JSP Enabled Web Servers

 Tomcat 7 Installation on Windows Systems

 JSP Scripting Elements

 Java Servlet Introduction

 JSP Implicit Objects

 Syntax of JSP Pages and JSP Documents

 JSP Application Session

 Managing Cookies in JSP Pages

 JavaBean Objects and "useBean" Action Elements

 Managing HTTP Response Header Lines

 Non-ASCII Characters Support in JSP Pages

 Performance of JSP Pages

 EL (Expression Language)

 Overview of JSTL (JSP Standard Tag Libraries)

 JSTL Core Library

 JSP Custom Tags

 JSP Java Tag Interface

 Custom Tag Attributes

 Multiple Tags Working Together

 File Upload Test Application

 Outdated Tutorials

 References

 PDF Printing Version