Today we will look into JSP custom tags. Sometimes avoiding scripting elements in JSP pages are not enough and we might get tempted to write java code to perform some operations in JSP page. Fortunately JSP is extendable an we can create our own custom tags to perform certain operations.
Lets take an example for creating custom tag to display today's date. This can be very useful for user to print today's date at any place in JSP page. So we want to create a custom tag like below -
<mytag:today/>
Now to create custom tags we need three things -
1. Tag Handler Class - In this class, we specify what our custom tag will do when it is used in JSP page.
2. TLD File - Tag Descriptor File is the file where we specify our tag name, tag handler class and tag attribute.
3. JSP Page - A JSP page where we will use our custom tag.
So first we'll create our Tag Handler Class -
Today.java
package com.anjan;
import java.util.Calendar;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;
public class Today extends TagSupport{
public int doStartTag() throws JspException {
JspWriter out=pageContext.getOut();//returns the instance of JspWriter
try{
out.print(Calendar.getInstance().getTime());//printing date and time using JspWriter
}catch(Exception e){
System.out.println(e);
}
return SKIP_BODY;//will not evaluate the body content of the tag
}
}
Now we'll create our TLD File where we'll specify our tag name, tag handler class and tag attribute.
customDemo.tld
<?xml version="1.0" encoding="UTF-8"?>
<taglib>
<tlib-version>1.0</tlib-version>
<jsp-version>1.2</jsp-version>
<uri>http://tomcat.apache.org/example-taglib</uri>
<tag>
<name>today</name>
<tag-class>com.anjan.Today</tag-class>
</tag>
</taglib>
Now we are done with the creation of our custom tags. Now we have to use the tags in JSP page.
index.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<%@ taglib uri="WEB-INF/cutomDemo.tld" prefix="mytag" %>
<title>Demo Page</title>
</head>
<body>
Today is : <mytag:today/>
</body>
</html>
Below is the final structure of our example project -
We'll get the below output when we test our project in browser -
Today is : Tue Oct 04 19:59:28 IST 2016
Lets take an example for creating custom tag to display today's date. This can be very useful for user to print today's date at any place in JSP page. So we want to create a custom tag like below -
<mytag:today/>
Now to create custom tags we need three things -
1. Tag Handler Class - In this class, we specify what our custom tag will do when it is used in JSP page.
2. TLD File - Tag Descriptor File is the file where we specify our tag name, tag handler class and tag attribute.
3. JSP Page - A JSP page where we will use our custom tag.
So first we'll create our Tag Handler Class -
Today.java
package com.anjan;
import java.util.Calendar;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;
public class Today extends TagSupport{
public int doStartTag() throws JspException {
JspWriter out=pageContext.getOut();//returns the instance of JspWriter
try{
out.print(Calendar.getInstance().getTime());//printing date and time using JspWriter
}catch(Exception e){
System.out.println(e);
}
return SKIP_BODY;//will not evaluate the body content of the tag
}
}
Now we'll create our TLD File where we'll specify our tag name, tag handler class and tag attribute.
customDemo.tld
<?xml version="1.0" encoding="UTF-8"?>
<taglib>
<tlib-version>1.0</tlib-version>
<jsp-version>1.2</jsp-version>
<uri>http://tomcat.apache.org/example-taglib</uri>
<tag>
<name>today</name>
<tag-class>com.anjan.Today</tag-class>
</tag>
</taglib>
Now we are done with the creation of our custom tags. Now we have to use the tags in JSP page.
index.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<%@ taglib uri="WEB-INF/cutomDemo.tld" prefix="mytag" %>
<title>Demo Page</title>
</head>
<body>
Today is : <mytag:today/>
</body>
</html>
Below is the final structure of our example project -
We'll get the below output when we test our project in browser -
Today is : Tue Oct 04 19:59:28 IST 2016
No comments:
Post a Comment