测试web页面下的过滤器

来源:互联网 发布:cisco端口看环路 编辑:程序博客网 时间:2024/05/17 04:58

1.新建一个FirstFilter类,代码如下:

package com.ask.filter;

import java.io.IOException;

import javax.servlet.Filter;

import javax.servlet.FilterChain;

import javax.servlet.FilterConfig;

import javax.servlet.ServletException;

import javax.servlet.ServletRequest;

import javax.servlet.ServletResponse;

/*自定义一个java类实现filter接口*/

public class FirstFilter implements Filter {

public FirstFilter(){

System.out.println("FirstFilter: "+this.hashCode());

}

public void init(FilterConfig arg0) throws ServletException {

System.out.println("init: "+this.hashCode());

}

public void doFilter(ServletRequest req, ServletResponse res,

FilterChain chain) throws IOException, ServletException {

System.out.println("doFilter: "+this.hashCode());

//调用下一个filter或者是web资源

//chain.doFilter(req, res);

}

public void destroy() {

System.out.println("destroy: "+this.hashCode());

}

}

2.新建一个web端显示的页面showServlet类,代码如下:

package com.ask.filter;

import java.io.IOException;

import javax.servlet.Filter;

import javax.servlet.FilterChain;

import javax.servlet.FilterConfig;

import javax.servlet.ServletException;

import javax.servlet.ServletRequest;

import javax.servlet.ServletResponse;

/*自定义一个java类实现filter接口*/

public class FirstFilter implements Filter {

public FirstFilter(){

System.out.println("FirstFilter: "+this.hashCode());

}

public void init(FilterConfig arg0) throws ServletException {

System.out.println("init: "+this.hashCode());

}

public void doFilter(ServletRequest req, ServletResponse res,

FilterChain chain) throws IOException, ServletException {

System.out.println("doFilter: "+this.hashCode());

//调用下一个filter或者是web资源

//chain.doFilter(req, res);

}

public void destroy() {

System.out.println("destroy: "+this.hashCode());

}

}

3.web.xml配置文件如下:

<?xml version="1.0" encoding="UTF-8"?>

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">

<display-name>firstServlet</display-name>

<welcome-file-list>

<welcome-file>index.html</welcome-file>

<welcome-file>index.htm</welcome-file>

<welcome-file>index.jsp</welcome-file>

<welcome-file>default.html</welcome-file>

<welcome-file>default.htm</welcome-file>

<welcome-file>default.jsp</welcome-file>

</welcome-file-list>

<filter>

<filter-name>first</filter-name>

<filter-class>com.ask.filter.FirstFilter</filter-class>

</filter>

<filter-mapping>

<filter-name>first</filter-name>

<url-pattern>/show</url-pattern>

</filter-mapping>

<servlet>

<servlet-name>show</servlet-name>

<servlet-class>com.ask.servlet.showServlet</servlet-class>

</servlet>

<servlet-mapping>

<servlet-name>show</servlet-name>

<url-pattern>/show</url-pattern>

</servlet-mapping>

</web-app>

4.程序运行如下,没有显示show页面的内容,原因是过滤掉了

5.如果想要显示show页面,就的在FirstFilter类中doFilter方法添加

Chain.doFilter(req.res)方法;

0 0