A tag handler class is basically a JavaBeans class, it should contain set of set methods and set of get methods.
The legal values which are returned by doStartTag are:
doStartTag () method will be called by JSP container when starting of custom tag taken place <x:hello/>. SKIP_BODY is the constant which will be returned by doStartTag () method when we don't want to execute body of the custom tag. EVAL_BODY_INCLUDE is the constant which will be returned by doStartTag () method when we want to evaluate body of the custom tag.
The legal values which are returned by doEndTag () are:
doEndTag () method will be called by JSP container when the closing tag of custom tag taken place <x:hello/>. EVAL_PAGE is the constant to be returned by doEndTag () method when we want to execute rest of the JSP page. SKIP_PAGE is the constant to be returned by doEndTag () method when we don't want to execute rest of the JSP page.
Develop a JSP page which illustrate the concept of TagSupport class, mean while get connection with database using custom tag?
Answer:
<web-app> </web-app>
<html> <body> <%@ taglib prefix="table" uri="/WEB-INF/tlds/JdbcTag.tld" %> <center> <table:show username="scott" password="tiger" dsn="oradsn" table="product"> </center> </body> </html>
package tagpack; import javax.servlet.jsp.*; import javax.servlet.jsp.tagext.*; import java.sql.*; import java.io.*; public class JdbcTag extends TagSupport { String table, username, password, dsn; public void setUsername(String username) { this.username = username; } public void setPassword(String password) { this.password = password; } public void setDsn(String dsn) { this.dsn = dsn; } public void setTable(String table) { this.table = table; } Connection con = null; PreparedStatement ps = null; ResultSet rs = null; ResultSetMetaData rsmd = null; public int doStartTag() throws JspException { try { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); con = DriverManager.getConnection("jdbc:odbc:" + dsn, username, password); ps = con.prepareStatement("select * from " + table); return EVAL_BODY_INCLUDE; } catch (Exception e) { throw new JspException(e.getMessage()); } } public int doEndTag() throws JspException { int i = 1; try { rs = ps.executeQuery(); rsmd = rs.getMetaData(); pageContext.getOut().print("<table border=1><tr>"); for (i = 1; i <= rsmd.getColumnCount(); i++) { pageContext.getOut().print("<th>" + rsmd.getColumnName(i) + "</th>"); } pageContext.getOut().print("</tr>"); while (rs.next()) { pageContext.getOut().print("<tr>"); for (i = 1; i <= rsmd.getColumnCount(); i++) { pageContext.getOut().print("<td>" + rs.getString(i) + "</td>"); } pageContext.getOut().print("</tr>"); } pageContext.getOut().print("</table>"); return EVAL_PAGE; } catch (Exception e) { throw new JspException(e.getMessage()); } } };
<taglib> <shortname>table</shortname> <tag> <name>show</name> <tagclass>tagpack.JdbcTag</tagclass> <bodycontent>empty</bodycontent> <attribute> <name>username</name> <required>true</required> <rtexprvalue>false</rtexprvalue> </attribute> <attribute> <name>password</name> <required>true</required> <rtexprvalue>false</rtexprvalue> </attribute> <attribute> <name>dsn</name> <required>true</required> <rtexprvalue>false</rtexprvalue> </attribute> <attribute> <name>table</name> <required>true</required> <rtexprvalue>false</rtexprvalue> </attribute> </tag> </taglib>
Note: We should write the attributes same in the order of jsp program we used.
Develop a JSP page which illustrate the concept of TagSupport class, mean while print the typed name on the browser using custom tag?
Answer:
<web-app> </web-app>
<%@ taglib uri="/WEB-INF/tlds/hello.tld" prefix="test" %> <html> <h3>This is example on custom tag</h3> <h4><test:hello name="Hyderabad"/></h4> </html>
package t1; import java.io.*; import javax.servlet.jsp.*; import javax.servlet.jsp.tagext.*; public class HelloTag extends TagSupport { private String name; public void setName(String name) { this.name = name; } public int doEndTag() throws JspException { try { pageContext.getOut().print("Hello..! " + name); } catch (Exception e) { throw new JspException(e.getMessage()); } return EVAL_PAGE; } };
<taglib> <shortname>test</shortname> <tag> <name>hello</name> <tagclass>t1.HelloTag</tagclass> <bodycontent>empty</bodycontent> <attribute> <name>name</name> <required>true</required> <rtexprvalue>false</rtexprvalue> </attribute> </tag> </taglib>
Note:pageContext is an object of javax.servlet.jsp.PageContext interface and it will be created automatically by JSP container. pageContext object is pre-declared in TagSupport class and this object will be available to each and every sub-class of TagSupport class.