I.                    BodyTag

ܼ±(Simple Tag) ü ѹ ó, BodyTag ü ó ִ.

 

inteface

javax.servlet.jsp.tagext.BodyTag

+EVAL_BODY_TAG:int

+doInitBody:void

+setBodyContent(:javax.servlet.jsp.tagext.BodyContent):void
+doAfterBody():int

 

interface

javax.servlet.tagext.Tag

+EVAL_BODY_INCLUDE :int

+EVAL_PAGE:int

+SKIP_BODY:int

+SKIP_PAGE:int

+release():void

+getParent():javax.servlet.jsp.tagext.Tag

+getParent(:javax.servlet.jsp.tagext.Tag):void

+doEndTag():int

+doStartTag():int

+getPageContext(:javax.servlet.jsp.PageContext):void

 

BodyTag ̽ Tag ̽ Ͽ  ޼带 Ѵ.

BodyContent Ŭ ü ó ֵ Ѵ.

 

1.

 

 

SKIP_BODY

Ҷ ȯ

 
setPageContext(javax.servlet.jsp.PageContext):void

2. setParent(javax.servlet.jsp.tagext.Tag):void

3. //setAttribute()

4. doStartTag():int

5. setBodyContent(javax.servlet.jsp.tagext.BodyContent):void

6. doInitBody():void

7. doAfterBody():int

8. doEndTag():int

9. release():void

 

̾ ؼ BodyTag BodyContent Ŭ µ BodyContent

.

Javax.servlet.tagext.BodyContent

-enclosingWriter:javax.servlet.jsp.JspWriter

+BodyContent(:javax.servlet.jsp.JspWriter)

+getString():java.lang.String

+getReader():java.io.Reader

+writeOut(:java.io.Writer):void

+getEnclosingWriter():javax.servlet.jsp.JspWriter

+clearBody():void

 

BodyContent Ŭ JspWriter Ŭ ӹ޴µ ̴ ü ߿(?)ó ֵ ֵѴ.

BodyContent Ŭ content getReader() ޼尡 ϴ Reader ִ.

content getString()޼带 ȣϹǷν String ִ.

BodyContent content clearBody()޼ ִ.

BodyContent content ؼ writer (getEnclosingWriter() ޼忡 Ͽ ϵǴ JspWriteró)

writOut()޼带 ȣ ִ.

 

II.                  BodyTag ۼ

tag.LoopTag

-parent:Tag

-bodyContent:BodyContent

-pageContext:PageContext

-iterations:int=0

+LoopTag()

+setPageContext(PageContext:PageContext):void

+setParent(parent:javax.servlet.jsp.tagext.Tag):void

+setIterations(iterations:int):void

+doStartTag():int

+setBodyContent(bodyContent:BodyContent):void

+doInitBody():void

+doAfterBody():int

+doEndTag():int

+release():void

+getParent():javax.servlet.jsp.tagext.Tag

<LoopTag.java>

package tag;

 

import javax.servlet.jsp.*;

import javax.servlet.jsp.tagext.*;

 

public class LoopTag implements BodyTag{

       private Tag parent;

       private BodyContent bodyContent;

       private PageContext pageContext;

       private int iterations = 0;

 

       public LoopTag(){super();}

       public void setPageContext(PageContext pageContext){

                    this.pageContext = pageContext;

       }

       public void setParent(final javax.servlet.jsp.tagext.Tag parent){

                    this.parent = parent;

}

public void setIterations(int iteratons){

             this.iterations = iterations;

}

public int doStartTag() throws JspTagException{

             if(iterations > 0){

                           return EVAL_BODY_TAG;

             }else{

                           return SKIP_BODY;

             }

}

public void setBodycontent(BodyContent bodyContent){

             this.bodyContent = bodyContent;

}

public void doInitBody() throws JspTagException{}

public int doAfterBody() throws JspTagException{

             if(iterations > 1){

                           iterations--;

                          return EVAL_BODY_TAG;

             }else{

                           return SKIP_BOY;

             }

}

public int doEndTag() throws JspTagException{

             try{

                           if(bodyContent != null){

                                        bodyContent.writeOut(bodyContent.getEnclosingWriter());

                           }

             }catch(java.io.IOException e){

                           throw new JspTagException(IO Error : + e.getMessage());

             }

             return EVAL_PAGE;

}

public void relese();

public javax.servlet.jsp.tagext.Tag getParent(){

             return parent;

}

}

 

III.                Tag Library

<mytag.tld>

<tag>

<name>loop</name>
<tagclass> tag.LoopTag</tagclass>
<bodycontent>JSP</bodycontent>
<info>A BodyTag for looping</info>
<attribute>

<name>iterations</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>

</attribute>

</tag>

 

IV.               JSP ۼ

<loop.jsp>

<%@ page contentType=text/html;charset=EUC-KR %>

<%@ taglib uri=taglibExample prefix=mt>

<HTML>

<HEAD>

       <TITLE>Loop Test</TITLE>

</HEAD>

<BODY BGCOLOR=#FFFFFF>

<HR>

       <mt:loop iterations=5>

                    Hello<BR>

       <mt:loop/>

<HR>

</BODY>

</HTML>