Tech notes for java-jsp



  1. JSP: Create a tag file
  2. JSP: If else in JSTL
  3. JSP: Creating a custom EL function
  4. JSP: Setup JSTL and EL in Tomcat 7
  5. JSP: Actions basics
  6. JSP: Changing the page encoding to UTF-8
  7. JSP: Scriptlet basics

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 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>
java-jsp java

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.

java-jsp java-jstl java

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="" 

	      <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:



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" %> 

java-jsp java

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="" prefix="c" %> 
<c:forEach var="p" items="${applicationScope['products'].values().iterator()}">

	${} | ${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 ( 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').

java-jsp java-jstl java-tomcat java

JSP: Actions basics

Actions are marginally nicer versions of scriptlets, at least some actions that you can perform in scriptlets.

	<jsp:include page="some.jsp">
		<jsp:param name="hi" value="there />

This will include a file like scriptlet, but it's run at runtime, so the page value can be dynamically created.

You can import a Java bean (a getter and setter object with an empty constructor).

	<%@ page import="thing.Hello" %>
	<jsp:useBean id="bean" class="thing.Hello" />

The actual bean is this is simple enough:

	package thing;
	public class Bean {

		private String thing = "";

		public String getThing() {
			return thing;

		public void setThing(String thing) {
			this.thing = thing;

Now you can access it using a scriptlet using the variable 'bean'. You can also use the getProperty and setProperty action:

	<jsp:setProperty name="bean" property="thing" value="SAAATTAAAN" />

	<jsp:getProperty name="bean" property="thing" />

You can also use the getProperty and setProperty actions to manipulate request parameters. Below we set the object based on the URL parameter other:

	<jsp:setProperty name="bean" property="thing" param="other" />

If you omit the 'param' part, we'll set the thing property via thing in the URL,, for example. Or if we put a * in the property we'll set the entire bean via parameter properties.

You can also forward pages to another page, and pass parameters there:

	<% if(bean.getThing().equals("SAAAATAAAAAN")) { %>
		<jsp:forward page="e.jsp">
			<jsp:param name="hi" value="X" />
	<% } %> 
java java-jsp

Page 1 of 2
Click me