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
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.
Now, let us register st.tld in a JSP using taglib directive and use
<%@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">
Query
- ${DEPARTMENT_ID}, ${DEPARTMENT_NAME}
Title | Min 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
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.
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