Introduction
|
A well-designed custom tag library will typically contain a focused set of interrelated tags that provide common or integrated functionality. For example, one library might contain a set of debugging tags for use during page development, while another might provide extensions to the standard JSP Bean tags for improved flow control.
CLICK HERE to download this complete example (zip file)
|
|
AcceptInput.html
|
-
<html>
-
<body>
-
<h2>Select a color of your choice </h2>
-
<font size=4 face="Verdana" color=#112244>
-
<form method="post" action="Hello.jsp">
-
<br>
-
<input type="radio" name="r1" value="G" checked="false">Green
-
<br>
-
<input type="radio" name="r1" value="Y" checked="false">Yellow
-
<br>
-
<input type="radio" name="r1" value="R" checked="false">Pink
-
<br>
-
<input type="radio" name="r1" value="B" checked="false">Blue
-
<br>
-
<p>
-
Enter Name <input type="type" name="uname">
-
<br><br>
-
<input type="Submit" value= "Submit" name="b1">
-
</p>
-
</form>
-
</body>
-
</html>
Download: AcceptInput.html
|
HelloTag.java
|
-
package hello;
-
import java.io.*;
-
import javax.servlet.jsp.*;
-
import javax.servlet.jsp.tagext.*;
-
import java.util.GregorianCalendar;
-
public class HelloTag extends BodyTagSupport
-
{
-
private String name;
-
private PageContext pageContext;
-
private Tag parent;
-
String color;
-
public HelloTag()
-
{
-
super();
-
}
-
public void setName(String name)
-
{
-
this.name = name;
-
}
-
public void setColor(String color)
-
{
-
this.color = color;
-
}
-
public int doAfterBody() throws JspException
-
{
-
try
-
{
-
BodyContent bc = getBodyContent();
-
String body = bc.getString();
-
JspWriter out = bc.getEnclosingWriter()
-
String dt;
-
GregorianCalendar now = new GregorianCalendar();
-
dt = now.getTime().toString();
-
String dt1 = dt.substring(11,16);
-
out.print("<body bgcolor="+color+">");
-
if(body != null)
-
out.print("<CENTER>Hi"+body+"! </CENTER>");
-
if(dt1.compareTo("12.00")<0)
-
out.print("<CENTER>Good Morning to You.</CENTER>");
-
if((dt1.compareTo("12.00")>0) && (dt1.compareTo("16.00")<0))
-
out.print("<CENTER>Good Afternoon to You.</CENTER>");
-
if((dt1.compareTo("16.00")>0) && (dt1.compareTo("24.00")<0))
-
out.print("<CENTER>Good Evening to You.</CENTER>");
-
out.print("<BR>");
-
out.print("<CENTER>Welcome to ABC Inc.</CENTER>");
-
out.print("<BR>");
-
out.print("<CENTER>The current time: "+dt1+"</CENTER>");
-
out.print("</body>");
-
}
-
catch(IOException ioe)
-
{
-
throw new JspException("Error:"+ioe.getMessage());
-
}
-
return SKIP_BODY;
-
}
-
}
Download: HelloTag.java
|
Mytaglib.tld
|
-
<taglib>
-
<tlib-version>1.0</tlib-version>
-
<jsp-version>1.2</jsp-version>
-
<short-name>first</short-name>
-
<uri></uri>
-
<info>A simple tag library for the examples</info>
-
<tag>
-
<name>hello</name>
-
<tag-class>hello.HelloTag</tag-class>
-
<body-content>JSP</body-content>
-
<attribute>
-
<name>color</name>
-
<rtexprvalue>true</rtexprvalue>
-
<required>true</required>
-
</attribute>
-
</tag>
-
</taglib>
Download: Mytaglib.tld
|
Hello.jsp
|
-
<%@ taglib uri="/Mytaglib.tld" prefix="first" %>
-
<HTML>
-
<HEAD>
-
<TITLE>Customized Tag</TITLE>
-
</HEAD>
-
<BODY>
-
<%
-
String str = request.getParameter("r1");
-
if(str.compareTo("G")==0)
-
{
-
%>
-
<first:hello color="LIGHTGREEN">
-
<%=request.getParameter("uname")%>
-
</first:hello>
-
<%
-
}
-
if(str.compareTo("R")==0)
-
{
-
%>
-
<first:hello color="RED">
-
<%=request.getParameter("uname")%>
-
</first:hello>
-
<%
-
}
-
if(str.compareTo("Y")==0)
-
{
-
%>
-
<first:hello color="LIGHTYELLOW">
-
<%=request.getParameter("uname")%>
-
</first:hello>
-
<%
-
}
-
if(str.compareTo("B")==0)
-
{
-
%>
-
<first:hello color="LIGHTBLUE">
-
<%=request.getParameter("uname")%>
-
</first:hello>
-
<%
-
}
-
%>
-
<BR>
-
<B>
-
</BODY>
-
</HTML>
Download: Hello.jsp
|
|
Click Next To Continue ...
|
|