Servlet custom tag development steps, servlet custom tags

Source: Internet
Author: User

Servlet custom tag development steps, servlet custom tags
1. Why do I need to use custom tags?
(1) integrated with JSP pages
(2) It also has certain business logic functions, such as loops and CIDR blocks...

(3) Replace <%>

2. Step 3 of custom tag development ):

(1) customize a tag processing class and implement the SimpleTag Interface

Package cn. wenhao. www. web. serlvet. el; import java. io. IOException; import javax. servlet. servletRequest; import javax. servlet. jsp. jspContext; import javax. servlet. jsp. jspException; import javax. servlet. jsp. jspWriter; import javax. servlet. jsp. pageContext; import javax. servlet. jsp. tagext. jspFragment; import javax. servlet. jsp. tagext. jspTag; import javax. servlet. jsp. tagext. simpleTag;/*** class: defined tag processing class *** @ autho R yiye Binzhou * @ version 1.0 * @ Creation Time: 10:25:32 */public class IpTag implements SimpleTag {private PageContext context; // The Order @ Overridepublic void doTag () throws JspException, IOException {System. out. println ("execution order of doTag ...... "); ServletRequest request = context. getRequest (); String addr = request. getRemoteAddr (); String host = request. getRemoteHost (); int port = request. getRemotePort (); JspWriter out = context. getOut (); out. write ("<font size = '36' color = 'blue'>" + addr + ":" + host + ": "+ port +" </font> ") ;}@ Overridepublic JspTag getParent () {System. out. println ("execution order of doTag ...... "); Return null ;}@ Overridepublic void setJspBody (JspFragment arg0) {System. out. println (" execution of setJspContext ...... ");} // The web Container will call it first. Step 1 @ Overridepublic void setJspContext (JspContext pc) {System. out. println (" execution of setJspContext ...... "); Context = (PageContext) pc;} @ Overridepublic void setParent (JspTag arg0) {System. out. println (" execution of setParent ...... ");}}


(2) In the/WEB-INF/directory, write a *. tld file, the purpose is to let the Web Container know the correspondence between the custom label and the label processing class


<?xml version="1.0" encoding="UTF-8" ?><taglib xmlns="http://java.sun.com/xml/ns/j2ee"    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd"    version="2.0">  <tlib-version>1.0</tlib-version>  <short-name>simple</short-name>  <uri>http://java.sun.com/jsp/jstl/simple</uri>    <tag>    <name>ip</name>    <tag-class>cn.wenhao.www.web.serlvet.el.IpTag</tag-class>    <body-content>empty</body-content>  </tag>   </taglib>

(3). On the JSP page, use the <% @ taglib %> command to reference the tag library.

<% @ Page language = "java" import = "java. util. * "pageEncoding =" UTF-8 "%> <% @ taglib uri =" http://java.sun.com/jsp/jstl/simple "prefix =" simple "%> <% String path = request. getContextPath (); String basePath = request. getScheme () + ": //" + request. getServerName () + ":" + request. getServerPort () + path + "/"; %> <! Doctype html public "-// W3C // dtd html 4.01 Transitional // EN"> 

Running result diagram:


3. SimpleTag Method Introduction

(1). setJspContext Method
It is used to pass the pageContext object of the JSP page to the tag processor object.
(2). setParent Method
Used to pass the parent Tag Processor object to the current Tag Processor object
(3). getParent Method
Parent Tag Processor object used to obtain the current tag
(4). setJspBody Method
It is used to pass the JspFragment object (that is, the content of the label body) representing the label body to the label processor object.
(5) doTag Method
It is used to complete all tag logic, including output, iteration, and modification of TAG body content. In the doTag method, a javax. servlet. jsp. SkipPageException exception can be thrown to notify the WEB Container not to execute the content after the ending mark on the JSP page.

4. execution sequence of SimpleTag interface methods

(1) When the web Container starts to execute tags, the following method is called to complete tag initialization:

The WEB Container calls the setJspContext method of the tag processor object to pass the pageContext object representing the JSP page to the tag processor object.

The WEB Container calls the setParent method of the tag processor object and passes the parent Tag Processor object to the tag processor object.

Note that the WEB Container will call this method only when the tag has a parent tag.

If an attribute is set when a tag is called, the container will call the setter method corresponding to each attribute to pass the attribute value to the tag processor object. If the attribute value of a tag is an EL expression or a script expression

The container first calculates the expression value and then passes the value to the tag processor object.

If a simple tag has a label body, the container will call the setJspBody method to pass in the JspFragment object representing the label body.

(2). When executing tags:
The container calls the doTag () method of the tag processor. by operating the JspFragment object in the method body, developers can execute, iterate, and modify the TAG body.


Java custom tag compilation process

1: Write a servlet to process the tag logic and generate html code.
2: Write a tld file to define the attributes and format of tags.
3: Configure tags in web. xml
4: Add tags to jsp pages
You can use it. You can find the specific steps on the Internet.
 
How to Write custom tags

Paste an article and make it necessary to popularize it. 1. what is a custom tag? A: 1) a custom Java language element is essentially a JavaBean that runs one or two interfaces; 2) it can be very confidential to be associated with the JSP representation logic, and has the same business logic processing capability as the common JavaBean; 2) When a JSP page is changed to a servlet, in the meantime, user-defined tags are converted into an object called Tag hander; 3) default objects can be operated, form data can be processed, databases can be accessed, and other enterprise services can be operated; 2. features of the custom tag library answer: 1) customize by passing parameters on the call page; 2) access all possible objects on the JSP page; 3) modify the response generated on the call page; 4) custom tags can communicate with each other. 5) complex interaction can be achieved through tag nesting on the same JSP page. 3. how to Use the custom tag library answer: 1) Declare the tag library 2) Make the tag library execution available to Web applications 4. declaration label library answer: 1) Use the taglib command to declare the label library 2) Syntax: <% @ taglib uri = "URI" prefix = "pre" %> Note:. the uri attribute can be absolute or relative URL, which points to the tag library Descriptor (TLD) file; B. the uri attribute can also be a URL that does not exist. The URL is web. the xml file maps the absolute URL of the library Descriptor (TLD) file to the local system; 3) Example: <% @ taglib uri = "/WEB-INF/template. tld "prefix =" test "%> <% @ taglib uri =" java.sun.com/jstl/core "prefix =" core "%> 5. make the tag library executable answer: Method 1: deploy the tag handler class under the WEB-INF/classes directory Method 2: Package the markup handler class into a jar file and place it in the WEB-INF/lib directory. 6. Several Typical tags (question: can there be tags with subject but no attribute ?) Answer: 1) Simple tags without attributes and subject:; 2) tags without subject but attributes:; 3) tags with subject and attributes :... // TAG body; Note:. the attribute is listed in the start tag, which is specified in the tag library Descriptor (TLD) file and serves the User-Defined behavior of the tag Library; B. the tag body is located between the start tag and end tag. It can be any legal JSP content or tag. 7. define tags answer: 1) Develop and implement the tag class (tag handler); 2) edit the tag library Descriptor (TLD) file; 3) on the web. in xml, A ing is created for the absolute URL of the tag library Descriptor (TLD) file (this step is optional); 8. tag library Descriptor (TLD) file answer: 1) an XML file describing the tag Library; 2) the content begins with the description of the entire library, and then ...... remaining full text>
 

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.