Java_ Custom Label, my first custom label!

Source: Internet
Author: User
Tags tld

Custom Label, my first custom label!

A total of two steps
    1. Write a Java class that implements the tag interface and move the Java code in the JSP page to this class (Tag processor Class)
    2. Write a tag library descriptor (TLD) file that describes the label processor class as a label in the TLD file
I. Cases,

Output Client IP

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

In the Web-inf directory, create a TLD file that:

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 Han dlers.</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

To write a label processor class:

Viewiptag.java

  
 PackageTag;Importjava.io.IOException;Importjavax.servlet.http.HttpServletRequest;Importjavax.servlet.jsp.JspException;ImportJavax.servlet.jsp.JspWriter;ImportJavax.servlet.jsp.tagext.TagSupport;  Public classViewiptagextendstagsupport{ Public intdoStartTag ()throwsjspexception{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 blockE.printstacktrace (); }                   return Super. doStartTag (); }}
Viewiptag.java

Effect:

  

Java_ Custom Label, my first custom label!

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.