Object Oriented Java

champak
₹2,000.00 ₹300.00
  • 27 students
  • 25 lessons
  • 0 quizzes
  • 10 week duration

Object Oriented Java

Making Custom Tags for a JSP Page

We have seen many HTML tags, used Java Beans.

Interested readers should go to this video and check it out.

Custom Tags are a further enhancement where we can create more functionality.

Let us outline the steps involved.

  1. Create a class that extends SimpleTagSupport.  The fully qualified name is javax.servlet.jsp.tagext.SimpleTagSupport; 
  2. Make a method called doTag().
  3. Create a tld(Tag Library Descriptor) file. This file will be placed inside WEB-INF. Enter your Custom Tag.
  4. Use it in a JSP a page.

 

A CustomTag


package tagspackage;

import java.io.IOException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.SimpleTagSupport;

public class TestTag extends SimpleTagSupport
{
@Override
public void doTag() throws IOException
{
JspWriter out =getJspContext().getOut();
out.println("A JSP Tag Output");
}
}

Contents of the TLD file

<?xml version="1.0" encoding="UTF-8"?>
<taglib version="2.1" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-jsptaglibrary_2_1.xsd">
  <tlib-version>1.0</tlib-version>
  <short-name>customtag</short-name>
  <uri>/tlds/customtag</uri>
 
   <tag>
    <name>TestTag</name>
    <tag-class>tagspackage.TestTag</tag-class>
    <body-content>scriptless</body-content>
  </tag>
 </taglib>

Use it in a JSP.


<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib prefix="tt" uri="/tlds/customtag" %>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Custom Tag</title>
    </head>
    <body>
        <tt:TestTag/>
    </body>
</html>

<%@taglib prefix=”tt” uri=”/tlds/customtag” %>
The taglib entry makes the Custom Tag available for use in the JSP.
The URI gives the path to the tld file. The tag will be accessed in this manner <tt:TestTag/> tt is the prefix and TestTag is the name from the tld entry.

 

Let us add an attribute now. The first thing you do is to add a setter.


package tagspackage;

import java.io.IOException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.SimpleTagSupport;

public class TestTag extends SimpleTagSupport
{
private int n=0;

public void setN(int n) {
this.n=n;
}


@Override
public void doTag() throws IOException
{
JspWriter out =getJspContext().getOut();
out.println("A JSP Tag Output Number = " + (2*n));
}
}

The attribute will have to be entered into the tld


<?xml version="1.0" encoding="UTF-8"?>
<taglib version="2.1" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-jsptaglibrary_2_1.xsd">
<tlib-version>1.0</tlib-version>
<short-name>customtag</short-name>
<uri>/tlds/customtag</uri>

<tag>
<name>TestTag</name>
<tag-class>tagspackage.TestTag</tag-class>
<body-content>scriptless</body-content>
<attribute>
<name>n</name>
<type>>Integer</type>
</attribute>
</tag>

</taglib>

 

Put it in a JSP.


<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib prefix="tt" uri="/tlds/customtag" %>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Custom Tag</title>
</head>
<body>
<tt:TestTag n="5"/>
</body>
</html>

 

 

The attribute can be made compulsory using the required attribute in the tld file.

[jxml]

<?xml version=”1.0″ encoding=”UTF-8″?>
<taglib version=”2.1″ xmlns=”http://java.sun.com/xml/ns/javaee” xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance” xsi:schemaLocation=”http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-jsptaglibrary_2_1.xsd”>
<tlib-version>1.0</tlib-version>
<short-name>customtag</short-name>
<uri>/tlds/customtag</uri>

<tag>
<name>TestTag</name>
<tag-class>tagspackage.TestTag</tag-class>
<body-content>scriptless</body-content>
<attribute>
<name>n</name>
<type>>Integer</type>
<required>True</required>
</attribute>
</tag>

</taglib>

[/xml]

 

Use <rtexprvalue>True</rtexprvalue>. If you want scriptlets to be allowed as input for attributes.


<?xml version="1.0" encoding="UTF-8"?>
<taglib version="2.1" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-jsptaglibrary_2_1.xsd">
<tlib-version>1.0</tlib-version>
<short-name>customtag</short-name>
<uri>/tlds/customtag</uri>

<tag>
<name>TestTag</name>
<tag-class>tagspackage.TestTag</tag-class>
<body-content>tagdependent</body-content>
<attribute>
<name>n</name>
<type>>Integer</type>
<required>True</required>
<rtexprvalue>True</rtexprvalue>
</attribute>
</tag>

</taglib>

 

 

<tt:TestTag n=”<%=3%>”/>

Finally, to use BodyTag.

tld


<?xml version="1.0" encoding="UTF-8"?>
<taglib version="2.1" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-jsptaglibrary_2_1.xsd">
<tlib-version>1.0</tlib-version>
<short-name>customtag</short-name>
<uri>/tlds/customtag</uri>

<tag>
<name>TestTag</name>
<tag-class>tagspackage.TestTag</tag-class>
<body-content>tagdependent</body-content>
<attribute>
<name>n</name>
<type>>Integer</type>
<required>True</required>
<rtexprvalue>True</rtexprvalue>
</attribute>
</tag>

</taglib>

Tag


package tagspackage;

import java.io.IOException;
import java.io.StringWriter;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.SimpleTagSupport;

public class TestTag extends SimpleTagSupport
{
private StringWriter sw = new StringWriter();

private int n=0;

public void setN(int n) {
this.n=n;
}


@Override
public void doTag() throws IOException ,JspException
{
JspWriter out =getJspContext().getOut();
out.println("A JSP Tag Output Number = " + (2*n));
getJspBody().invoke(sw);
String bodytext="" + sw;
out.println("<h1>" + bodytext + "</h1>");
}
}

JSP


<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib prefix="tt" uri="/tlds/customtag" %>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Custom Tag</title>
</head>
<body>
<tt:TestTag n="<%=3%>">This is Body</tt:TestTag>
</body>
</html>

 

 

end

Leave a Reply

Your email address will not be published. Required fields are marked *