tagged: jsp

JSP: Create a tag file

Put this in your WEB-INF/tags folder. It's called box.tag:
<%@ attribute name="colour" required="true" rtexprvalue="true" %>
<div style="background-color: ${colour}">
	<jsp:doBody />

Note the attribute directive. It's saying the tag requires a parameter called 'color' and we can add in run-time expressions if we won't (i.e. not static text only).
You can also pass a 'type' to the attribute tag. This is for when you're passing an object, not a String. It must be the fully qualified class name.
You can pass dynamic attributes, too. This means any attribute you specify on the calling tag will end up in a list of dynamic attributes that you can iterate over in your tag file:
<%@ tag dynamic-attributes="dynattrs" %>
<c:forEach var="a" items="${dynattrs}">

Now you're specifying, in the 'tag' directive, that all the dynamic attributes will be in a property called ${dynattrs}.
Now reference this from your JSP file:
<%@ taglib tagdir="/WEB-INF/tags" prefix="mytag" %> 
<mytag:box colour="#ccc" something="dynamic">

Note the taglib directive has the taglib attribute that points to the directory where our box.tab file is. We give this a prefix.
Then we call a xml like tag, with the name above as the namespace, followed by the name of the tab. We add in the required attribute 'color'. And the content between the tags.
We could also use this more verbose version:
<%@ taglib tagdir="/WEB-INF/tags" prefix="mytag" %> 
	<jsp:attribute name="color">#ccc</jsp:attribute>
	<jsp:body>hi ho</jsp:body>


JSP: If else in JSTL

And if statement in JSTL is:
<c:if test="${true}">

And if-else is actually choose-when-otherwise:
	<c:when test="${true}">

Don't forget to include the jstl taglib.

jsp jstl

JSP: Setup JSTL and EL in Tomcat 7

If you want to use JSTL (and you do) instead of scriptlets you first have to include it in your war file or your tomcat lib file.
I imported it into my project using Gradle. You should see the jar file in the created WAR file once you're using it.
Now you can use things like:
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> 
<c:forEach var="p" items="${applicationScope['products'].values().iterator()}">

	${p.name} | ${p.getPrice()} || <a href="Cart?add=true&id=${p.getId()}">add</a>
	<br />


Note you don't need the scriptlet %@ page import.. directives anymore. You do need thee taglib directive to point to the jstl core library.
Note we're using the JSP EL, expression language, syntax to call method and attribute (p.name is the same as p.getName()). EL comes with Tomcat 7 - no additional libraries are required.
As with scriplets there are implicit objects in EL, applicationScope, in this example. 'applicationScope['products'] is the same as applicationScope.getAttribute('products').

jsp jstl tomcat

JSP: Creating a custom EL function

Note I'm creating this custom EL tag in Tomcat 7.
First make a class with a static method like so:
package your.package;

public class Functions {
	public static String hello(String name) {
		return "Hiya, " + name + ".";


Then make a file called mytaglib.tld in WEB-INF/tags/:
<?xml version="1.0" encoding="ISO-8859-1" ?>
<taglib xmlns="http://java.sun.com/xml/ns/j2ee" 
  xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/javaee/web-jsptaglibrary_2_1.xsd" 

      <function-signature>java.lang.String hello(java.lang.String)</function-signature>


Note the function part, which specified the name of the new EL function, what class it resides in and its function signature.
The uri would be used if we were accessing this directly, but instead we'll be accessing the url from the web.xml below:
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"


Note we're pointing to the mytaglib.tld file just created. And taglib-uri is how we'll refer to it in the JSP:
<%@ taglib uri="http://some.thing/mine" prefix="a" %> 



JSP: Changing the page encoding to UTF-8

Put this in your web.xml to change the page encoding:

Now any jsp file you render, from a servlet or wherever, will have the UTF-8 charset in its response headers, i.e:


Page 1 of 2