JSTL

JavaServer Pages Standard Tag Library

Tag Libraries
JSTL core JSTL 1.1 core library
JSTL fmt JSTL 1.1 i18n-capable formatting library
JSTL sql JSTL 1.1 sql library
JSTL XML JSTL 1.1 XML library
JSTL functions JSTL 1.1 functions library

JSTL core

Standard Syntax:
<%@ taglib prefix=”c” uri=”http://java.sun.com/jsp/jstl/core” %>

Tag Summary
catch Catches any Throwable that occurs in its body and optionally exposes it.
choose Simple conditional tag that establishes a context for mutually exclusive conditional operations, marked by <when> and <otherwise>
if Simple conditional tag, which evalutes its body if the supplied condition is true and optionally exposes a Boolean scripting variable representing the evaluation of this condition
import Retrieves an absolute or relative URL and exposes its contents to either the page, a String in ‘var’, or a Reader in ‘varReader’.
forEach The basic iteration tag, accepting many different collection types and supporting subsetting and other functionality
forTokens Iterates over tokens, separated by the supplied delimeters
out Like <%= … >, but for expressions.
otherwise Subtag of <choose> that follows <when> tags and runs only if all of the prior conditions evaluated to ‘false’
param Adds a parameter to a containing ‘import’ tag’s URL.
redirect Redirects to a new URL.
remove Removes a scoped variable (from a particular scope, if specified).
set Sets the result of an expression evaluation in a ‘scope’
url Creates a URL with optional query parameters.
when Subtag of <choose> that includes its body if its condition evalutes to ‘true’

The examples helps you to walk through some common usages of JSTL core tags:

The following example shows you forEach loop:

Step 1 Create New Maven-based Dynamic Web Project

Step 1.1 Create New Project

Go to File->New->Others… Select Dynamic Web Project under Web category then click Next. Type JspDemo or whatever name you want in the Project name field.

Step 1.2 Configure Target Runtime

  • Click New Runtime… button under Target Runtime.
  • Select Apache Tomcat v7.0. Click Next.
  • Click Browse button to specify Tomcat Server installation directory. The select the folder then click Ok.
  • Click Installed JREs… button to add/remove or edit JRE definition. Remove existing one; then click Add button to configure new one. Select Standard VM; then click Next. Click Directory button to select the root folder ofJDK installation. Select the folder then click Ok. Click Finish.
  • Check the JDK then click Ok.
  • Change JRE to the JDK then click Finish.
  • Click Finish the finish the project creation.

Step 2.3 Convert The Project To Maven Project

Right click to the project; select Configure->Convert to Maven Project. Eclipse will generate Project Object Model (pom.xml) file. Open the file by Eclipse text editor then paste the content below.

Note: you can create normal java project and add jar files to your project build path. But it’s easier & faster using maven as maven is integrated with Eclipse IDE; so you don’t need to manually install it. 

Step 2 Create Java Bean Class

  • Right click to the project or src folder, select New -> Class from the menu.
  • Type User in name field and info.java.tips.to in Package field
  • Click Finish.
  • Add the following code to the file:

Note: simply copy the code above to clipboard, select src folder of your project in Eclipse IDE then press CTRL + V.

Step 3 Create New JSP File

  • Right click to the project or WebContent folder, select New -> JSP File from the menu.
  • You can choose any name, for example forEachDemo.jsp
  • Click Finish.
  • Eclipse IDE will generate some boilerplate code. We just clear all its content and replace with the followings code:

Step 4 Clean & Build

Clean & build the project to clean old classes file (if any) & force the IDE to recompile java code.

  • Go to Project on menu bar; select Clean…
  • Select Clean all projects (default)
  • Click Ok to proceed clean & build.

Step 5 Configure Tomcat

Select Servers window; in case it’s not there, go to Windows -> Show View-> Others… Select Server -> Servers; then click Ok.

If this is the first time you are creating a server, in the Servers area you will see a link as No servers are available click this link to create a new server. Click the link.

Define a New Server window will be opened, In Select the server type. Select Apache -> Tomcat v7.0 Server. Then click Finish.

Step 6 Deploy The Application

  • Right click to the Tomcat Server select Add and Remove.
  • Move the application (JSPDemo) from Available column to Configured. Then click on Finish to deploy it to tomcat server.

Step 7 Start Tomcat Server

Right click on the tomcat server and choose Start or simply click on the green start icon to start the server.

Step 8 Run The JSP

We assume that you install tomcat on default port (8080) and project name is JSPDemo. Open web browser and type:

http://localhost:8080/JspDemo/forEachDemo.jsp

Output


The following example shows you forTokens loop:

Step 1 Create New JSP File

Similarly create new JSP file named forTokensDemo.jsp

 Step 2 Run The JSP

http://localhost:8080/JspDemo/forTokensDemo.jsp

Output


The following example shows you chooseWhen loop:

Step 1 Create New JSP File

Similarly create new JSP file named chooseWhenDemo.jsp

Step 2 Run The JSP

http://localhost:8080/JspDemo/chooseWhenDemo.jsp

Output

Note: for JSP, you don’t need to Clean & Build the project or restart the tomcat server before running. You just change the code, save the changes and run.

For java classes, you need to Clean & Build the project to re-compile the code. In case register new servlet, change settings in web.xml or server.xml… you need to Clean & Build the project and restart tomcat server.