Navigating complex data structures using JSTL

This is a simple example showing how to navigate through a complex data structure of inner objects using JSTL.

InnerJSTLTest.jsp looks like this:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
		<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
			<title>Inner JSTL Test</title>
		<%@ taglib uri="" prefix="c"%>
		<table width="100%" class="table">
					<b>JSTL Iteration</b>

					<table width="100%" class="table" cellpadding="0"
						cellspacing="5" border="0" bgcolor="#FFFFFF">
						<c:set var="listsize" value="1" scope="page" />
						<c:forEach var="arrayElement" items="${messageVO.outerArrayList}"
							<c:set var="currentlistindex" value="${listsize.count}"
								scope="page" />
							<br />
								<td width="20%">
											<c:out value="${currentlistindex}" />
									<br />
									<c:forEach var="outerMapElement" items="${arrayElement}">
										<b>Sub-Section: ${outerMapElement.key}</b>
										<br />
										<c:set var="flag" value="0" scope="page" />
										<c:forEach var="innerMapElement" items="${outerMapElement.value}"
											<u>Sub-Sub-Section: ${innerMapElement.key}</u>
											<br />
											<c:forEach var="innerArrayElement" items="${innerMapElement.value}">
												<c:forEach var="elem" items="${innerArrayElement}">
													<br />
											<c:set var="flag" value="${innermapsize.count}" scope="page" />
										<c:if test='${flag== 0}'>
											<p style="color:#009900">
												<b>EMPTY SUB SECTION!</b>
									<hr />

</html> looks like this:

package com.mattiz.jstlTest;


import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class JSTLTestServlet extends HttpServlet {
	public void doPost(HttpServletRequest req, HttpServletResponse res) {
		try {
			JSTLTestVO jstlTestVO = new JSTLTestVO();
			req.setAttribute("messageVO", jstlTestVO);
					.forward(req, res);
		} catch (ServletException e) {
			// TODO Auto-generated catch block
		} catch (IOException e) {
			// TODO Auto-generated catch block

	public void doGet(HttpServletRequest req, HttpServletResponse res) {
		doPost(req, res);
} which builds up the data structure and makes it available to the jsp looks like this

package com.mattiz.jstlTest;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class JSTLTestVO {

	private List outerArrayList;

	public List getOuterArrayList() {
		return populateVO();

	public void setOuterArrayList(List outerArrayList) {
		this.outerArrayList = outerArrayList;

	public List populateVO() {
		List innerList1 = new ArrayList();
		innerList1.add("Java Rocks");
		innerList1.add("Down With M$");
		innerList1.add("Ever Tried Java?");
		innerList1.add(".NET sucks");
		innerList1.add("Make Java while the SUN shines");
		innerList1.add("Java has Everything Under The SUN!");
		innerList1.add("God Bless the SUN");
		innerList1.add("Java rules!");
		List innerList2 = new ArrayList();
		innerList2.add("IBM= I Blame Microsoft!");
		innerList2.add("MAC = Most Applications Crash!");
		innerList2.add("MAC= Most Applications Crash Or System Hangs!");
		List innerList3 = new ArrayList();
		innerList3.add("Good Day!");
		innerList3.add("Guten Tag!");
		innerList3.add("Au Revoir!");
		List innerList4 = new ArrayList();
		innerList4.add("Damn It!");
		innerList4.add("It sucks!");
		Map innermap1 = new HashMap();
		innermap1.put("Java", innerList1);
		Map innermap2 = new HashMap();
		innermap2.put("Acronyms", innerList2);
		innermap2.put("Greetings", innerList3);
		Map innermap3 = new HashMap();
		Map outerMap1 = new HashMap();
		outerMap1.put("Geek Tongue", innermap1);
		outerMap1.put("Mother Tongues", innermap2);
		Map outerMap2 = new HashMap();
		outerMap2.put("Empty?", innermap3);
		List outerArrayList = new ArrayList();
		return outerArrayList;

web.xml looks like this for starters:

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4"
	xmlns="" xmlns:xsi=""

The URL to run the app is


The output looks something like this:

JSTL Iteration
Section  1
Sub-Section:  Mother Tongues 
Sub-Sub-Section:  Greetings
Good Day!
Guten Tag!
Au Revoir!
Sub-Sub-Section:  Acronyms
IBM= I Blame Microsoft!
MAC = Most Applications Crash!
MAC= Most Applications Crash Or System Hangs!
Sub-Section:  Geek Tongue
Sub-Sub-Section:  Java
Java Rocks
Down With M$
Ever Tried Java?
.NET sucks
Make Java while the SUN shines
Java has Everything Under The SUN!
God Bless the SUN
Java rules!
Section  2
Sub-Section:  Empty?

About cuppajavamattiz
Matty Jacob - Avid technical blogger with interests in J2EE, Web Application Servers, Web frameworks, Open source libraries, Relational Databases, Web Services, Source control repositories, ETL, IDE Tools and related technologies.

Comments are closed.

%d bloggers like this: