Java _ custom tag, my first custom tag !, Java labels

Source: Internet
Author: User
Tags tld

Java _ custom tag, my first custom tag !, Java labels

Custom tag, my first custom tag!

 

Two steps in total. Case study,

Output Client IP Address

 

ViewIP. jsp

   
<% @ Page language = "java" contentType = "text/html; charset = UTF-8" pageEncoding = "UTF-8" %> <% @ taglib uri = "/WEB-INF/firstTag. tld "prefix =" itcast "%> <! DOCTYPE html PUBLIC "-// W3C // dtd html 4.01 Transitional // EN" "http://www.w3.org/TR/html4/loose.dtd"> ViewIP. jsp

 

 

Create a tld file in the WEB-INF directory ,:

FirstTag. tld

  
<?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>view</short-name>    <uri>/WEB-INF/firstTag.tld</uri>    <tag>        <name>viewIP</name>        <tag-class>Tag.viewIPTag</tag-class>        <body-content>empty</body-content>    </tag>   </taglib>
FirstTag. tld

 

 

Compile the tag processor class:

ViewIPTag. java

  
package 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;  public class viewIPTag extends TagSupport{                 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) {                            // TODO Auto-generated catch block                            e.printStackTrace();                   }                   return super.doStartTag();         }}
ViewIPTag. java

 

Effect:

  

 

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.