Javaweb Base (_jsp) Custom Label

Source: Internet
Author: User
Tags tag name throwable tld

One, the role of custom tags

Custom labels are primarily used to remove Java code from JSP pages.

II. Custom Label development and use 2.1. Custom Label Development Steps 1. Write a Java class (Tag processor class) that implements the tag interface
1 package Me.gacl.web.tag; 2 3 Import java.io.IOException; 4 5 Import Javax.servlet.http.HttpServletRequest; 6 Import javax.servlet.jsp.JspException; 7 Import Javax.servlet.jsp.JspWriter; 8 Import Javax.servlet.jsp.PageContext; 9 Import javax.servlet.jsp.tagext.tag;10 one public classViewiptagImplementsTag {12 13//Receive PageContext objects passed in PageContext pagecontext;15 @Override17 public int Doendtag     () throws Jspexception {System.out.println ("Call Doendtag () method"); return 0;20}21 @Override23 public int doStartTag () throws Jspexception {System.out.println ("Call doStartTag () method"); Httpservletr Equest request = (HttpServletRequest) pagecontext.getrequest (); JspWriter out = Pagecontext.getout ();          Tring IP = request.getremoteaddr (); 29///This will throw a IOException exception when output is Out.write (IP); 31         } catch (IOException e) {32//Catch IOException exception after continuing to throw the throw of the new RuntimeException (e); 34     }35 return 0;36}37 @Override39 public Tag getParent () {43 return null;41}42     @Override44 public void Release () {System.out.println ("Call Release () method");}47 @Override49 public void SetpagecontExt (PageContext pagecontext) {System.out.println ("Setpagecontext (PageContext PageContext)"); This.pag Econtext = pagecontext;52}53 @Override55 public void setParent (Tag arg0) {56 57}58 59}
2. Create a new TLD file in the web-inf/directory to describe the label processor class in the TLD file

  

The code for the Gacl.tld file is as follows:

 1 <?xml version= "1.0" encoding= "UTF-8"?> 2 3 <taglib xmlns= "HTTP://JAVA.SUN.COM/XML/NS/J2EE" 4 xmlns:xsi= " Http://www.w3.org/2001/XMLSchema-instance "5 xsi:schemalocation=" HTTP://JAVA.SUN.COM/XML/NS/J2EE/http Java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd "6 version=" 2.0 "> 7 <!--description To add a description of the Taglib (tag library)- 8 <description> aloof Wolf developed custom tag library </description> 9 <!--taglib (tag Library) version number-->10 <tlib-versio N>1.0</tlib-version>11 <short-name>gacltaglibrary</short-name>12 <!--13 to customize tag library settings A Uri,uri with/start,/After the contents of the random, such as/gacl here, 14 in the JSP page reference tag library, you need to find the tag library by Uri 15 in the JSP page to introduce the tag library: <% @taglib uri= "/gac L "prefix=" GaCl "%>16-->17 <uri>/gacl</uri>18 <!--a taglib (tag library) contains multiple custom labels, each of which is custom-defined Use a tag tag to describe-->20 <!--a tag tag corresponding to a custom label-->21 <tag>22 <description> The function of this tag is to output the client The IP address </description>23 <!           ---24 label processor class with a label name, the use of tags in the JSP page is the tag name to find the label processor class to be called 25 by Viewip can find the corresponding Me.gacl.web.tag.ViewIPTag class 26 -->27 <name>viewip</name>28 <!--label corresponding processor class-->29 <tag-class>me     . gacl.web.tag.viewiptag</tag-class>30 <body-content>empty</body-content>31 </tag>32 </taglib>
2.2. Use a custom label in a JSP page

1. Use the prefix "%>" directive to introduce the tag library to use with the "<% @taglib uri=" tag Library's uri "prefix=" tag.

Example: referencing the GACL tag library in jsptag_test1.jsp

1 <%@ page language= "java" pageencoding= "UTF-8"%> 2 <!--use taglib directives to reference GaCl tag library, tag library prefix (prefix) can be set casually, as set here to prefix= "XDP"-3 <% @taglib uri= "/gacl"  prefix= "XDP"%> 4 <! DOCTYPE html> 5 

The label works as follows:

  

As you can see from the run effect species, the Java code on the JSP page can be removed using a custom label, and if you need to output the client's IP address on a JSP page, use the <xdp:viewIP/> tag instead of the code on the JSP page:

1 <%2         //Use Java code in the JSP page to get the client IP address 3         String IP = request.getremoteaddr (), 4         out.write (IP), 5%>

This is the benefit of developing and using custom tags, which allows us to not nest Java code on our JSP pages.

Third, the implementation of the custom label process

When the JSP engine encounters a custom label, it first creates an instance object of the label processor class and then calls its method sequentially, according to the communication rules defined by the JSP specification.
1, public void Setpagecontext (PageContext pc), after the JSP engine instantiates the label processor, the Setpagecontext method is called to pass the PageContext object of the JSP page to the label processor. The label processor can then communicate with the JSP page through this PageContext object.
2, public void setParent (Tag t), after the Setpagecontext method executes, the Web container then calls the SetParent method to pass the parent tag of the current label to the current label processor, if the current label does not have a parent tag, The value of the parameter passed to the SetParent method is null.
3, public int dostarttag (), when the Setpagecontext method and the SetParent method are called, the doStartTag method of the label processor is called when the Web container executes to the start tag of the custom label.
4, public int doendtag (), after the Web container executes the label body of the custom label, it then executes the end tag of the custom label, at which point the Web container invokes the Doendtag method of the label processor.
5, public void release (), usually after the Web container finishes executing the custom label, the label processor resides in memory and the Web container calls the release method for the other pull server until the Web app is stopped.

We can find the jsptag_ in the "work\catalina\localhost\javaweb_jsptag_study_20140816\org\apache\jsp" Directory of the Tomcat server. Test1.jsp translated into a servlet Java source code, as shown in:

Opens the Jsptag_005ftest1_jsp.java file, you can see Setpagecontext (PageContext pc), setParent (Tag t), doStartTag (), Doendtag (), Release () These 5 methods call order and procedure.

The code for Jsptag_005ftest1_jsp.java is as follows:

  1 package org.apache.jsp;  2 3 Import javax.servlet.*;  4 Import javax.servlet.http.*;  5 Import javax.servlet.jsp.*; 6 7 Public final class Jsptag_005ftest1_jsp extends Org.apache.jasper.runtime.HttpJspBase 8 implements ORG.APACHE.J  Asper.runtime.JspSourceDependent {9 private static final Jspfactory _jspxfactory = Jspfactory.getdefaultfactory (); Each of the private static java.util.List _jspx_dependants; static {_jspx_dependants = new java.util.ArrayList (1); _jspx_dependants.add ("/web-inf/gacl.tld"); Org.apache.jasper.runtime.TagHandlerPool _005fjspx_005ftagpool_005fxdp_005fviewip_005fnobody; The private javax.el.ExpressionFactory _el_expressionfactory; The private org.apache.AnnotationProcessor _jsp_annotationprocessor; 29 Public Object Getdependants () {_jspx_dependants; n} (public void _jspinit ()} 005fjspx_005ftagpool_005fxdp_005fviewip_005fnobody = Org.apache.jasper.runtiMe. Taghandlerpool.gettaghandlerpool (Getservletconfig ()); _el_expressionfactory = _jspxfactory.getjspapplicationcontext (Getservletconfig (). Getservletcontext ()). Getexpressionfactory (); _jsp_annotationprocessor = (org.apache.AnnotationProcessor) getservletconfig (). Getservletcontext (). getattribute (Org.apache.AnnotationProcessor.class.getName ()); _jspdestroy public void () {_005fjspx_005ftagpool_005fxdp_005fviewip_005fnobody.release (); 36} 3 7. public void _jspservice (HttpServletRequest request, httpservletresponse response) throws Java.io.IOExce Ption, servletexception {PageContext pagecontext = null; HttpSession session = null; Servletcont EXT application = NULL; ServletConfig config = null; JspWriter out = null; The Object page = this; JspWriter _jspx_out = null; PageContext _jspx_page_context = null; A try {response.setcontenttype ("Text/html;charset=utf-8")); PageContext = _jspxfactory.getpagecontext (this, request, response, NULL, True, 8192, true); _jspx_page_context = PageContext; application = Pagecontext.getservletcontext (); Config = Pagecontext.getservletconfig (); The session = Pagecontext.getsession (); Pagecontext.getout = (); _jspx_out = out; Out.write ("\ r \ n"); Out.write ("<!--reference GaCl tag Library, tag library prefix (prefix) can be arbitrarily set, as set here as prefix=\" gacl\ "-->\r\n"); Out.write ("\ r \ n"); Out.write ("<! DOCTYPE html>\r\n "); Out.write ("_jspx_meth_xdp_005fviewip_005f0 (_jspx_page_context)) The return; Out.write ("\ r \ n"); Out.write ("</body>\r\n"); Out.write (" Private Boolean _jspx_meth_xdp_005fviewip_005f0 (PageContext _jspx_page_context) throws Throwable {101 PageContext PageContext = _jspx_page_context;102 JspWriter out = _jspx_page_context.getout (); 103//Xdp:viewIP 104 Me.gacl.web.tag.ViewIPTag _jspx_th_xdp_005fviewip_005f0 = (me.gacl.web.tag.ViewIPTag) _005fjspx_005ftagpool_ 005fxdp_005fviewip_005fnobody.get (me.gacl.web.tag.ViewIPTag.class); _jspx_th_xdp_005fviewip_ 005f0.setpagecontext (_jspx_page_context); 106 _jspx_th_xdp_005fviewip_005f0.setparent (null); 107 int _JSPX_EVAL_XDP _005fviewip_005f0 = _jspx_th_xdp_005fviewip_005f0.dostarttag (); 108 if (_jspx_th_xdp_005fviewip_005f0.doendtag () = = Javax.servlet.jsp.tagext.Tag.SKIP_PAGE) {109 _005fjspx_005ftagpool_005fxdp_005fviewip_005fnobody.reuse (_jspx_th_ XDP_005FVIEWIP_005F0); return true;111}112 _005fjspx_005ftagpool_005fxdp_005fviewip_005fnobody.reuse (_js PX_TH_XDP_005FVIEWIP_005F0); 113 Return false;114}115} 

The following highlights the code in the private Boolean _jspx_meth_xdp_005fviewip_005f0 (PageContext _jspx_page_context) method in which the above code is marked red

①, here is an object that instantiates a VIEWIP tag processor class Me.gacl.web.tag.ViewIPTag

1  //  xdp:viewip2     me.gacl.web.tag.ViewIPTag _jspx_th_xdp_005fviewip_005f0 = ( ME.GACL.WEB.TAG.VIEWIPTAG) _005fjspx_005ftagpool_005fxdp_005fviewip_005fnobody.get ( Me.gacl.web.tag.ViewIPTag.class);

②, after instantiating the label processor, call the Setpagecontext method to pass the PageContext object of the JSP page to the label processor

1 _jspx_th_xdp_005fviewip_005f0. Setpagecontext (_jspx_page_context);

After the ③, Setpagecontext method executes, the SetParent method that is called then passes the parent tag of the current label to the current label processor, and the parameter value passed to the SetParent method is null if the current label does not have a parent tag

1 _jspx_th_xdp_005fviewip_005f0. setParent (null);

④, the Setpagecontext method and the SetParent method are called, and the Web container executes the start tag of the custom label, invoking the doStartTag method of the label processor

1 int _jspx_eval_xdp_005fviewip_005f0 = _jspx_th_xdp_005fviewip_005f0. doStartTag ();

⑤, the Web container executes the label body of the custom label and then executes the end tag of the custom label, at which point the Web container invokes the Doendtag method of the label processor

1 if (_jspx_th_xdp_005fviewip_005f0.  Doendtag () = = Javax.servlet.jsp.tagext.Tag.SKIP_PAGE)

This is the process of customizing the label's execution.

Here is an entry-level case to explain Javaweb's custom label development, which will be described in more detail later in the blog post.

Javaweb Base (_jsp) Custom Label

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.