We describe some details of how to compile tag libraries and show a sketch of some code implementing a JSP page. JSP.B.3.1 Processing a Tag Library DescriptorThe tag library descriptor is read and information is extracted from it. Some of the actions to be performed include:
JSP.B.3.2 Processing a JSP PageWhen the JSP container processes a JSP page, it will perform analysis, validation, and generation of code. Actions include:
JSP.B.3.3 Generating the JSP Page Implementation ClassThe JSP page implementation class generated by the JSP container includes code that:
JSP.B.3.4 An ExampleWe now describe a simple example. JSP.B.3.4.1 JSP Page ExampleWe will use a JSP page fragment as in Figure JSP.B-1, where " chunk " is some uninterpreted template text. Figure JSP.B-1 A JSP Page Fragmentchunk1 <x:foo id="myFoo" ...> chunk2 <x:bar id="myBar" ...> chunk3 </x:bar> chunk4 </x:foo> chunk5 <x:baz ref="myFoo" .../> For the example, we will assume the TLD and TagExtraInfo provide the information in Table JSP.B-1. Table JSP.B-1. TagInfo for the Example
JSP.B.3.4.2 Implementation Code FragmentThe following code fragment can be used to implement the page fragment of Figure JSP.B-1. static { JspFactory _fact = JspFactory.getDefaultFactory(); } _jspService(HttpServetRequest req, HttpServletResponse res) { PageContext pc = _fact.getPageContext(...); // once Object tempObject = null; int tempReturn; // just as an example, let's initialize // all the Tag handlers FooTag footag = new FooTag(); BarTag bartag = new BarTag() BazTag baztag = new BazTag(); JspWriter out = pageContext.getOut(); // initial out // -- ditto for all other implicit objects EVAL chunk1; _______________________________________________ Evaluate <x:foo>...</x:foo> _______________________________________________ EVAL chunk5; baztag.setPageContext(pc); baztag.setParent(null); baztag.setRef("myFoo"); try { (void)baztag.doStartTag(); tempReturn = baztag.doEndTag(); } finally { baztag.release(); } if (tempReturn == SKIP_PAGE) { goto endOfPage; // pseudo-code }; endOfPage: } where the evaluation of <foo>...</foo> is: footag.setPageContext(pc); footag.setParent(null); footag.setId("myFoo"); try { if (footag.doStartTag() == EVAL_BODY_TAG) { try { out = pc.pushBody(); foobag.setBodyContent(out); footag.doInitBody(); repeat2: EVAL chunk2; _______________________________________________ Evaluate <x:foo>...</x:foo> _______________________________________________ EVAL chunk4; if (footag.doAfterBody() == EVAL_BODY_TAG) { goto repeat2; // pseudo-code } } finally { out = pc.popBody(); } } tempResult = footag.doEndTag(); tempObject = pc.getAttribute("myFoo"); } finally { footag.release(); } FooResult myFoo = (FooResult) tempObject; if (tempResult == SKIP_PAGE) { goto endOfPage; // pseudo-code } and the evaluation of <bar>...</bar> is essentially the same: bartag.setPageContext(pc); bartag.setParent(footag); bartag.setId("myBar"); try { if (bartag.doStartTag() == EVAL_BODY_TAG) { try { out = pc.pushBody(); bartag.setBodyContent(out); bartag.doInitBody(); repeat3: EVAL chunk3; if (bartag.doAfterBody() == EVAL_BODY_TAG) { goto repeat3; // pseudo-code } } finally { out = pc.popBody(); } } tempResult = bartag.doEndTag(); tempObject = pc.getAttribute("myBar"); } finally { bartag.release(); } BarResult myBar = (BarResult) tempObject; if (tempResult == SKIP_PAGE) { goto endOfPage; // pseudo-code } |