Step 6: Declare Scriptlet Variables
Page 8 (9 pages a total)
To understand the Scriptlet variable, you must understand the role of the TLD file. This file is basically a repository of metadata. When the JSP page is converted to servlet, the tag uses these metadata. In the generated servlet, the Scriptlet variable is a local variable. To let the JSP conversion engine know that these variables should be declared, you need to add items below to add items in the TLD file:
Simple label survival cycle overview
Page 9 (9 pages a total)
If I first use the label for the first time, you may find that it is not easy to make the conversion engine survival cycle and the actual thing that actually happens. Like me, you may see a lot of charts that try to explain these concepts, which look like this. Here is the minimized image This chart doesn't make sense, but the generated code is meaningful, so we will analyze these code in this section. (Now I think these charts are meaningful, but I think these code can explain the problem. When we complete this section, you may want to analyze this chart again.) You may remember that the JSP page is actually camouflage servlet . The JSP file is converted to servlet before use. Below is a small JSP page called TestmapDefine.jsp, we will use it to show and test the mapdefinetag tag developed in the previous section: <% @ Taglib URI = "prefix =" MAP "%>
The map.tld file in the web-inf directory looks like this: XML Version = "1.0" Encoding = "UTF-8"?>
Generated servlet then added the following code: public class _testmapdefine__jsp extends ... JavaPage {public void _jspService (HttpServletRequest request, HttpServletResponse response) throws ... {trivera.tags.map.MapDefineTag tag0 = null; java.util. Map Employee = NULL; try {... if (tag0 == null) {tag0 = new trivera.tags.map.mapdefinetag (); tag0.setPageContext (PageContext); tag0.SetParent ((javax.servlet.jsp.tageXT) .Tag) NULL); tag0.setid ("Employee");} int includebody = tag0.dostarttag (); if (includebody! = Javax.servlet.jsp.tagext.tag.skip_body) {Employee = (java.util. Map) PageContext.Findattribute ("Employee"); OUT.PRINT ("
/ n");} Employee = (java.util.map) PageContext.Findattribute ("Employee"); ...} Catch (java.lang.throwable _jsp_e) {pageco NTEXT.HANDLEPAGEEXCEPTION (_Jsp_e); ...} ...} The generated JSP servlet declares a local variable named tag0, type Trivera.tags.map.mapDefineTag. Then it creates an instance of the label, set the page, set the parent label as NULL, and set the ID as Employee. Then, the generated servlet calls the dostartTAG () method, which checks if the return type is set to tag.skip_body. If so, the container is not judged without the body of the label (here is IF block). If it returns Eval_Body_include, just like our label, the container will handle the body. The body of the label can be added to this technique - ie control flow. Note that the generated servlet has a local variable named Employee, set it to EMPLOYEE according to the ID attribute of the tag. Therefore, the conversion engine defines a local variable called EMPLOYEE when the conversion engine is not runtime. This concept makes a lot of newcomers confused. Understand nested label
Page 1 (3 pages of)
The previous JSP page example uses the JSP Scriptlet to add an item to the Employee Map. If you can do it with another label. Let us define a nested label called MapenTRYTAG, which is obtained by calling getParent () and converts it to MAPDEFINETAG. MAPDEFINETAG defines a getMap () method, which returns the newly created MAP.
Nested MapenTryTAG in DoendTag () Use the mapDefineTag's getMap () method to add values to MAP, as shown: public class mapenTrytag extends tagsupport {string type = "java.lang.string"; string id; string value; string name ; String property; String scope; public int doEndTag () throws JspException {/ * Grab the MapDefineTag using the getParent method and cast it to a MapDefineTag * / MapDefineTag mapDef = (MapDefineTag) this.getParent ();. Object objectValue = null; ... / * Instantiate a new string, integer or float based on the type. * / If ("java.lang.string")) {ObjectValue = Value;} else} else} else} else} else} else} else.equals ("Java .lang.integer ") {Integer INTVALUE = INTEGER.VALUEOF (Value); ObjectValue = INTVALUE;} else IF (" java.lang.float ")) {float floatvalue = float.Valueof (value); ObjectValue = floatvalue;} / * put the new entry INTO THE MAP. * / mapdef.getmap (); Ret (ID, ObjectValue); Return EVAL_PAGE; on the basis of the above code, and assume that necessary in the TLD file Item, you can use our label like this: <% @ Taglib URI = "MAP" prefix = "map"%>