欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

JSP技术中的开发自定义标签

程序员文章站 2022-04-15 13:29:18
因为在jsp中不要使用一行java代码,所以说必须使用标签来移除显示的java代码,下面罗列一下标签的开发过程。   1.首先编写一个实现tag接口的java类   packag...
因为在jsp中不要使用一行java代码,所以说必须使用标签来移除显示的java代码,下面罗列一下标签的开发过程。
 
1.首先编写一个实现tag接口的java类
 
package com.bird.web.tag; 
 
import java.io.ioexception; 
 
import javax.servlet.http.httpservletrequest; 
import javax.servlet..jspexception; 
import javax.servlet.jsp.jspwriter; 
import javax.servlet.jsp.tagext.tagsupport; 
 
/**
 * 显示来访者ip的自定义标签
 * @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;
 
/**
 * 显示来访者ip的自定义标签
 * @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.在tld文件里面对标签处理器类进行描述,tld文件的位置在web-inf下面可以在tomcat的webapps下面抄一下
 
 
 
<?xml version="1.0" encoding="utf-8" ?> 
 
<taglib xmlns="https://java.sun.com/xml/ns/j2ee" 
    xmlns:xsi="https://www.w3.org/2001/xmlschema-instance" 
    xsi:schemalocation="https://java.sun.com/xml/ns/j2ee https://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>https://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="https://java.sun.com/xml/ns/j2ee"
    xmlns:xsi="https://www.w3.org/2001/xmlschema-instance"
    xsi:schemalocation="https://java.sun.com/xml/ns/j2ee https://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>https://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.在界面里面使用tablib命令到导入和使用标签就可以了,
 
 
 
<%@ page language="java" import="java.util.*" pageencoding="utf-8" contenttype="text/html; charset=utf-8"%> 
<%@taglib uri="https://www.bird.net" prefix="bird" %> 
<!doctype html public "-//w3c//dtd html 4.01 transitional//en"> 
<html> 
  <head> 
    <title>一个简单的自定义标签</title> 
     
  </head> 
   
  <body> 
   您访问的ip为: <bird:viewip/> 
  </body> 
</html> 
<%@ page language="java" import="java.util.*" pageencoding="utf-8" contenttype="text/html; charset=utf-8"%>
<%@taglib uri="" prefix="bird" %>
<!doctype html public "-//w3c//dtd html 4.01 transitional//en">
<html>
  <head>
    <title>一个简单的自定义标签</title>
   
  </head>
 
  <body>
   您访问的ip为: <bird:viewip/>
  </body>
</html>
 
 


作者 小愤青