The Simplest Way to Use JSP Tag

The Hello, World example is extracted from samples of tomcat, the other is not.

Hello, World

(Code can be found in attachment

It's stupid and simple. You just need to do:

  1. Create a tag file, here names helloworld.tag locates in /WEB-INF/tags, and write any word as you like, here is "Hello, world!"
  2. Declare the directory path of tag files /WEB-INF/tags: <%@ taglib prefix="hw" tagdir="/WEB-INF/tags" %>
  3. Use the tag: to insert this line: <hw:helloworld/>

A bit more complex tag file

(Code can be found in attachment

What a dull matter if a tag file contains only plain text! Actually, it can use HMTL element, its own attribute, and so on.

Just define another tag file pinfo.tag in /WEB-INF/tags, and put the following content to it:

<%@ tag language="java" pageEncoding="ISO-8859-1"%>
<%@ attribute name="district" %>
<%@ attribute name="email" %>
<%@ attribute name="phone" %>
<br/>I live in ${district},
<br/>my email is ${email},
<br/>and ${phone} is my cell phone number.

Add the below to the JSP file:

<hw:pinfo district="Shanghai, China" phone="12345678901" email=""/>

All work has been done. The output to user browser is:

I live in Shanghai, China,
my email is,
and 12345678901 is my cell phone number.