Open xiongxin opened 8 years ago
<?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"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="
http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="ServketWeb" version="3.0">
<welcome-file-list>
<welcome-file>
/index.html
</welcome-file>
</welcome-file-list>
<filter>
<filter-name>MyFiter</filter-name>
<filter-class>com.netease.MyFitler</filter-class>
</filter>
<filter-mapping>
<filter-name>MyFiter</filter-name>
<url-pattern>/hello/world</url-pattern>
</filter-mapping>
<servlet>
<servlet-name>HelloWrold</servlet-name>
<servlet-class>com.netease.HelloWorld</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>HelloWrold</servlet-name>
<url-pattern>/hello/world</url-pattern>
</servlet-mapping>
</web-app>
package com.netease;
import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
/**
* Created by xiongxin on 2/26/16.
*/
public class MyFitler implements Filter {
public void init(FilterConfig filterConfig) throws ServletException {
System.out.println("filter init");
}
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
System.out.println("filter do");
HttpServletRequest res =
(HttpServletRequest) servletRequest;
HttpSession session = res.getSession();
if (session.getAttribute("username") == null) {
HttpServletResponse resp =
(HttpServletResponse) servletResponse;
resp.sendRedirect("../index.html");
} else {
filterChain.doFilter(servletRequest, servletResponse);
}
//将请求对象和响应对象传递出去,否则会失效
filterChain.doFilter(servletRequest, servletResponse);
}
public void destroy() {
System.out.println("filter destroy");
}
}
<listener>
<listener-class>com.netease.MyListener</listener-class>
</listener>
package com.netease;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.ServletRequestEvent;
import javax.servlet.ServletRequestListener;
import javax.servlet.http.HttpSessionAttributeListener;
import javax.servlet.http.HttpSessionBindingEvent;
/**
* Created by xiongxin on 2/26/16.
*/
public class MyListener implements HttpSessionAttributeListener,
ServletContextListener, ServletRequestListener{
@Override
public void attributeAdded(HttpSessionBindingEvent httpSessionBindingEvent) {
}
@Override
public void attributeRemoved(HttpSessionBindingEvent httpSessionBindingEvent) {
}
@Override
public void attributeReplaced(HttpSessionBindingEvent httpSessionBindingEvent) {
}
@Override
public void contextInitialized(ServletContextEvent servletContextEvent) {
}
@Override
public void contextDestroyed(ServletContextEvent servletContextEvent) {
}
@Override
public void requestDestroyed(ServletRequestEvent servletRequestEvent) {
}
@Override
public void requestInitialized(ServletRequestEvent servletRequestEvent) {
}
}
过滤器
主要作用
工作原理
应用场景
过滤器的生命周期