package javahello; import javax.servlet.jsp.*; import javax.servlet.jsp.tagext.*; import java.sql.*; import java.io.*; public class HelloWorldBodyTag extends BodyTagSupport { private ResultSet result; public HelloWorldBodyTag() { super(); } public ResultSet getResult() { return result; } public void setResult(ResultSet result) { this.result = result; } public int doStartTag() throws JspTagException { result = (ResultSet) pageContext.getAttribute(id); try { if (result.next()) { pageContext.setAttribute(id, result); // 結果が存在する場合はボディを評価する return EVAL_BODY_AGAIN; } else { return SKIP_BODY; } } catch (Exception e) { throw new JspTagException(e.getMessage()); } } public int doAfterBody() throws JspTagException { // ボディの内容を取得 BodyContent body = getBodyContent(); try { // ボディを出力 body.writeOut(getPreviousOut()); } catch (IOException e) { throw new JspTagException(e.getMessage()); } body.clearBody(); try { if (result.next()) { // まだ行が残っている場合はボディの評価を繰り返す return EVAL_BODY_AGAIN; } else { return SKIP_BODY; } } catch (SQLException e) { throw new JspTagException(e.getMessage()); } } public int doEndTag() { return EVAL_PAGE; } public void release() { result = null; } }