JavaWeb——Filter & Listener

过滤器

在Java中是一个Filter接口,重写实现过滤操作。

过滤某些内容中的非法字符、登陆权限检查。



浏览器
请求信息→



响应信息←


过滤器
请求信息→



响应信息←


服务器
package beer.onexone.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;
import javax.servlet.annotation.WebFilter;

//'/*'全路径拦截
@WebFilter("/*")
public class Filter1 implements Filter {

    public Filter1() {
    }
    
	public void destroy() {
	}
	
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
		System.out.println("过滤前");
		//放行语句
		chain.doFilter(request, response);
		System.out.println("过滤后");
	}

	public void init(FilterConfig fConfig) throws ServletException {
	}
}
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	<%System.out.println("访问成功"); %>
</body>
</html>

控制台输出:

过滤前
访问成功
过滤后

使用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_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>2019-8-1_One</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>Test1</filter-name>
  <filter-class>beer.onexone.filter.Filter1</filter-class>
  </filter>
  <filter-mapping>
  <filter-name>Test1</filter-name>
  <url-pattern>/*</url-pattern>
  </filter-mapping>
  
</web-app>

Filter的路径匹配方式与Servlet一致。

使用过滤器解决中文乱码

package beer.onexone.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;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet Filter implementation class charsetUTF8
 */
@WebFilter("/*")
public class charsetUTF8 implements Filter {

    /**
     * Default constructor. 
     */
    public charsetUTF8() {
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see Filter#destroy()
	 */
	public void destroy() {
		// TODO Auto-generated method stub
	}

	/**
	 * @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain)
	 */
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
		// TODO Auto-generated method stub
		// place your code here

		// pass the request along the filter chain
		HttpServletRequest httpServletRequest = (HttpServletRequest)request;
		HttpServletResponse httpServletResponse = (HttpServletResponse) response;
		httpServletRequest.setCharacterEncoding("UTF_8");
		httpServletResponse.setContentType("text/html;charset=UTF-8");
		chain.doFilter(request, response);
	}

	/**
	 * @see Filter#init(FilterConfig)
	 */
	public void init(FilterConfig fConfig) throws ServletException {
		// TODO Auto-generated method stub
	}

}

监听器

声明周期监听

package beer.onexone.listener;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.ServletRequestEvent;
import javax.servlet.ServletRequestListener;
import javax.servlet.annotation.WebListener;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;

@WebListener
public class Listen_lifecycle implements ServletContextListener, HttpSessionListener, ServletRequestListener {

    public Listen_lifecycle() {
    }

    public void sessionCreated(HttpSessionEvent se)  { 
    	System.out.println("HttpSession创建");
    }

    public void requestDestroyed(ServletRequestEvent sre)  { 
    	System.out.println("ServletRequest销毁");
    }
    
    public void requestInitialized(ServletRequestEvent sre)  { 
    	System.out.println("ServletRequest创建");
    }
    
    public void sessionDestroyed(HttpSessionEvent se)  { 
    	System.out.println("HttpSession销毁");
    }

    public void contextDestroyed(ServletContextEvent sce)  { 
    	System.out.println("ServletContext销毁");
    }
    
    public void contextInitialized(ServletContextEvent sce)  { 
    	System.out.println("ServletContext创建");
    }
	
}

属性监听器

package beer.onexone.listener;

import javax.servlet.ServletContextAttributeEvent;
import javax.servlet.ServletContextAttributeListener;
import javax.servlet.ServletRequestAttributeEvent;
import javax.servlet.ServletRequestAttributeListener;
import javax.servlet.annotation.WebListener;
import javax.servlet.http.HttpSessionAttributeListener;
import javax.servlet.http.HttpSessionBindingEvent;

@WebListener
public class Listen_attr implements ServletContextAttributeListener, HttpSessionAttributeListener, ServletRequestAttributeListener {

    public Listen_attr() {
    }

    public void attributeAdded(ServletContextAttributeEvent scae)  { 
    	System.out.println("Context属性添加");
    }
    
    public void attributeRemoved(ServletContextAttributeEvent scae)  { 
    	System.out.println("Context属性移除");
    }

    public void attributeRemoved(ServletRequestAttributeEvent srae)  { 
    	System.out.println("Request属性移除");
    }

    public void attributeAdded(ServletRequestAttributeEvent srae)  { 
    	System.out.println("Request属性添加");
    }

    public void attributeReplaced(ServletRequestAttributeEvent srae)  { 
    	System.out.println("Request属性替换");
    }

    public void attributeAdded(HttpSessionBindingEvent se)  { 
    	System.out.println("Session属性添加");
    }

    public void attributeRemoved(HttpSessionBindingEvent se)  { 
    	System.out.println("Session属性移除");
    }

    public void attributeReplaced(HttpSessionBindingEvent se)  { 
    	System.out.println("Session属性替换");
    }
    
    public void attributeReplaced(ServletContextAttributeEvent scae)  { 
    	System.out.println("Context属性替换");
    }	
}

第三类监听器

监听HttpSession中特殊JavaBean的状态改变。

事件源:特殊JavaBean
事件:	状态改变事件(绑定/解绑,钝化/活化)
监听器:实现HttpSessionBindingListener/ HttpSessionActivationListener接口即可
		特殊JavaBean
绑定监听器: 特殊JavaBean实现接口后,自己在监听自己

HttpSessionBindingListener		监听HttpSession中特殊JavaBean的绑定/解绑状态

jingsongchan

发表评论

电子邮件地址不会被公开。 必填项已用*标注