过滤器
当需要限制用户访问某些资源或者处理请求是提前处理某些资源时
Filter接口定义了3个接口方法
init():过滤器初始化,可以通过Filterconfig配置启动参数
dofilter():完成具体的操作,然后通过Filter让请求继续向下传递
destroy():过滤器销毁时调用
给出例子如下
创建动态工程
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_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>testfilter</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>demo</filter-name>
<filter-class>testfilter.test.TestFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>demo</filter-name>
<!-- 过滤器的路径 -->
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
类class
public class TestFilter implements Filter {
@Override
public void destroy() {
//销毁时调用
}
@Override
public void doFilter(ServletRequest arg0, ServletResponse arg1, FilterChain arg2)
throws IOException, ServletException {
System.out.println("传递request请求");
}
@Override
public void init(FilterConfig arg0) throws ServletException {
System.out.println();
}
}
过滤器的作用
更改字符集
简单的登录
案例
class类
public class TestFilter implements Filter {
@Override
public void destroy() {
// 销毁时调用
}
@Override
public void doFilter(ServletRequest arg0, ServletResponse arg1, FilterChain chain)
throws IOException, ServletException {
System.out.println("传递request请求");
HttpServletRequest request = (HttpServletRequest) arg0;
HttpSession session = request.getSession();
if (session.getAttribute("uid") != null) {
chain.doFilter(request, arg1);
System.out.println("login");
} else {
request.getRequestDispatcher("login.jsp").forward(request, arg1);
System.out.println("请登录...");
}
}
@Override
public void init(FilterConfig arg0) throws ServletException {
System.out.println();
}
}
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_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>testfilter</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>demo</filter-name>
<filter-class>testfilter.test.TestFilter</filter-class>
<init-param>
<param-name>login</param-name>
<param-value>/login.jsp</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>demo</filter-name>
<!-- 过滤器的路径 -->
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
监听器
监听servlet,主要功能是负责监听web的各种操作,当相关事件发生后,将产生事件,
并对此事件进行处理(springmvc中的servlet监听器,启动webapplication)
对application监听
上下文监听器 servletContextListener
接口定义俩个方法 contextInitialized()和contextDestroyed()
web.xml配置监听器
<listener>
<listener-class>... </listener-class>
</listener>
servlet配置监听器与过滤器
建议顺序如下 先配置过滤器,然后配置监听器
session监听器
httpSessionlistener 接口
定义俩个方法 sessionCreated () 与sessionDestroyed()方法
对request监听
定义俩个方法
requestInitialized()方法
requestDestroyed()方法