Java Filter过滤器使用
本文来自CSDN博客,转载出处:http://blog.csdn.net/royaki/archive/2009/05/07/4159279.aspx
1、建立一个Servlet并且实现Filter接口
该类需要实现Filter接口中的init() doFilter() destory()方法
其中init()方法自动在项目启动的时候加载,doFilter()在调用xml配置的路径是加载,destory()方法在退出项目的时候进行。
public class TestFilter implements Filter{
public void init(FilterConfig filterConfig) throws ServletException{
System.out.println("初始化Filter");
}
public void doFilter(ServletRequest request,ServletResponse response,FilterChain chain) throws Exception{
System.out.println("进行doFilter");
chain(request,response); //使用这一句继续Servlet请求
}
public void destory(){
System.out.println("过滤器销毁");
}
}
2、在xml中配置Filter
<filter>
<filter-name></filter-name>
<filter-class></filter-class>
</filter>
<filter-mapping>
<filter-name></filter-name>
<url-pattern>/*</url-pattern> //这里想对哪个路径进行过滤就填写哪个
</filter-mapping>
应用:
编码过滤
public void doFilter(ServletRequest request,ServletResponse response,FilterChain chain) throws Exception{
System.out.println("进行doFilter"编码过滤);
try
{
request.setCharacterEncoding("GB2312") ;
}
catch (Exception e)
{
e.printStackTrace();
}
chain.doFilter(request,response) ;
}
xml中的配置:
<url-pattern>/*</url-pattern> 对所有请求都过滤
敏感词汇过滤
public void doFilter(ServletRequest request,
ServletResponse response,
FilterChain chain)
throws IOExce
相关文档:
Java学习从入门到精通
一、 JDK (Java Development Kit)
JDK是整个Java的核心,包括了Java运行环境(Java Runtime Envirnment),一堆Java工具和Java基础的类库(rt.jar)。不论什么Java应用服务器实质都是内置了某个版本的JDK。因此掌握JDK是学好Java的第一步。最主流的J ......
1. 阅读下列代码回答问题(第一个Java程序,理解PATH和CLASSPATH,学会使用javac和java命令)
view plaincopy to clipboardprint?
package cn.edu.uibe;
public class HelloWorld {
public static void main(String[] args) {
&nb ......
一直被同步搞得晕头转向,今天遇到了要写静态方法,总担心会不会有同步问题,结果看了以下一席话,豁然开朗~~
Every method of java will have a stack, and every invokation on that
method will have it's own 'stack frame'. So the locale data of one
method invokation will not affect others.
Please do not c ......
package com.test;
public class Jm1 {
private static int asnum;
private static char stchar;
public static void main(String[] args) {
System.out.println(getAsc("A"));
System.out.println(backchar(99));
}
/**
* 字符转ASC
*
* @param st
* @return
*/
public static int get ......
一.获得控制台用户输入的信息
Java代码
/** */
/**获得控制台用户输入的信息
* @return
* @throws IOException
*/
public
  ......