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