// // select // mytags.SelectTag // // Do an SQL "SELECT" query, and iterate over rows of result. // // // session // true // // // columns // true // // // table // true // // JSP // package mytags ; import javax.servlet.*; import javax.servlet.http.*; import javax.servlet.jsp.* ; import javax.servlet.jsp.tagext.* ; import java.io.* ; import java.sql.*; public class SelectTag extends BodyTagSupport { public int doStartTag() throws JspException { try { HttpSession session = pageContext.getSession() ; DBSession dbs = (DBSession) session.getAttribute(sessionID) ; if(dbs == null) { dbs = new DBSession() ; session.setAttribute(sessionID, dbs) ; session.setMaxInactiveInterval(300) ; // 5 minutes. } rs = dbs.stat.executeQuery("SELECT " + columns + " FROM " + table) ; if(rs.next()) return EVAL_BODY_TAG ; else return SKIP_BODY ; } catch (SQLException e) { throw new JspTagException(e.getMessage()) ; } } public int doAfterBody() throws JspException { try { if(rs.next()) return EVAL_BODY_TAG ; else { BodyContent body = getBodyContent() ; body.writeOut(getPreviousOut()) ; return SKIP_BODY ; } } catch (IOException e) { throw new JspTagException(e.getMessage()) ; } catch (SQLException e) { throw new JspTagException(e.getMessage()) ; } } public void setSession(String sessionID) { this.sessionID = sessionID ; } public void setTable(String table) { this.table = table ; } public void setColumns(String columns) { this.columns = columns ; } private String sessionID, columns, table ; ResultSet rs ; }