JSP Custom Tag

A custom tag is a user-defined JSP language element. When a JSP page containing a custom tag is translated into a servlet, the tag is converted to operations on a tag handler. The web container then invokes those operations when the JSP page’s servlet is executed.

Custom tags have a rich set of features. They can

  • Be customized by means of attributes passed from the calling page.
  • Pass variables back to the calling page.
  • Access all the objects available to JSP pages.
  • Communicate with each other. You can create and initialize a JavaBeans component, create a public EL variable that refers to that bean in one tag, and then use the bean in another tag.

Be nested within one another and communicate by means of private variables.

Example 1 TagSupport Without Body

Step 1 Create New Maven-based Dynamic Web Project

Step 1.1 Create New Project

Go to File->New->Others… Select Dynamic Web Project under Web category then click Next. Type JspDemo or whatever name you want in the Project name field.

Step 1.2 Configure Target Runtime
  • Click New Runtime… button under Target Runtime.
  • Select Apache Tomcat v7.0. Click Next.
  • Click Browse button to specify Tomcat Server installation directory. The select the folder then click Ok.
  • Click Installed JREs… button to add/remove or edit JRE definition. Remove existing one; then click Add button to configure new one. Select Standard VM; then click Next. Click Directory button to select the root folder of JDK installation. Select the folder then click Ok. Click Finish.
  • Check the JDK then click Ok.
  • Change JRE to the JDK then click Finish.
  • Click Finish the finish the project creation.

Step 2 Create New JSP File

  • Right click to the project or WebContent folder, select New -> JSP File from the menu.
  • You can choose any name, for example tagSupportWithoutBodyDemo.jsp
  • Click Finish.
  • Eclipse IDE will generate some boilerplate code. We just clear all its content and replace with the following code:

Step 3 Create Tag Library Descriptor

  • Right click to WEB-INF folder; select New -> Other…
  • Select File under General category then click Next.
  • Enter tagSupportDemo.tld in File Name field.
  • Click Finish.
  • Open the file by Eclipse text editor (click on Source tab). Then paste the content as below:

Step 4 Create Tag Handler Class

  • Right click to the project or src folder, select New -> Class from the menu.
  • Type ClassicTagNoBody in name field and info.java.tips.tag.handler in Package field
  • Click Finish.
  • Add the following code to the file:

Note: simply copy the code above to clipboard, select src folder of your project in Eclipse IDE then press CTRL + V.

Step 5 Clean & Build

Clean & build the project to clean old classes file (if any) & force the IDE to recompile java code.

  • Go to Project on menu bar; select Clean…
  • Select Clean all projects (default)
  • Click Ok to proceed clean & build.

Step 6 Configure Tomcat

Select Servers window; in case it’s not there, go to Windows -> Show View-> Others… Select Server -> Servers; then click Ok.

If this is the first time you are creating a server, in the Servers area you will see a link as No servers are available click this link to create a new server. Click the link.

Define a New Server window will be opened, In Select the server type. Select Apache -> Tomcat v7.0 Server. Then click Finish.

Step 7 Deploy The Application

  • Right click to the Tomcat Server select Add and Remove.
  • Move the application (JspDemo) from Available column to Configured. Then click on Finish to deploy it to tomcat server.

Step 8 Start Tomcat Server

Right click on the tomcat server and choose Start or simply click on the green start icon to start the server.

Step 9 Run The JSP

We assume that you install tomcat on default port (8080) and project name is JSPDemo. Open web browser and type:

http://localhost:8080/JspDemo/tagSupportWithoutBodyDemo.jsp

Output

Example 2 TagSupport With Body

Step 1 Create New JSP File

  • Right click to the project or WebContent folder, select New -> JSP File from the menu.
  • You can choose any name, for example tagSupportWithBodyDemo.jsp
  • Click Finish.
  • Eclipse IDE will generate some boilerplate code. We just clear all its content and replace with the followings code:

