Development custom tags in JSP Technology

Source: Internet
Author: User
Tags tld

Because a line of Java code is not used in JSP, labels must be used to remove the displayed Java code. The development process of labels is listed below.
 
1. First write a java class that implements the tag Interface
 
Package com. bird. web. tag;
 
Import java. io. IOException;
 
Import javax. servlet. http. HttpServletRequest;
Import javax. servlet. jsp. JspException;
Import javax. servlet. jsp. JspWriter;
Import javax. servlet. jsp. tagext. TagSupport;
 
/**
* Display custom tags of visitors' IP addresses
* @ Author Bird
*
*/
Public class ViewIPTag extends TagSupport {
 
Private static final long serialVersionUID = 1L;


@ Override
Public int doStartTag () throws JspException {

HttpServletRequest request = (HttpServletRequest) this. pageContext. getRequest ();
JspWriter out = this. pageContext. getOut ();

String ip = request. getRemoteAddr ();
Try {
Out. print (ip );
} Catch (IOException e ){
Throw new RuntimeException (e );
}


Return super. doStartTag ();
}
}
Package com. bird. web. tag;
 
Import java. io. IOException;
 
Import javax. servlet. http. HttpServletRequest;
Import javax. servlet. jsp. JspException;
Import javax. servlet. jsp. JspWriter;
Import javax. servlet. jsp. tagext. TagSupport;
 
/**
* Display custom tags of visitors' IP addresses
* @ Author Bird
*
*/
Public class ViewIPTag extends TagSupport {
 
Private static final long serialVersionUID = 1L;


@ Override
Public int doStartTag () throws JspException {

HttpServletRequest request = (HttpServletRequest) this. pageContext. getRequest ();
JspWriter out = this. pageContext. getOut ();

String ip = request. getRemoteAddr ();
Try {
Out. print (ip );
} Catch (IOException e ){
Throw new RuntimeException (e );
}


Return super. doStartTag ();
}
}
 
 
2. Describe the tag processor class in the tld file. The tld file is located under web-inf and can be copied under tomcat's webapps.
 
 
 
<? 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">
<Description> A tag library exercising SimpleTag handlers. </description>
<Tlib-version> 1.0 </tlib-version>
<Short-name> Bird </short-name>
<Uri> http://www.bird.net </uri>
<Tag>
<Name> viewIP </name>
<Tag-class> com. bird. web. tag. ViewIPTag </tag-class>
<Body-content> empty </body-content>
</Tag>
</Taglib>
<? 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">
<Description> A tag library exercising SimpleTag handlers. </description>
<Tlib-version> 1.0 </tlib-version>
<Short-name> Bird </short-name>
<Uri> http://www.bird.net </uri>
<Tag>
<Name> viewIP </name>
<Tag-class> com. bird. web. tag. ViewIPTag </tag-class>
<Body-content> empty </body-content>
</Tag>
</Taglib>
 
4. Use the tablib command on the interface to import and use tags,
 
 
 
<% @ Page language = "java" import = "java. util. *" pageEncoding = "UTF-8" contentType = "text/html; charset = UTF-8" %>
<% @ Taglib uri = "http://www.bird.net" prefix = "bird" %>
<! Doctype html public "-// W3C // dtd html 4.01 Transitional // EN">
<Html>
<Head>
<Title> A simple custom tag </title>

</Head>

<Body>
The IP address you access is: <bird: viewIP/>
</Body>
</Html>
<% @ Page language = "java" import = "java. util. *" pageEncoding = "UTF-8" contentType = "text/html; charset = UTF-8" %>
<% @ Taglib uri = "http://www.bkjia.com" prefix = "bird" %>
<! Doctype html public "-// W3C // dtd html 4.01 Transitional // EN">
<Html>
<Head>
<Title> A simple custom tag </title>

</Head>
 
<Body>
The IP address you access is: <bird: viewIP/>
</Body>
</Html>
 
 


Author: Xiao fenqing

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.