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
相关文档:
1. 阅读下列代码回答问题(第一个Java程序,理解PATH和CLASSPATH,学会使用javac和java命令)
view plaincopy to clipboardprint?
package cn.edu.uibe;
public class HelloWorld {
public static void main(String[] args) {
&nb ......
使用poi来生成xls文件:引用的包poi-3.2.final.jar;
要了解的类HSSFWorkbook,HSSFSheet,HSSFRow,HSSFCell
HSSFWorkbook workbook=new HSSFWorkbook();得到一个Excel文件的引用
HSSFSheet sheet=workbook.createSheet();得到一个sheet,还有HSSFSheet sheet=workbook.createSheet("sheet1");方法可以设置Sheet的名称。
然 ......
一直被同步搞得晕头转向,今天遇到了要写静态方法,总担心会不会有同步问题,结果看了以下一席话,豁然开朗~~
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 ......
Mysql
sudo netstat -tap | grep mysql
当您运行该命令时,您可以看到类似下面的行:
linuxidc@linuxidc-laptop:~$">linuxidc@linuxidc-laptop:~$ sudo netstat -tap | grep mysql
[sudo] password for linuxidc:
tcp &n ......
一.获得控制台用户输入的信息
Java代码
/** */
/**获得控制台用户输入的信息
* @return
* @throws IOException
*/
public
  ......