Step 2 Create Tag Library Descriptor

  • Right click to WEB-INF folder; select New -> Other…
  • Select File under General category then click Next.
  • Enter tagSupportDemo2.tld in File Name field.
  • Click Finish.
  • Open the file by Eclipse text editor (click on Source tab). Then paste the content as below:

Step 3 Create Tag Handler Class

  • Right click to the project or src folder, select New -> Class from the menu.
  • Type ClassicTagWithBody in name field and info.java.tips.tag.handler in Package field
  • Click Finish.
  • Add the following code to the file:

 

Step 4 Run The JSP

  • Stop the tomcat server
  • Clean & build the project to re-compile the application
  • Start the tomcat server
  • We assume that you install tomcat on port 8080 and context path is JspDemo. By default project name is same as context path. Open web browser and run the below URL:

http://localhost:8080/JspDemo/tagSupportWithBodyDemo.jsp

 

Output

Example 3 SimpleTagSupport Without Body

Step 1 Create New JSP File

  • Right click to the project or WebContent folder, select New -> JSP File from the menu.
  • You can choose any name, for example simpleTagWithoutBodyDemo.jsp
  • Click Finish.
  • Eclipse IDE will generate some boilerplate code. We just clear all its content and replace with the following code:

Step 2 Create Tag Library Descriptor

  • Right click to WEB-INF folder; select New -> Other…
  • Select File under General category then click Next.
  • Enter simpleTagSupportDemo.tld in File Name field.
  • Click Finish.
  • Open the file by Eclipse text editor (click on Source tab). Then paste the content as below:

Step 3 Create Tag Handler Class

  • Right click to the project or src folder, select New -> Class from the menu.
  • Type SimpleTagNoBody in name field and info.java.tips.tag.handler in Package field
  • Click Finish.
  • Add the following code to the file:

 

Step 4 Run The JSP

  • Stop the tomcat server
  • Clean & build the project to re-compile the application
  • Start the tomcat server
  • We assume that you install tomcat on port 8080 and context path is JspDemo. By default project name is same as context path. Open web browser and run the below URL:

http://localhost:8080/JspDemo/simpleTagWithoutBodyDemo.jsp

Note: for JSP, you don’t need to Clean & Build the project or restart the tomcat server before running. You just change the code, save the changes and run.

For servlet, you need to Clean & Build the project to re-compile the code. In case register new servlet, you need to restart tomcat server.

Output

Example 4 SimpleTagSupport with body

Step 1 Create New JSP File

  • Right click to the project or WebContent folder, select New -> JSP File from the menu.
  • You can choose any name, for example simpleTagWithBodyDemo.jsp
  • Click Finish.
  • Eclipse IDE will generate some boilerplate code. We just clear all its content and replace with the following code:

Step 2 Create Tag Library Descriptor

  • Right click to WEB-INF folder; select New -> Other…
  • Select File under General category then click Next.
  • Enter simpleTagSupportDemo2.tld in File Name field.
  • Click Finish.
  • Open the file by Eclipse text editor (click on Source tab). Then paste the content as below:

Step 3 Create Tag Handler Class

  • Right click to the project or src folder, select New -> Class from the menu.
  • Type SimpleTagWithBody in name field and info.java.tips.tag.handler in Package field
  • Click Finish.
  • Add the following code to the file:

 

Step 4 Run The JSP

  • Stop the tomcat server
  • Clean & build the project to re-compile the application
  • Start the tomcat server
  • We assume that you install tomcat on port 8080 and context path is JspDemo. By default project name is same as context path. Open web browser and run the below URL:

http://localhost:8080/JspDemo/simpleTagWithBodyDemo.jsp

Note: for JSP, you don’t need to Clean & Build the project or restart the tomcat server before running. You just change the code, save the changes and run.

For servlet, you need to Clean & Build the project to re-compile the code. In case register new servlet, you need to restart tomcat server.

Output