Servlet & JSP (16)

Source: Internet
Author: User
Tags tld

In the previous article, we discussed custom tags and completed a simple empty tag instance by implementing the tag interface. In this article, we first use an instance to implement an attribute tag. This instance implements a <max> label to calculate the maximum values of two numbers. We will not go into details about the project establishment process here. If you have any questions, please refer to the previous content. Directly paste the code. The main code is as follows:

Compile maxtag. Java

Package COM. shan. tag; import Java. io. *; import javax. servlet. JSP. *; import javax. servlet. JSP. tagext. *; public class maxtag extends tagsupport {private int numbera; private int numberb; Public void setnumbera (INT numbera) {This. numbera = numbera;} public void setnumberb (INT numberb) {This. numberb = numberb;} public int doendtag () throws jspexception {// use the getout () method of the pagecontext object to obtain jspwriter object jspwriter out = Pagecontext. getout (); try {out. println (numbera> numberb? Numbera: numberb);} catch (ioexception e) {system. Out. println (E. tostring ();} return eval_page;} public void release (){}}

Compile running statements

javac -classpath D:\apache-tomcat-7.0.33\lib\jsp-api.jar;classes -d classes src\com\shan\tag\MaxTag.java  

Configure the <max> tag in TLD

<?xml version="1.0" encoding="ISO-8859-1" ?><!DOCTYPE taglib        PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN"        "http://java.sun.com/j2ee/dtd/web-jsptaglibrary_1_2.dtd"><taglib>  <tlib-version>1.0</tlib-version>  <jsp-version>1.2</jsp-version>  <short-name>mytag</short-name>  <display-name>MyTag</display-name>  <description>My Tag library.</description>  <tag>    <name>max</name>    <tag-class>com.shan.tag.MaxTag</tag-class>    <body-content>empty</body-content>    <attribute>      <name>numberA</name>      <required>true</required>      <rtexprvalue>true</rtexprvalue>    </attribute>    <attribute>      <name>numberB</name>      <required>true</required>      <rtexprvalue>true</rtexprvalue>    </attribute>  </tag></taglib>

Configure the Web. xml file

<?xml version='1.0' encoding='utf-8'?><web-app xmlns="http://java.sun.com/xml/ns/javaee"  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  xsi:schemaLocation="http://java.sun.com/xml/ns/javaee                      http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"  version="3.0"  metadata-complete="true"><jsp-config><taglib><taglib-uri>/mytag</taglib-uri><taglib-location>/WEB-INF/MyTaglib.tld</taglib-location></taglib></jsp-config></web-app>

Compile the test page

<% @ Page contenttype = "text/html; charset = gb2312 "%> <% @ taglib uri ="/mytag "prefix =" mytag "%> <% int numbera = integer. parseint (request. getparameter ("numbera"); int numberb = integer. parseint (request. getparameter ("numberb"); out. println (numbera); out. println (numberb); %> maximum value: <mytag: Max numbera = "<% = numbera %>" numberb = "<% = numberb %>"/>

In the browser URL bar, enter http: // localhost: 8080/maxtag/test. jsp? Numbera = 24 & numberb = 96. The running result is as follows:

The label that implements simpletag interface is called simple tag, and the label that implements other interfaces or classes is called traditional tag. In traditional tag development, you need to select the interface to be implemented or the class to be inherited based on the tag function, and also consider the return value of the method. To simplify tag development, we use simple tags to implement the desired functions.

Simpletag Interface

The life cycle of the label Processor Implementing the simpletag interface is as follows:

1) After the container creates a tag processor instance, call setjspcontext () to set jspcontext. If the tag is not nested, The setparent () method is not called. If the tag is nested, The setparent () method is called to set its parent tag. Unlike traditional tag processing, a traditional tag calls the setparent () method regardless of whether the tag is nested.
2) Call the setxxx () method of the tag processor to set the tag attributes. skip this step if no attribute exists.
3) if the TAG body exists, call setjspbody () to set the TAG body. If the TAG body does not exist, skip this step.
4) The container calls the dotag () method to complete the main logic of the tag processor.
Note that the label processor of a simple tag is not cached and used repeatedly. Whenever a tag is encountered, the container creates a tag processor instance, which is different from the traditional tag processor.

To simplify tag development, the javax. servlet. jsp. tagext package also provides simpletag interface implementation class simpletagsupport. During development, you only need to inherit the class and then rewrite the dotag () method.

In this case, we will stop providing instances. If you are interested, you can refer to the Development of Traditional labels to write programs for simple tag development.

Reprinted please indicate the source: http://blog.csdn.net/iAm333

Related Article

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.