代码语言
.
CSharp
.
JS
Java
Asp.Net
C
MSSQL
PHP
Css
PLSQL
Python
Shell
EBS
ASP
Perl
ObjC
VB.Net
VBS
MYSQL
GO
Delphi
AS
DB2
Domino
Rails
ActionScript
Scala
代码分类
文件
系统
字符串
数据库
网络相关
图形/GUI
多媒体
算法
游戏
Jquery
Extjs
Android
HTML5
菜单
网页交互
WinForm
控件
企业应用
安全与加密
脚本/批处理
开放平台
其它
【
Java
】
整站过滤器
作者:
Dezai.CN
/ 发布于
2011/6/10
/
1211
<div>** * @author * @company leemenz (C) copyright * @time Dec 18, 2006 2:08:18 PM * @version 1.0.0.0 * @package com */ package com;</div> import java.io.IOException; import java.io.PrintWriter;</div> import javax.servlet.Filter; import javax.servlet.FilterChain; import javax.servlet.FilterConfig; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; import javax.servlet.http.HttpServletRequest;</div> <div>/** * @author Administrator * * 这个过滤器用来过滤黑名单中的用户不能留言 * */ public class NoteFilter implements Filter {</div> <div>private FilterConfig config = null;</div> <div>private String blackList = null;</div> <div>/* * (non-Javadoc) * * @see javax.servlet.Filter#destroy() */ public void destroy() { // TODO Auto-generated method stub config = null; } <div>/* * (non-Javadoc) * * @see javax.servlet.Filter#doFilter(javax.servlet.ServletRequest, * javax.servlet.ServletResponse, javax.servlet.FilterChain) */ public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { // TODO Auto-generated method stub String username = request.getParameter("username"); if (username != null) username = new String(request.getParameter("username").getBytes( "ISO8859-1"), "GB2312"); if (username != null && username.indexOf(blackList) != -1) { response.setContentType("text/html;charset=GB2312"); PrintWriter out = response.getWriter(); out.println("<html><head></head><body>"); out.println("<h1>对不起," + username + ",您没有留言的权限!"); out.println("</h1></body></html>"); out.flush(); return; } <div>long before = System.currentTimeMillis(); config.getServletContext() .log("NoteFilter:before call chain.Filter();"); chain.doFilter(request, response); config.getServletContext().log("NoteFilter:after call chain.Filter();"); long end = System.currentTimeMillis(); String name = ""; if (request instanceof javax.servlet.http.HttpServletRequest) { name = ((HttpServletRequest) request).getRequestURI(); } config.getServletContext().log( "NoteFilter " + name + " : " + (end - before) + " ms"); } <div>/* * (non-Javadoc) * * @see javax.servlet.Filter#init(javax.servlet.FilterConfig) */ public void init(FilterConfig arg0) throws ServletException { // TODO Auto-generated method stub this.config = arg0; this.blackList = config.getInitParameter("blackList"); } <div>} <div> /** * @author * @company leemenz (C) copyright * @time Dec 18, 2006 2:08:18 PM * @version 1.0.0.0 * @package com */ package com;</div> import java.io.IOException;</div> import javax.servlet.ServletException; import javax.servlet.ServletOutputStream; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse;</div> <div>/* *测试页面 */ public class NoteServlet extends HttpServlet {</div> <div>public static final String CONTENT_TYPE = "text/html;charset=GB2312";</div> <div>/** * Constructor of the object. */ public NoteServlet() { super(); } <div>/** * Destruction of the servlet. */ public void destroy() { super.destroy(); // Just puts "destroy" string in log // Put your code here } <div>/** * The doDelete method of the servlet. * * This method is called when a HTTP delete request is received. * * @param request * the request send by the client to the server * @param response * the response send by the server to the client * @throws ServletException * if an error occurred * @throws IOException * if an error occurred */ public void doDelete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {</div> <div>// Put your code here } <div>/** * The doGet method of the servlet. * * This method is called when a form has its tag value method equals to get. * * @param request * the request send by the client to the server * @param response * the response send by the server to the client * @throws ServletException * if an error occurred * @throws IOException * if an error occurred */ public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {</div> <div>response.setContentType(CONTENT_TYPE); ServletOutputStream out = response.getOutputStream(); out .println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">"); out.println("<HTML>"); out.println(" <HEAD><TITLE>留言簿</TITLE></HEAD>"); out.println(" <BODY>");</div> <div>String name = request.getParameter("username"); String content = request.getParameter("content");</div> if (name != null) name = new String(name.getBytes("ISO-8859-1"), "GB2312"); if (content != null) content = new String(content.getBytes("ISO-8859-1"), "GB2312");</div> if (content != null) out.println(" " + name + "的留言为:" + content);</div> <div>out.println("<form method=\"post\" action=\"" + request.getContextPath() + "/servlet/NoteServlet\">"); out.println("<b>姓名:</b><input type=\"text\" name=\"username\">"); out .println("<b>内容:</b><textArea name=\"content\" row=\"5\" cols=\"20\" wrap></textArea>"); out.println("<input type=\"submit\" name=\"提交\" value=\"提交\">"); out.println("<input type=\"reset\" name=\"重填\" value=\"重填\">"); out.println(" </BODY>"); out.println("</HTML>"); out.flush(); out.close(); } <div>/** * The doPost method of the servlet. * * This method is called when a form has its tag value method equals to * post. * * @param request * the request send by the client to the server * @param response * the response send by the server to the client * @throws ServletException * if an error occurred * @throws IOException * if an error occurred */ public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {</div> <div>this.doGet(request, response); } <div>/** * Initialization of the servlet. * * @throws ServletException * if an error occure */ public void init() throws ServletException { // Put your code here } <div>} <div> /** * @author * @company leemenz (C) copyright * @time Dec 18, 2006 3:58:53 PM * @version 1.0.0.0 * @package com */ package com;</div> import java.io.IOException;</div> import javax.servlet.ServletOutputStream; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponseWrapper;</div> <div>/** * @author Administrator * 这个类继承HttpServletResponseWrapper ,并重写相应的方法 */ public class ReplaceServletWapper extends HttpServletResponseWrapper {</div> <div>private ReplaceTextStream tpStream;</div> <div>/** * @param arg0 */ public ReplaceServletWapper(HttpServletResponse arg0, String searchText, String replaceText) throws IOException { // TODO Auto-generated constructor stub super((HttpServletResponse)arg0); tpStream = new ReplaceTextStream(arg0.getOutputStream(),searchText,replaceText); } <div>public ServletOutputStream getOutputStream() throws IOException { return tpStream; } <div>} <div> /** * @author * @company leemenz (C) copyright * @time Dec 18, 2006 2:08:18 PM * @version 1.0.0.0 * @package com */ package com;</div> import java.io.IOException;</div> import javax.servlet.Filter; import javax.servlet.FilterChain; import javax.servlet.FilterConfig; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; import javax.servlet.http.HttpServletResponse;</div> <div>/** * @author Administrator * * 这个类用来过滤留言或者是用户名中的非法内容 * */ public class ReplaceTextFilter implements Filter {</div> <div>private FilterConfig config;</div> <div>private String searchStr;</div> <div>private String replaceStr;</div> <div>/* * (non-Javadoc) * * @see javax.servlet.Filter#destroy() */ public void destroy() { // TODO Auto-generated method stub config = null; } <div>/* * (non-Javadoc) * * @see javax.servlet.Filter#doFilter(javax.servlet.ServletRequest, * javax.servlet.ServletResponse, javax.servlet.FilterChain) */ public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { // TODO Auto-generated method stub ReplaceServletWapper myWrappered = new ReplaceServletWapper( (HttpServletResponse) response, searchStr, replaceStr); config.getServletContext().log( "ReplaceTextFilter : before called doFilter"); chain.doFilter(request, myWrappered); config.getServletContext().log( "ReplaceTextFilter : after called doFilter"); myWrappered.getOutputStream().close(); } <div>/* * (non-Javadoc) * * @see javax.servlet.Filter#init(javax.servlet.FilterConfig) */ public void init(FilterConfig arg0) throws ServletException { // TODO Auto-generated method stub this.config = arg0; this.searchStr = config.getInitParameter("search"); this.replaceStr = config.getInitParameter("replace"); } <div>} <div> /** * @author * @company leemenz (C) copyright * @time Dec 18, 2006 3:33:51 PM * @version 1.0.0.0 * @package com */ package com;</div> import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.OutputStream;</div> import javax.servlet.ServletOutputStream;</div> <div>/** * @author Administrator * 这个类继承ServletOutputStream ,并重写相应的方法 */ public class ReplaceTextStream extends ServletOutputStream {</div> <div>private OutputStream intStream;</div> <div>private ByteArrayOutputStream baStream;</div> <div>private boolean closed = false;</div> <div>private String newStr;</div> <div>private String oldStr;</div> <div>/** * */ public ReplaceTextStream(OutputStream outStream, String strSearch, String strReplace) { // TODO Auto-generated constructor stub intStream = outStream; baStream = new ByteArrayOutputStream(); oldStr = strSearch; newStr = strReplace; } <div>/* * (non-Javadoc) * * @see java.io.OutputStream#write(int) */ public void write(int b) throws IOException { // TODO Auto-generated method stub baStream.write(b); } <div>/** * 重写方法 */ public void println(String s) throws IOException { s = s + "\n"; byte[] bs = s.getBytes(); baStream.write(bs); } <div>/** * 重写方法 */ public void close() throws IOException { if (!closed) { processStream(); intStream.close(); closed = true; } } <div>/** * 重写方法 */ public void flush() throws IOException { if (baStream.size() != 0) { processStream(); baStream = new ByteArrayOutputStream(); } } <div>/** * 重写 processStream 方法 * * @throws IOException */ public void processStream() throws IOException { intStream.write(replaceContent(baStream.toByteArray())); } <div>/** * 替换所有的目的词语 * * @param inBytes * @return */ public byte[] replaceContent(byte[] inBytes) { String retVal = ""; String tpString = new String(inBytes); String srchString = (new String(inBytes)).toLowerCase(); int endBody = srchString.indexOf(oldStr); if (endBody != -1) { retVal = tpString.replaceAll(oldStr, newStr); } else retVal = tpString; return retVal.getBytes(); } <div>} <div> <?xml version="1.0" encoding="GB2312"?> <web-app version="2.4" xmlns="<a href="http://java.sun.com/xml/ns/j2ee">http://java.sun.com/xml/ns/j2ee</a>" xmlns:xsi="<a href="http://www.w3.org/2001/XMLSchema-instance">http://www.w3.org/2001/XMLSchema-instance</a>" xsi:schemaLocation="<a href="http://java.sun.com/xml/ns/j2ee">http://java.sun.com/xml/ns/j2ee</a> <a href="http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd</a>"></div> <div><!-- --></div> <div><filter> <filter-name>NoteFilter</filter-name> <filter-class>com.NoteFilter</filter-class> <init-param> <param-name>blackList</param-name> <param-value>毛泽东</param-value> </init-param> </filter></div> <div><filter-mapping> <filter-name>NoteFilter</filter-name> <url-pattern>/servlet/NoteServlet</url-pattern> </filter-mapping></div> <div><filter> <filter-name>ReplaceTextFilter</filter-name> <filter-class>com.ReplaceTextFilter</filter-class> <init-param> <param-name>search</param-name> <param-value>***</param-value> </init-param> <init-param> <param-name>replace</param-name> <param-value>**</param-value> </init-param> </filter></div> <div><filter-mapping> <filter-name>ReplaceTextFilter</filter-name> <url-pattern>/servlet/NoteServlet</url-pattern> </filter-mapping></div> <div><servlet> <servlet-name>NoteServlet</servlet-name> <servlet-class>com.NoteServlet</servlet-class> </servlet></div> <div><servlet-mapping> <servlet-name>NoteServlet</servlet-name> <url-pattern>/servlet/NoteServlet</url-pattern> </servlet-mapping> </web-app></div> <div></div> <div>另外,要看到日志的输出信息,必须确保在server.xml文件中有如下代码: <div class="code"> <Logger className="org.apache.catalina.logger.FileLogger" prefix="catalina_log." suffix=".txt" timestamp="true"/> enjoy it!!! :) </div> </div>
试试其它关键字
同语言下
.
List 切割成几份 工具类
.
一行一行读取txt的内容
.
Java PDF转换成图片并输出给前台展示
.
java 多线程框架
.
double类型如果小数点后为零则显示整数否则保留两位小
.
将图片转换为Base64字符串公共类抽取
.
sqlParser 处理SQL(增删改查) 替换schema 用于多租户
.
JAVA 月份中的第几周处理 1-7属于第一周 依次类推 29-
.
java计算两个经纬度之间的距离
.
输入时间参数计算年龄
可能有用的
.
List 切割成几份 工具类
.
一行一行读取txt的内容
.
Java PDF转换成图片并输出给前台展示
.
java 多线程框架
.
double类型如果小数点后为零则显示整数否则保留两位小
.
将图片转换为Base64字符串公共类抽取
.
sqlParser 处理SQL(增删改查) 替换schema 用于多租户
.
JAVA 月份中的第几周处理 1-7属于第一周 依次类推 29-
.
java计算两个经纬度之间的距离
.
输入时间参数计算年龄
Dezai.CN
贡献的其它代码
(
4037
)
.
多线程Socket服务器模块
.
生成随机密码
.
清除浮动样式
.
弹出窗口居中
.
抓取url的函数
.
使用base HTTP验证
.
div模拟iframe嵌入效果
.
通过header转向的方法
.
Session操作类
.
执行sqlite输入插入操作后获得自动编号的ID
Copyright © 2004 - 2024 dezai.cn. All Rights Reserved
站长博客
粤ICP备13059550号-3