【
<c:filterHTML>
<a href="">链接</a>
</c:filterHTML>
】
应用场景是需要对HTML进行一个转义显示源代码
实现步骤如下:
//注:下面filter方法可以再apache服务器中找到.
//\apache-tomcat-6.0.30\apache-tomcat-6.0.30\webapps\examples\WEB-INF\classes\util\HTMLFilter
第一步:编写一个标签处理器类
public class Transferred extends SimpleTagSupport{
public void doTag() throws JspException, IOException {
JspFragment body=getJspBody();
StringWriter sw=new StringWriter();
body.invoke(sw);
String content=sw.getBuffer().toString();
content=filter(content);
getJspContext().getOut().write(content);
}
public String filter(String message) {
if (message == null)
return (null);
char content[] = new char[message.length()];
message.getChars(0, message.length(), content, 0);
StringBuffer result = new StringBuffer(content.length + 50);
for (int i = 0; i < content.length; i++) {
switch (content[i]) {
case '<':
result.append("<");
break;
case '>':
result.append(">");
break;
case '&':
result.append("&");
break;
case '"':
result.append(""");
break;
default:
result.append(content[i]);
}
}
return (result.toString());
}
}
第二步:编写一个标签描述文件*.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>SimpleTagLibrary</short-name>
<uri>http://www.liyong.costomtag</uri>
<tag>
<description>show client IP</description>
<name>filterHTML</name>
<tag-class>com.liyong.transferred.Transferred</tag-class>
<!-- 标签体为空 这与传统标签同 empty -->
<body-content>scriptless</body-content>
</tag>
</taglib>
第三步:编写一个jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@taglib uri="http://www.liyong.costomtag" prefix="c" %>
<%
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 'transferred.jsp' starting page</title>
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
//使用自定义过滤标签
<c:filterHTML>
<a href="">链接</a>
</c:filterHTML>
</body>
</html>
第四步:测试...
<c:filterHTML>
<a href="">链接</a>
</c:filterHTML>
】
应用场景是需要对HTML进行一个转义显示源代码
实现步骤如下:
//注:下面filter方法可以再apache服务器中找到.
//\apache-tomcat-6.0.30\apache-tomcat-6.0.30\webapps\examples\WEB-INF\classes\util\HTMLFilter
第一步:编写一个标签处理器类
public class Transferred extends SimpleTagSupport{
public void doTag() throws JspException, IOException {
JspFragment body=getJspBody();
StringWriter sw=new StringWriter();
body.invoke(sw);
String content=sw.getBuffer().toString();
content=filter(content);
getJspContext().getOut().write(content);
}
public String filter(String message) {
if (message == null)
return (null);
char content[] = new char[message.length()];
message.getChars(0, message.length(), content, 0);
StringBuffer result = new StringBuffer(content.length + 50);
for (int i = 0; i < content.length; i++) {
switch (content[i]) {
case '<':
result.append("<");
break;
case '>':
result.append(">");
break;
case '&':
result.append("&");
break;
case '"':
result.append(""");
break;
default:
result.append(content[i]);
}
}
return (result.toString());
}
}
第二步:编写一个标签描述文件*.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>SimpleTagLibrary</short-name>
<uri>http://www.liyong.costomtag</uri>
<tag>
<description>show client IP</description>
<name>filterHTML</name>
<tag-class>com.liyong.transferred.Transferred</tag-class>
<!-- 标签体为空 这与传统标签同 empty -->
<body-content>scriptless</body-content>
</tag>
</taglib>
第三步:编写一个jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@taglib uri="http://www.liyong.costomtag" prefix="c" %>
<%
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 'transferred.jsp' starting page</title>
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
//使用自定义过滤标签
<c:filterHTML>
<a href="">链接</a>
</c:filterHTML>
</body>
</html>
第四步:测试...