Easy learning with example program codes

Custom tag with attributes

Custom tags:

Custom tags are the user defined tags. These tags are mainly used for code re-usability. We can define a custom tag with any number of attributes. Let us discuss it with the below example.


import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.SimpleTagSupport;
 * This class is used for defining a custom tag with attributes.
 * @author javawithease
public class CustomTagWithAttribute extends SimpleTagSupport{
	//tag attribute
	private int num;
	public void doTag() throws JspException, IOException {
      JspWriter out = getJspContext().getOut();
    	  out.println("Square of " + num + " = " + num * num);
      }catch(Exception e){
	public void setNum(int num) {
		this.num = num;


	 <short-name>Our first custom tag</short-name>


<%@ taglib prefix="stag" uri="WEB-INF/squaretag.tld"%>
		<title>custom tag with attribute example</title>
		<stag:squareTag num="25"/>




jsp example 68
Download this example.
Next Topic: Struts Tutorial with examples.
Previous Topic: Steps to create Custom tags with body in jsp with example.

Related Topics:

JSTL (JSP Standard Tag Library) with example.
JSTL Core Tags with example.
JSTL Formatting Tags with example.
JSTL Functions with example.
Steps to create Custom tags with body in jsp with example.
Custom tag with attributes.


Industrial Training

We offers Placement Oriented Training on Java, Spring, JSF, Hibernate, PHP, AngularJS, Angular 4, PLSQL, Oracle BI Publisher etc. We also provides Online training, please mail us at


We also provides the Development services for Website Development , Java Development, PHP Development, Android App Development etc. You can contact us on

Copyright © 2019 CodesJava Protection Status SiteMap Reference: Java Wiki