Thursday, February 26, 2009

Custom tags and JSPs interaction

Custom tags are not new to any JSP programmer. In this article, I would like to create a custom tag that demonstrates how a custom tag interacts with JSP using attributes.

In case you are new to custom tags in JSP 2.0, see article Creating and using custom tags.

I will create a custom tag called , which takes an SQL query and makes data retrieved by the query available to JSP through attributes.The following are the steps to be taken to develop this tag and use it.

The first step is to create the tag handler as follows:
package st;

import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;
import java.sql.*;


public class QueryHandler extends SimpleTagSupport {
private String sql;

public void doTag() throws JspException {
JspWriter out=getJspContext().getOut();
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:XE","hr","hr");
Statement st = con.createStatement();
ResultSet rs = st.executeQuery(sql);
JspContext ctx = getJspContext();

// get data related to ResultSet
ResultSetMetaData rsmd = rs.getMetaData();
int count = rsmd.getColumnCount();
JspFragment f=getJspBody();

while ( rs.next()) {
for(int i = 1 ; i <= count ; i ++ ) {
// Create an attribute for each column.
// Column name is attribute name and value is attribute's value.
ctx.setAttribute( rsmd.getColumnName(i), rs.getString(i));
}
// process the body of the tag and send result to JSPWriter
f.invoke(out);
}
rs.close();
con.close();
} catch (Exception ex) {
throw new JspException(ex.getMessage());
}
}
// attribute sql
public void setSql(java.lang.String value) {
this.sql = value;
}
}


In the above Tag Handler, we use SQL query passed through attribute sql to retrieve data. The data is placed in ResultSet. For each row of the ResultSet we create a set of attributes - one for each column. The name of the attribute is column name and value is column's value. To get names of columns we have to get ResultSetMetaData object from ResultSet.

NOTE : The column names are in uppercase. So even the attribute names are in uppercase. In case you want to create attributes with lowercase names, use toLowerCase() method of String class as follows:
ctx.setAttribute( rsmd.getColumnName(i).toLowerCase() , rs.getString(i));

The following is tab library descriptor (st.tld) for the above tag.


1.0
Srikanth Technologies Tags
/WEB-INF/tlds/st

query
st.QueryHandler
scriptless

sql
true
true
java.lang.String





Now, let us register st.tld in a JSP using taglib directive and use tag. The following JSP shows how to use it.
<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>
<%@taglib uri="/WEB-INF/tlds/st.tld" prefix="st"%>


"http://www.w3.org/TR/html4/loose.dtd">




Using Query Tag


Query





  • ${DEPARTMENT_ID}, ${DEPARTMENT_NAME}









TitleMin Salary
${JOB_TITLE} ${MIN_SALARY}






First usage of query tag is to retreive details from DEPARTMENTS table. Attributes DEPARTMENT_ID and DEPARTMENT_NAME are created from tag handler. Those attributes are used in JSP using Expression Langauge (EL) syntax. By making data available using attributes, we allow JSP to display data in the way it wants. It is demonstrated by second usage of tag as we used a table to display the data of attributes.
This article shows how to make use of the following:
Creating a custom tag using JSP 2.0 simple tag technique
Getting Connection to Oracle . Make sure you make JDBC driver available to web application.
Using ResultSet and ResultSetMetaData to get information about ResultSet
Creating page attributes in custom tag to make data available to JSP in which tag is used
Using attributes created by custom tag with expression language
The concept of custom tags is very powerful. JSTL, JSF and even frameworks such as Struts make use of custom tags extensively. So, it is worth understanding how custom tags and JSPs interact.

1 comment:

  1. this was really useful to me, learning jsp/tags etc is quite difficult without practical examples, this is much clearer than the books i've been using, thank you

    ReplyDelete