Contents |
Servlet JSP J2EE and JSTL Versions
| Servlet | JSP | JSTL | J2EE | JDK Required | Release Date |
|---|---|---|---|---|---|
| 3.0 | 2.2 | 1.2 | 6 | Java 6 | December 2009 |
| 2.5 | 2.1 | 1.2 | 5 | Java 5 | September 2005 |
| 2.4 | 2.0 | 1.1 | 1.4 | Java 4 | November 2003 |
| 2.3 | 1.2 | 1.0 | 1.3 | Java 1.2 | August 2001 |
<ul> <% java.util.Enumeration en = pageContext.getAttributeNamesInScope(1); while (en.hasMoreElements()) { String key = (String) en.nextElement(); Object val = pageContext.getAttribute(key); out.print(String.format("<li><b>%s</b>: '%s'</li>", key, val)); } %> <ul>
Described here.
fn:length, to get the length of any Collection or String object. So I have a List object and I want to get its size, and I want to use EL to do it. If you're pre JSP 2.0 you can't do it without trickery. If you are JSP 2.0 or later, you're good to go, use the fn taglib to get the length of any Collection object.
My specific problem looked like this:
<c:when test="${results.size() == 0}">
That does not work, because size is not a bean method. By the way, I understand the reasoning, but I think it's not worth making EL less intuitive just to keep it "pure".
To get the length of a Collection or String object you'll need to use the functions tag length. To use this tag, you'll first need to include a tablib declaration for the functions taglib:
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>
Then you can write expressions like the following:
Here's what I used to get the length of my results collection.
<c:when test="${fn:length(results) == 0}">
Here's an example of an expression used to get the length of a String object.
string length = ${fn:length(name)}
Here are the articles I used to understand and solve my problem.
So you have this variable you declared in your JSP page, and you want to use it as an EL variable. Doing it is easy, but as usual, that how-to do it is hidden away in the spell books we call documentation, so you have to dig to get it. I found what I needed here, http://week.icxo.com/resin-doc/doc/jsp-el.xtp .
Here's an example.
In this example I have a list, bookList line 1, that I want to user later in the page.
I have included the <portlet:defineObjects /> tag, line 0, so that I get the pageContext object that I need.
I set an attribute on the pageContext object, line 2.
Then on line 3, I can reference the object using EL in my forEach tag.
<portlet:defineObjects /> // 0 <% List bookList = bookDao.getBooksByTitle("neverland"); // 1 pageContext.setAttribute("books", bookList); // 2 %> ... <c:forEach var="book" items="${books}"> // 3 <li>${book.title}</li> </c:forEach>
I was having a problem using the Expression Language (EL) feature in my JSP. It took me a while to find out that you have to use a specific version of the servlet spec and you must have the correct jars.
I ended up with 2 jars
jstl.jar
Manifest Attributes (Main)
Implementation-Vendor: Sun Microsystems, Inc.
Ant-Version: Apache Ant 1.6.1
Implementation-Title: JavaServer Pages Standard Tag Library API Reference Implementation
Implementation-Version: 1.1.2
Implementation-Vendor-Id: com.sun
Manifest-Version: 1.0
Specification-Title: JavaServer Pages Standard Tag Library (JSTL)
Created-By: 1.4.2-38 ("Apple Computer, Inc.")
Specification-Version: 1.1
Extension-Name: javax.servlet.jsp.jstl
and
standard.jar
Manifest Attributes (Main) Implementation-Vendor: Apache Software Foundation Ant-Version: Apache Ant 1.5.3 Implementation-Title: jakarta-taglibs 'standard': an implementation of JSTL Implementation-Version: 1.1.0 Implementation-Vendor-Id: org.apache Manifest-Version: 1.0 Specification-Title: JavaServer Pages Standard Tag Library (JSTL) Created-By: 1.4.2-b28 (Sun Microsystems Inc.) Specification-Version: 1.1 Extension-Name: org.apache.taglibs.standard
<%@ taglib uri="jstl-tags" prefix="c"%> <%@ page import="java.util.List" %> <%@ page import="java.util.ArrayList" %> <% List l = new ArrayList(); %> <% l.add("one"); %> <% l.add("two"); %> <% l.add("three"); %> <% l.add("four"); %> <% l.add("five"); %> <% l.add("six"); %> <% request.setAttribute("nums", l); %> <c:forEach var="num" items="${nums}"> <p>${num}</p> </c:forEach>
http://download.oracle.com/docs/cd/E17477_01/javaee/1.4/tutorial/doc/
Nice for prototyping tags or just being lazy.
<%@ taglib prefix="mytags" tagdir="/WEB-INF/tags" %>
<mytags:Foo />, in your JSP
Foo.tag
<%@ tag import="java.util.ArrayList" %> <%@ tag import="java.util.List" %> <% // do your thing %>
sample.jsp
<%@ taglib prefix="mytags" tagdir="/WEB-INF/tags" %> <p><mytags:Foo /></p>
Magic!
Here's a simple web.xml that includes a servlet mapping.
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5"> <display-name>Web App Description</display-name> <servlet> <servlet-name>MyServletName</servlet-name> <servlet-class>fully.qualified.servlet.ClassName</servlet-class> </servlet> <servlet-mapping> <servlet-name>MyServletName</servlet-name> <url-pattern>/myservlet</url-pattern> </servlet-mapping> </web-app>