Chapter 5 Custom Tag Library javabeans jsp, Bean jsp content scripting jsp ( ) xxx, Custom tag( ) 1 Tag Handler Class ( ) 2 Tag Library Descriptor File ( ) 3 taglib (JSP ) Tag Handler Class ( ) Class, JSP, JSP javaxservletjsptagexttag (implements), javaxservletjsptagexttagsupport javaxservletjsptagextbodytagsupport (extends) TagSupport, BodyTagSupport Tag Tag Hello Jsp List 51Hello jsp http://wwwjava2xmlcom
List 51 CustomTagExjava package comboolpaejsp; import javaxservletjsp*; import javaxservletjsptagext*; import javaio*; public class CustomTagEx extends TagSupport{ public int dostarttag(){ try{ JspWriter out = pagecontextgetout(); outprint(" "); catch(ioexception e){ Systemoutprintln("Error : "+e); return SKIP_BODY; List 51TagSupport TagSupport BodyTagSupport javaxservletjsptagext import Tag javaxservletjsp javaio import JspWriter javaxservletjsp dostarttag <tag attribute= value >body</tag> attribute, body dostarttag, JspWriter JSP out javaioprintwriter (?), PrintWriter JspWriter pagecontext getout() pagecontext request, response, sessionjsp getrequest(), getresponse(), getsession() JspWriterprint IOException try~catch http://wwwjava2xmlcom
dostarttag JspException dostarttag public int dostarttag() throws JspException{ try{ JspWriter out = pagecontextgetout(); outprint(" "); catch(ioexception e){ throw new JspTagException("Error : "+egetmessage()); return SKIP_BODY; SKIP_BODY SKIP_BODY SKIP_BODY SKIP_BODY? Tag 4 SKIP_BODY EVAL_BODY_INCLUDE, SKIP_PAGE, EVAL_PAGE EVAL_BODY_INCLUDE SKIP_BODY SKIP_PAGEEVAL_PAGEdoEndTag doendtag, tag ( ) doendtag SKIP_PAGE,, EVAL_PAGE SKIP_BODY EVAL_BODY_INCLUDE dostarttag dostarttag SKIP_PAGE EVAL_PAGE doendtag doendtag BodyTagSupport EVAL_BODY_TAG doafterbody, http://wwwjava2xmlcom
jsp WEB-INF/classes Tag Library Descriptor( ) List 52 List 52 taglibexampletld <?xml version="10" encoding="iso-8859-1"?> <!DOCTYPE taglib PUBLIC "-//Sun MicroSystems, Inc //DTD JSP Tag Library 11//EN" "http://javasuncom/j2ee/dtds/web-jsptaglibrary_1_1dtd"> <taglib> <tlibversion>10</tlibversion> <jspversion>11</jspversion> <shortname>jspace</shortname> <uri/> <info> JavaServer Pages tag library Example </info> <tag> <name>test</name> <tagclass>comboolpaejspcustomtagex</tagclass> <info>simple example</info> <bodycontent>empty</bodycontent> </tag> </taglib> List 52 xml xml xml Html,! xml <?xml version= 10?> encoding DTD(Sun) DTD List 52 xml <taglib> Html <html> <taglib> <tag> <tlibversion> 10 http://wwwjava2xmlcom
<jspversion>jsp 11 11 <shortname> jsp (prefix) jsp taglib taglib? <%@ taglib uri= taglibraryuri prefix= tagprefix %> <tag> <tlibversion> <shortname> <uri> uri, <info> <tag> List 52 <name>taglib <name>test</name> test, taglib prefix= jspace, jsp <jspace:test> xml <jspace:test/> <tagclass> test <tagclass> List 52 <tagclass>comboolpaejspcustomtagex</tagclass> <info> <bodycontent> EMPTY, JSP, TAGDEPEDENT EMPTY, JSPJSP TAGDEPENT JSP Page jsp? taglib List 53 List 53 TagExjsp <%@ page contenttype="text/html;charset=euc-kr" %> <HTML> <HEAD> http://wwwjava2xmlcom
<TITLE> </TITLE> </HEAD> <BODY> <CENTER> <H1> </H1><br><br> <%@ taglib uri="web-inf/tlds/taglibexampletld" prefix="jspace" %> <font size=5 color=black face=" "><jspace:test/></font><br> <font size=3 color=blue face=" "><jspace:test/></font><br> <font size=2 color=orange face=" "><jspace:test/></font><br> </CENTER> </BODY> </HTML> List 53 WEB-INF/tlds/taglibExampletld, prefixjspace TLD( ) WEB-INF tlds, Custom Tag <jspace:test attribute01= value01 attribute02= value02 /> http://wwwjava2xmlcom
setattribute() Attribute attribute01, attribute02 setxxx (?) <jsp:setproperty name property value /> Property Xxx setattribute() TLD( TLD ) private int size; public int dostarttag(){ outprint("<font size='"+size+"'> "); public void setsize(int size){ thissize=size; size int setsize, TLD <attribute> <tag> <tag> <attribute> <name>size</name> <required>false</required> <rtexprvalue>false</rtexprvalue> </attribute>, <name> <required> true false <rtexprvalue> (expression) true false false <jspace:test size= <%=requestgetprarameter( size )%> /> <jspace:test size= 3 /> (?) http://wwwjava2xmlcom
List 54 CustomTagExjava package comboolpaejsp; import javaxservletjsp*; import javaxservletjsptagext*; import javaio*; public class CustomTagEx extends TagSupport{ private int size; public int dostarttag(){ try{ JspWriter out = pagecontextgetout(); outprint("<font size='"+size+"'> "); catch(ioexception e){ Systemoutprintln("Error : "+e); return SKIP_BODY; public void setsize(int size){ thissize=size; List 55 taglibexampletld <?xml version="10" encoding="iso-8859-1"?> <!DOCTYPE taglib PUBLIC "-//Sun MicroSystems, Inc //DTD JSP Tag Library 11//EN" "http://javasuncom/j2ee/dtds/web-jsptaglibrary_1_1dtd"> <taglib> <tlibversion>10</tlibversion> <jspversion>11</jspversion> <shortname>jspace</shortname> <urn/> <info> JavaServer Pages tag library Example </info> <tag> <name>test</name> <tagclass>comboolpaejspcustomtagex</tagclass> <info>simple example</info> <bodycontent>empty</bodycontent> <attribute> <name>size</name> <required>false</required> <rtexprvalue>false</rtexprvalue> </attribute> http://wwwjava2xmlcom
</tag> </taglib> jsp? jsp font List 56 tagexjsp <HTML> <HEAD> <TITLE> </TITLE> </HEAD> <BODY> <CENTER> <H1> </H1><br><br> <%@ taglib uri="web-inf/tlds/taglibexampletld" prefix="jspace" %> <jspace:test size="5" /></font><br> <jspace:test size='3' /></font><br> <jspace:test size='2'/></font><br> </CENTER> </BODY> </HTML> font size attribute (" "), (' ') attribute a, setattribute() A http://wwwjava2xmlcom
Custom Tag <jspace:test /> <jspace:test> </jspace:test>, dostarttagskip_body EVAL_BODY_INCLUDE EVAL_BODY_INCLUDE, doendtag() dostarttag() doendtag EVAL_PAGE SKIP_PAGE (scripting elements), (expression), (action) dostarttag fontsize face( ) doendtag font font dostarttag EVAL_BODY_INCLUDE List 57 List 57 CustomTagExjava http://wwwjava2xmlcom
package comboolpaejsp; import javaxservletjsp*; import javaxservletjsptagext*; import javaio*; public class CustomTagEx extends TagSupport{ private int size; private String face; public int dostarttag(){ try{ JspWriter out = pagecontextgetout(); outprint("<font size='"+size+"' face='"+face+"'>"); catch(ioexception e){ Systemoutprintln("Error : "+e); return EVAL_BODY_INCLUDE; public int doendtag(){ try{ JspWriter out = pagecontextgetout(); outprint("</font>"); catch(ioexception e){ Systemoutprintln("Error : "+e); return EVAL_PAGE; public void setsize(int size){ thissize=size; public void setface(string face){ thisface = face; TLD face <attribute> <tag> <bodycontent> JSP JSP JSP List 58 taglibexampletld <?xml version="10" encoding="iso-8859-1"?> <!DOCTYPE taglib PUBLIC "-//Sun MicroSystems, Inc //DTD JSP Tag Library 11//EN" "http://javasuncom/j2ee/dtds/web-jsptaglibrary_1_1dtd"> http://wwwjava2xmlcom
<taglib> <tlibversion>10</tlibversion> <jspversion>11</jspversion> <shortname>jspace</shortname> <urn/> <info> JavaServer Pages tag library Example </info> <tag> <name>test</name> <tagclass>comboolpaejspcustomtagex</tagclass> <info>simple example</info> <bodycontent>jsp</bodycontent> <attribute> <name>size</name> <required>false</required> <rtexprvalue>false</rtexprvalue> </attribute> <attribute> <name>face</name> <required>false</required> <rtexprvalue>false</rtexprvalue> </attribute> </tag> </taglib> List 59 tagexjsp <%@ page contenttype="text/html;charset=euc-kr" %> <style> BODY {font-family:tahoma; font-size:20pt </style> <HTML> <HEAD> <TITLE> </TITLE> </HEAD> <BODY> <CENTER> <H1> </H1><br><br> <%@ taglib uri="web-inf/tlds/taglibexampletld" prefix="jspace" %> <jspace:test size="6" face="comic sans MS">This font is Comic sans MS</jspace:test><br> <jspace:test size='4' face="verdana">bluenote Custom Tag is Verdana font Style </jspace:test><br> <jspace:test size='2' face="system">system font style is so strong</jspace:test><br> <hr> This font is pre-defined in style Tag </CENTER> </BODY> http://wwwjava2xmlcom
</HTML> BODY doendtag </font> font style (attribute)? size face size=0, face=null http://wwwjava2xmlcom
List 510 OptionTagjava package comboolpaejsp; import javaxservletjsp*; import javaxservletjsptagext*; import javaxservlet*; import javaio*; public class OptionTag extends TagSupport{ public int dostarttag(){ ServletRequest request = pagecontextgetrequest(); String option = requestgetparameter("option"); if("yes"equalsignorecase(option)){ return EVAL_BODY_INCLUDE; else{ return SKIP_BODY; option yes, pagecontext jsp ( jsp, jsp API ) getrequest() ServletRequst request List 511 taglibexampletld <!--------- ------ <taglib> <! ------- ------ <tag> <name>option</name> <tagclass>comboolpaejspoptiontag</tagclass> <info>option tag example</info> <bodycontent>jsp</bodycontent> </tag> </taglib> List 511 taglibexampletld <tag> </tag> http://wwwjava2xmlcom
List 512 optiontagjsp <%@ page import="javautilenumeration" contenttype="text/html;charset=euc- KR" %> <HTML> <HEAD> <TITLE> Option Tag Example </TITLE> </HEAD> <BODY> <center> <h1> Option Tag Example </h1><br> option parameter yes <br> yes <br><br> <%@ taglib uri="web-inf/tlds/taglibexampletld" prefix="jspace" %> <jspace:option> <% Enumeration e = requestgetheadernames(); while(ehasmoreelements()){ String headername = (String)enextElement(); %> Header Name : <%=headername%> : <%= requestgetheader(headername) %><br> <% %> </jspace:option> </BODY> </HTML> List 512 Http Request requestgetheadernames() Enumeration while Header EnumerationObject ( String ) requestgetheader(),,, option yes URL get [ ] http://wwwjava2xmlcom
[ yes ] http://wwwjava2xmlcom
Body BodyTagSupport TagSupport dostarttag doendtag Tag (implements) TagSupport Tag BodyTagSupport TagSupport BodyTag BodyTagSupport Tag BodyTagSupport, doafterbody : SKIP_BODY EVAL_BODY_TAG, getbodycontent :, BodyContent JspWriter getpreviousout : JspWriter JspWriter JspWriter enclosing writer TagSupport dostarttag pagecontextgetout JspWriter getprevioustout JspWriter dostarttagdoendtag JspWriter BodyContent JspWriter JspWriter BodyContentgetEnclosingWirter, getpreivousout, BodyContentgetEnclosingWriter JspWriter getstring : BodyContent String http://wwwjava2xmlcom
Html < > < > List 512 SourceViewjava package comboolpaejsp; public class SourceView{ public static String filter(string source){ StringBuffer sourcefilter = new StringBuffer(sourcelength()); char c; for(int i=0;i<sourcelength();i++){ c = sourcecharat(i); if(c=='<'){ sourcefilterappend("<"); else if(c=='>'){ sourcefilterappend(">"); else{ sourcefilterappend(c); return sourcefiltertostring(); StringBuffer,, String charat(index) append StringBuffer List 513 SourceTagjava package comboolpaejsp; import javaxservletjsp*; import javaxservletjsptagext*; import javaio*; public class SourceTag extends BodyTagSupport{ public int doafterbody(){ BodyContent body = getbodycontent(); String filteredbody = SourceViewfilter(bodygetString()); try{ http://wwwjava2xmlcom
JspWriter out = getpreviousout(); outprint(filteredbody); catch(ioexception e){ Systemoutprintln("Error : " +e); return SKIP_BODY; BodyContent getstring String List 512 SourceView filter <, > < > getpreviousout JspWriter SKIP_BODY (?)TLD taglibexampletld List 514 taglibexampletld <tag> <name>filter</name> <tagclass>comboolpaejspsourcetag</tagclass> <info>source code view Tag: Non HTML code</info> <bodycontent>jsp</bodycontent> </tag> JSP List 515 SourceViewjsp <HTML> <HEAD> <TITLE> Source code Filter </TITLE> <style> BODY {font-family: ; font-size:12pt TABLE, TR, TD {font-family: ; font-size:18pt </style> </HEAD> <BODY> <center> <h1>source View Example </h1> <%@ taglib uri="web-inf/tlds/taglibexampletld" prefix="jspace" %> <jspace:filter> <table><tr><td> TD </td></tr></table> </jspace:filter> http://wwwjava2xmlcom
<hr> <table><tr><td> TD </td></tr></table> </BODY> </HTML>, HTML Style style [SourceViewjsp] doafterbody EVAL_BODY_TAG? SKIP_BODY! List 516 LoopTagjava package comboolpaejsp; http://wwwjava2xmlcom
import javaxservletjsp*; import javaxservletjsptagext*; import javaio*; public class LoopTag extends BodyTagSupport{ private int repeats; public void setrepeats(int repeats){ thisrepeats = repeats; public int doafterbody(){ if(repeats-- >= 1){ BodyContent body = getbodycontent(); try{ JspWriter out = getpreviousout(); outprintln(bodygetstring()); bodyclearbody(); catch(ioexception e){ Systemoutprintln("Error : "+e); return EVAL_BODY_TAG; else{ return SKIP_BODY; List 516 0 doafterbody EVAL_BODY_TAG? TLD List 517 taglibexampletld <tag> <name>loop</name> <tagclass>comboolpaejsplooptag</tagclass> <info>loop tag body</info> <bodycontent>jsp</bodycontent> </tag> <attribute> <name>repeats</name> <required>false</required> <rtexprvalue>true</rtexprvalue> </attribute> <rtexprvalue> true http://wwwjava2xmlcom
jsp, jsp List 518 LoopTagjsp <HTML> <HEAD> <TITLE> New Document </TITLE> </HEAD> <BODY> <center> <%@ taglib uri="web-inf/tlds/taglibexampletld" prefix="jspace" %> <jspace:loop repeats="10"> i love you~<br> </jspace:loop> </BODY> </HTML> ~10 JSP? http://wwwjava2xmlcom
List 519 inlooptagjava package comboolpaejsp; import javaxservletjsp*; import javaxservletjsptagext*; import javaio*; public class InLoopTag extends TagSupport{ public int dostarttag(){ double i = Mathrandom(); try{ JspWriter out = pagecontextgetout(); outprint(i); catch(ioexception e){ Systemoutprintln("Error : "+e); return SKIP_BODY; Mathrandom TLD List 520 taglibexampletld <tag> <name>inloop</name> <tagclass>comboolpaejspinlooptag</tagclass> <info>will be repeated in Loop Tag</info> <bodycontent>jsp</bodycontent> </tag> JSP, LoopTag repeats <rtexprvalue> true LoopTag, setrepeats http://wwwjava2xmlcom
List 521 LoopTagsetRepeats public void setrepeats(string repeats){ try{ thisrepeats = IntegerparseInt(repeats); catch(numberformatexception e){ thisrepeats = 1; requestgetparameter String NumberFormatException, LoopTagjsp List 522 LoopTagjsp <HTML> <HEAD> <TITLE> New Document </TITLE> </HEAD> <BODY> <center> <h1>loop Tag Example</h1> <%@ taglib uri="web-inf/tlds/taglibexampletld" prefix="jspace" %> <jspace:loop repeats='<%=requestgetparameter("repeats")%>'> <jspace:inloop/><br> </jspace:loop> </BODY> </HTML> 5, 10 repeats [5 LoopTagjsp] http://wwwjava2xmlcom
[10 LoopTagjsp] http://wwwjava2xmlcom
JSP ( )? ~ API, findancestorwithclass API public static final Tag findancestorwithclass(tag tag, javalangclass cl) final, Class Class class JspTagException, <prefix:if> <prefix:condition>true false</prefix:condition> <prefix:then> if condition is true then process something </prefix:then> <prefix:else> if condition is false then process something </prefix:else> </prefix:if> http://wwwjava2xmlcom
if(condition){ else{ List 523 IfTagjava package comboolpaejsp; import javaxservletjsp*; import javaxservletjsptagext*; import javaio*; public class IfTag extends TagSupport{ private boolean condition; private boolean hascondition = false; public void setcondition(boolean condition){ thiscondition = condition; hascondition = true; public boolean getcondition(){ return condition; public void sethascondition(boolean hascondition){ thishascondition = hascondition; public boolean gethascondition(){ return hascondition; http://wwwjava2xmlcom
public int dostarttag(){ return EVAL_BODY_INCLUDE; condition hascondition conditiontrue false hascondition conditionif hasconditionfalse ( false ) condition sethascondition hascondition true List 524 IfConditionTagjava package comboolpaejsp; import javaxservletjsp*; import javaxservletjsptagext*; import javaio*; public class IfConditionTag extends BodyTagSupport{ public int dostartag() throws JspException{ IfTag parent = (IfTag)findAncestorWithClass(this, IfTagclass); if(parent == null){ throw new JspTagException("ConditionTag Error"); return EVAL_BODY_TAG; public int doafterbody(){ IfTag parent = (IfTag)findAncestorWithClass(this, IfTagclass); BodyContent body = getbodycontent(); String bodystring = bodygetstring(); if("true"equals(bodystringtrim())){ parentsetcondition(true); http://wwwjava2xmlcom
else{ parentsetcondition(false); return SKIP_BODY; IfConditionTag dostarttag findancestorwithclass (IfTag) trueiftag condtion true false equals equalsignorecase SKIP_BODY IfThenTag List 525 IfThenTagjava package comboolpaejsp; import javaxservletjsp*; import javaxservletjsptagext*; import javaio*; public class IfThenTag extends BodyTagSupport{ public int dostarttag() throws JspTagException{ IfTag parent = (IfTag)findAncestorWithClass(this, IfTagclass); if(parent == null){ throw new JspTagException("Error in IfThen Tag : parent Tag is null"); else if(!parentgethascondition()){ throw new JspTagException("Error in IfThen Tag: IfTag's hascondition value is false"); return EVAL_BODY_TAG; public int doafterbody(){ IfTag parent = (IfTag)findAncestorWithClass(this, IfTagclass); http://wwwjava2xmlcom
if(parentgetcondition()){ try{ BodyContent body = getbodycontent(); JspWriter out = getpreviousout(); outprint(bodygetstring()); catch(ioexception e){ Systemoutprintln("Error in IfThen Tag : "+e); return SKIP_BODY; IfThen dostarttag (IfTag) hasconditionfalse IfCondition Condition hasconditiontrue EVAL_BODY_TAG doafterbody doafterbody IfTag conditiontrue true IfThen conditiontrue true List 526 IfElseTagjava package comboolpaejsp; import javaxservletjsp*; import javaxservletjsptagext*; import javaio*; public class IfElseTag extends BodyTagSupport{ public int dostarttag() throws JspTagException{ IfTag parent = (IfTag)findAncestorWithClass(this, IfTagclass); if(parent == null){ throw new JspTagException("Error in IfElseTag : parent Tag is null"); else if(parentgethascondition()){ throw new JspTagException("Error in IfElseTag : IfTag's hascondition http://wwwjava2xmlcom
value is false"); return EVAL_BODY_TAG; public int doafterbody(){ IfTag parent = (IfTag)findAncestorWithClass(this, IfTagclass); if(!parentgetcondition()){ try{ BodyContent body = getbodycontent(); JspWriter out = getpreviousout(); outprint(bodygetstring()); catch(ioexception e){ Systemoutprintln("Error in IfElseTag : "+e); return SKIP_BODY; IfElseTag IfThen dostarttagifthen doafterbody condition false 4 ~ TLD List 527taglibExampletld List 527 taglibexampletld <tag> <name>if</name> <tagclass>comboolpaejspiftag</tagclass> <info>if Tag</info> <bodycontent>jsp</bodycontent> </tag> <tag> http://wwwjava2xmlcom
<name>condition</name> <tagclass>comboolpaejspifconditiontag</tagclass> <info>ifcondition Tag</info> <bodycontent>jsp</bodycontent> </tag> <tag> <name>then</name> <tagclass>comboolpaejspifthentag</tagclass> <info>ifthen Tag</info> <bodycontent>jsp</bodycontent> </tag> <tag> <name>else</name> <tagclass>comboolpaejspifelsetag</tagclass> <info>ifelse Tag</info> <bodycontent>jsp</bodycontent> </tag> TLD JSP JSP condition condition, loop Mathrandom() 05, List 528 IfTagExamplejsp <HTML> <HEAD> <TITLE> </TITLE> <style> BODY {font-size:15pt </style> </HEAD> <BODY> <center> <h1> Example </h1> http://wwwjava2xmlcom
<%@ taglib uri="web-inf/tlds/taglibexampletld" prefix="jspace" %> <jspace:if> <jspace:condition><%=requestgetparameter("condition")%></jspace:conditi on> <jspace:then>condition is true</jspace:then> <jspace:else>condition is false</jspace:else> </jspace:if> <hr> <jspace:loop repeats='10'> <jspace:if> <jspace:condition><%=mathrandom() > 05%></jspace:condition> <jspace:then>the number is greater than 05<br></jspace:then> <jspace:else>the number is smaller than 05<br></jspace:else> </jspace:if> </jspace:loop> </BODY> </HTML> http://wwwjava2xmlcom
http://wwwjava2xmlcom chapter JSP? ASPPHP JSP,
JSP JSP MVC, MVC MVC (MVC SmallTalk GUI ) Thinking in Patterns with Java(Bruce Eckel ) J2EE MVC Sun http://javasuncom/j2ee/blueprints/design_patterns/mvc/indexhtml EJB 2 chapter JDBC http://wwwjava2xmlcom