JSTL & XPath

JSTL stands for JSP (Java/Jakarta Server Pages) Standard Tag Library. It is a large library for tools commonly used, like databases and iterators, in one language, or library of tags. The JSTL is organized into functional categories with separate URIs. Oracle provides a graphical overview of the sub-libraries, their functions, and prefixes: [1]

A table featuring a header of Area - Subfunctions - PrefixCore: prefix- c; Variable support, Flow control, URL management, Misc. XML: prefix- x; Core, Flow control, Transformation. I18N: prefix- fmt; Locale, Message formatting, Number & date format.

Table 7-1 from The Java EE 5 Tutorial on Oracle.com

The syntax of the sub libraries resembles html/xml/jsp element tags, but they provide access to programming logic within the display. A simple but common example are the tags <c:set var = “myVariable” value=”VariableValue” || “${expression}” scope = “session”/> and <c:out value=”${myVariable}”/>. These are simple to import as well, needing one link and one prefix per sub-library. [2]

       XPath is part of the XSLT library or family. XSLT is a language for transforming XML, while XPath is specifically for navigating them. They can be used with many languages, including Java, JavaScript, Python, PHP, and more. The XPath gives more power to the XSLT by allowing the selection of different elements/nodes [3].

       While XPath is part of the XLST family, it has also been incorporated into the power of JSTL by way of the x prefix and the XML sub-library. The <x:out select=”xPathStatement”/> tag takes an XPath location/expression and displays it on the page, similar to a scriptlet expression tag (<%=) . To demonstrate the ease of use, I have prepared an example. Please see example3 of the attached module 9 war file for a full copy of code used for examples.

        First the environment must be prepared. There are necessary jar downloads for using JSTL. Note the read-me.bin file in the initial download of JSTL, some declaring outside dependencies for the XML library from Xalan. The jars should be placed somewhere safe for probable re-use and they should be inserted the web projects WEB-INF/lib folder. This instance required both xalan and serializer jars in addition to JSTL jars.  After the source files are sorted in the application, the libraries can be imported into a JSP page as follows:

JSP taglib declaration snippet for Tomcat 9 and below allowing Java EE.

NOTE: This code snippet only works for Java EE or Tomcat 9 or prior. The newer version of JSP was renamed Jakarta EE to separate it from the core Java language. The tag libraries have a different address under Jakarta for Tomcat 9 and above.

With this example, there is a custom XML (example3.xml) creating tags for a bus yard system. If I needed to locate a specific bus or driver to send a message when something occurred, we could use internal xml data to help. First we use the core library to import our xml (line 20). Then we parse it to remove the content from the tag body (line 21) [4]. After parsing, we can use <x:forEach>  method by giving it our desired element or category via select=”XPathExpression”, or in this case, the school bus unit (line 32) [5].

A snippet of the JSP code to display a custom tag.

To access the tag you created, use your file names in the formula expression on line 24 above.

Finally, to display the retrieved value, use select with “$forEachVar”. This results in the following output, with no source organization information visible in the displayed jsp file or upon ‘inspect source’, respectively.

List of drivers to alert from custom tag created.

While XPath was intended to bolster XSLT, it can also lend its logical selection method/language to other sources like the JSTL XML library due to their similar focus on XML. This makes the XML file more accessible to many languages.

Sources

[1] The Java EE 5 Tutorial, https://docs.oracle.com/javaee/5/tutorial/doc/bnake.html

[2] https://www.tutorialspoint.com/jsp/jstl_core_set_tag.htm

[3] https://www.w3schools.com/xml/xpath_syntax.asp

[4] https://www.codejava.net/java-ee/jstl/jstl-xml-tag-parse#google_vignette

[5] https://www.tutorialspoint.com/jsp/jstl_xml_foreach_tag.htm

Previous
Previous

What are JSF Facelets?

Next
Next

JSP and the Request & Response Objects