首页 > 代码库 > filter入门

filter入门

TestFilter.java

package com.cdsxt.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;

/**
* @author Administrator
*测试 filter
*
* 实现特定的一个接口
*/
public class TestFilter implements Filter{


//filter默认就会加载  过滤器的init方法随着服务器启动就加载

@Override
public void init(FilterConfig filterConfig) throws ServletException {
// TODO Auto-generated method stub
System.out.println("TestFiler.init()");

}

@Override
public void doFilter(ServletRequest request, ServletResponse response,FilterChain chain) throws IOException, ServletException {
// TODO Auto-generated method stub
System.out.println("TestFiler.doFilter()");
chain.doFilter(request, response);
System.out.println("TestFiler1.doFilter()");
}

@Override
public void destroy() {
// TODO Auto-generated method stub
System.out.println("TestFiler.destroy()");
}

}

 

TestFilter1.java

package com.cdsxt.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.http.HttpServletRequest;

/**
* @author Administrator
*测试 filter
*
* 实现特定的一个接口
*/
public class TestFilter1 implements Filter{


//filter默认就会加载

@Override
public void init(FilterConfig filterConfig) throws ServletException {
// TODO Auto-generated method stub
System.out.println("TestFiler.init1()");

}

@Override
public void doFilter(ServletRequest request, ServletResponse response,FilterChain chain) throws IOException, ServletException {
// TODO Auto-generated method stub
System.out.println("TestFiler.doFilter1()");
System.out.println(request instanceof HttpServletRequest);

}

@Override
public void destroy() {
// TODO Auto-generated method stub
System.out.println("TestFiler.destroy1()");
}

}

 

 

LoginFilter.java

package com.cdsxt.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.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.cdsxt.po.Users;


public class LoginFilter implements Filter{

@Override
public void init(FilterConfig filterConfig) throws ServletException {
// TODO Auto-generated method stub

}

@Override
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
/**
* 如果请求是登录过的 那就 直接通过 不然就跳转到登录页面
*/
HttpServletRequest req = (HttpServletRequest) request;
HttpServletResponse resp = (HttpServletResponse) response;
//得到session 判断session中是否有用户
HttpSession session = req.getSession();

String uri = req.getRequestURI();

Object users = session.getAttribute("admin");
//登录过的
if(users!=null && users instanceof Users){
chain.doFilter(request, response);
}else{
if(uri.endsWith("login.jsp") || uri.endsWith("loginController") || uri.endsWith(".css") || uri.endsWith(".js") || uri.endsWith(".jpg")){
chain.doFilter(request, response);
}else{
resp.sendRedirect(req.getContextPath()+"/views/login.jsp");
}
}
}

@Override
public void destroy() {
// TODO Auto-generated method stub

}

}

 

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" 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_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>LoginSystem</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-mapping的顺序有关系 顺序由上往下
-->
<!-- <filter>
<filter-name>test-filter</filter-name>
<filter-class>com.cdsxt.filter.TestFilter</filter-class>
</filter>

<filter>
<filter-name>test-filter1</filter-name>
<filter-class>com.cdsxt.filter.TestFilter1</filter-class>
</filter>
<filter-mapping>
<filter-name>test-filter1</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

<filter-mapping>
<filter-name>test-filter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping> -->

<filter>
<filter-name>login-filter</filter-name>
<filter-class>com.cdsxt.filter.LoginFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>login-filter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter>
<filter-name>logout-filter</filter-name>
<filter-class>com.cdsxt.filter.LogoutFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>logout-filter</filter-name>
<url-pattern>/logout</url-pattern>
</filter-mapping>

<servlet>
<description></description>
<display-name>UsersController</display-name>
<servlet-name>UsersController</servlet-name>
<servlet-class>com.cdsxt.controller.UsersController</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>UsersController</servlet-name>
<url-pattern>/users/usersController</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>LoginController</servlet-name>
<servlet-class>com.cdsxt.controller.LoginController</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>LoginController</servlet-name>
<url-pattern>/login/loginController</url-pattern>
</servlet-mapping>
</web-app>

filter入门