• October 8, 2014
  • JSP
  • Comments Off on Custom tag with attributes

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 java.io.IOException;
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.


Comments are closed.