Custom Label, my first custom label!
A total of two steps
- Write a Java class that implements the tag interface and move the Java code in the JSP page to this class (Tag processor Class)
- 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.jspIn 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.tldTo 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.javaEffect:
Java_ Custom Label, my first custom label!