首页 > 其他 > 详细

自定义标签--显示客户端的ip

时间:2015-12-08 18:21:10      阅读:164      评论:0      收藏:0      [点我收藏+]

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@ taglib uri="http://www.hbsi.com.cn" prefix="wm" %> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html>   <head>     <base href="<%=basePath%>">         <title>My JSP ‘tag1.jsp‘ starting page</title>      <meta http-equiv="pragma" content="no-cache">  <meta http-equiv="cache-control" content="no-cache">  <meta http-equiv="expires" content="0">     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">  <meta http-equiv="description" content="This is my page">  <!--  <link rel="stylesheet" type="text/css" href="styles.css">  -->

  </head>     <body>     <%--自定义标签主要用于移除Jsp页面中的java代码。

   使用自定义标签移除jsp页面中的java代码,只需要完成以下两个步骤:    编写一个实现Tag接口的Java类(标签处理器类)。    编写标签库描述符(tld)文件,在tld文件中对标签处理器类进行描述。   --%>      您的ip是:   <wm:viewIP></wm:viewIP>   <%-- <%    String ip = request.getRemoteAddr();    out.println(ip);    %> --%>   </body> </html>

 

 

package cn.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.Tag; import javax.servlet.jsp.tagext.TagSupport;

/**  * 标签处理器的类  * @author 萌  *  */ public class ViewIP extends TagSupport{    @Override  public int doStartTag() throws JspException {   // TODO Auto-generated method stub      HttpServletRequest request = (HttpServletRequest) pageContext.getRequest();   String ip = request.getRemoteAddr();      JspWriter out = this.pageContext.getOut();   try {    out.println(ip);   } catch (IOException e) {    // TODO Auto-generated catch block    throw new RuntimeException(e);   }      return Tag.EVAL_BODY_INCLUDE;//返回的常量值是:是不是返回标签体   //return Tag.SKIP_BODY;//返回的常量值是:跳过标签体,不执行标签体的内容  }

}

 

 

<?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>hbsi</short-name>     <uri>http://www.hbsi.com.cn</uri>     <tag><!-- 用来自定义标签 -->         <description>IP</description>         <name>viewIP</name><!-- 指定自定义标签的名称 -->         <tag-class>cn.web.tag.ViewIP</tag-class><!-- 指明标签处理类的完全限定名 -->         <body-content>empty</body-content><!-- 用来指明标签体的内容,empty意味着标签体为空 -->     </tag>     <tag><!-- 用来自定义标签 -->         <description>IP</description>         <name>tag2</name><!-- 指定自定义标签的名称 -->         <tag-class>cn.web.tag.TagSkipDemo2</tag-class><!-- 指明标签处理类的完全限定名 -->         <body-content>JSP</body-content><!-- 用来指明标签体的内容,empty意味着标签体为空 -->     </tag>     </taglib>

自定义标签--显示客户端的ip

原文:http://www.cnblogs.com/mengwh/p/5029672.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!