The new JavaServer Pages (JSP) version incorporates the expression language (EL) introduced by the JSP Standard Tag Library (JSTL) to let Web designers produce scriptless JSP pages that don't contain Java code. Since JSP 2.0 provides backward compatibility to JSP 1.x, you may still include Java snippets in your pages, but tag handlers and JavaBean components are much better places for Java-based functionality.
JSP 2.0 provides new features for tag handlers such as dynamic attributes, the Simple Invocation Protocol, and .tag files. You still use the old JSP 1.0 standard actions for creating JavaBean instances and setting their properties, but now
you can access bean properties, request parameters, and JSP attributes/variables with the new expression language.
All those JSP technology improvements let you achieve the goal of separating the JSP/HTML markup from the Java code. One thing is missing, however. JSP 2.0 has no syntax for calling a public nonstatic JavaBean method from a scriptless JSP page. This article solves that issue by providing a JSP 2.0 simple tag with dynamic attributes.
Note: You can download this article's source code from Resources.
Suppose you have a java.util.List instance you must present as an HTML list. Here is a quick solution based on JSP 1.x:
<UL>
<%
Iterator iterator = list.iterator();
while (iterator.hasNext()) {
Object elem = iterator.next();
if (elem == null)
elem = "";
%>
<LI> <%= elem %> </LI>
<%
}
%>
</UL>
Existing JSP-based Web applications consist of Java code mixed with HTML markup like the above code fragment. Maintaining hundreds of pages like that can be a nightmare if you have separate Java development and Web design teams. The solution is to move the Java code into tag libraries so that developers can do their jobs without pasting Java code within Webpages and designers can edit their Webpages without worrying about breaking the Java code.
However, JSP 1.x has several problems that don't let you easily develop scriptless JSP pages. Until recently, no standard method existed for accessing Java objects from a JSP page without using Java code. In addition, coding tag handler classes wasn't as simple as it could have been.
The following lines of code are based on JSTL 1.0, which can be used with JSP 1.2. The <c:forEach> tag iterates over the elements of the given list and exports the elem variable for each element. Instead of declaring elem as a local variable, the <c:forEach> tag creates a page attribute with pageContext.setAttribute(). This attribute's value is printed with JSTL's <c:out> tag:
<UL>
<c:forEach var="elem" items="${list}">
<LI> <c:out value="${elem}"/> </LI>
</c:forEach>
</UL>
JSTL provides standard tags for processing XML documents and accessing relational databases along with formatting tags, internationalization tags, conditional tags, iterator tags, URL-related tags, and other general-purpose tags. JSTL has solved many of JSP 1.x's problems with the help of an expression language that allows you to access Java objects from JSP pages without using Java code. For example, instead of looking for an attribute or accessing a request parameter with:
DHZGJYTREWMFXSGT HDRGRFBy Anonymous on August 5, 2010, 11:14 amabercrombie uk abercrombie and fitch uk abercrombie fitch uk Abercrombie & fitch uk L?K?L?‘????’L?K?O?KKNNKLL?
Reply | Read entire comment
Great help reallyBy Anonymous on November 2, 2009, 4:23 amThanks a lot from Bahata. This article is the first one that could help me address the problem of calling instance methods of JavaBeans using scriptless jsp.
Reply | Read entire comment
my tag call for parameterized methodBy Anonymous on October 24, 2009, 9:06 amNice article! people will come to you to find better approaches.
Reply | Read entire comment
Superb articleBy Anonymous on August 29, 2009, 10:51 amThis is really superb article. Very very informative. It solved my problem which i was trying to solve since some hours. Thaks a ton Andrei.
Reply | Read entire comment
Superb articleBy Anonymous on August 29, 2009, 10:51 amThis is really superb article. Very very informative. It solved my problem which i was trying to solve since some hours. Thaks a ton Andrei.
Reply | Read entire comment
View all comments