1、过滤器Filter
1.1、filter的简介
filter是对客户端访问资源的过滤,符合条件放行,不符合条件不放行,并且可以对目标资源访问前后进行逻辑处理
1.2、快速入门
步骤:
编写一个过滤器的类实现Filter接口;
实现接口中尚未实现的方法(着重实现doFilter方法);
在web.xml中进行配置(主要是配置要对哪些资源进行过滤)。
public class QuickFilter1 implements Filter{
@Override
//Filter创建的时候执行init方法
public void init(FilterConfig filterConfig) throws ServletException {
//1、获得web.xml中filter 的名称<filter-name>QuickFilter1</filter-name>
System.out.println(filterConfig.getFilterName());
//2、获得当前filter的初始化参数
System.out.println(filterConfig.getInitParameter("aaa"));
//3、获得servletContext
filterConfig.getServletContext();
System.out.println("init ....");
}
@Override
//doFilter是Filter的核心过滤的方法
/*
* request: 内部封装是客户端http请求的内容
* response: 代表是响应
* FilterChain: 过滤器链对象
*/
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException {
System.out.println("quick1 running....");
//放行请求
chain.doFilter(request, response);
}
@Override
//Filter对象销毁的时候执行destory方法
public void destroy() {
System.out.println("destroy...");
}
}
web.xml
<filter>
<filter-name>QuickFilter1</filter-name>
<filter-class>com.web.filter.QuickFilter1</filter-class>
</filter>
<filter-mapping>
<filter-name>QuickFilter1</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
1.3、Filter的API详解
filter生命周期及其与生命周期相关的方法
Filter接口有三个方法,并且这个三个都是与Filter的生命相关的方法
init(Filterconfig):代表filter对象初始化方法,filter对象创建时执行;
doFilter(ServletRequest,ServletResponse,FilterCha):代表filter执行过滤的核心方法,如果某资源在已经被配置到这个filter进行过滤的话,那么每次访问这个资源都会执行doFilter方法;
destory():代表是filter销毁方法,当filter对象销毁时执行该方法。
Filter对象的生命周期:
Filter何时创建:服务器启动时就创建该filter对象
Filter何时销毁:服务器关闭时filter销毁
init(FilterConfig)
其中参数config代表该Filter对象的配置信息的对象,内部封装是该filter的配置信息。
destory()方法
filter对象销毁时执行
doFilter方法
doFilter(ServletRequest,ServletResponse,FilterChain)
其中的参数:
ServletRequest/ServletResponse:每次在执行doFilter方法时,web容器负责创建一个request和一个response对象作为doFilter的参数传递进来。该request个该response就是在访问目标资源的service方法时的request和response。
FilterChain:过滤器链对象,通过该对象的doFilter方法可以放行该请求。
1.4、Filter的配置
url-pattern配置时
1)完全匹配 /sertvle1
2)目录匹配 /aaa/bbb/* ----最多的
/user/*:访问前台的资源进入此过滤器
/admin/*:访问后台的资源时执行此过滤器
3)扩展名匹配 *.abc *.jsp
注意:url-pattern可以使用servlet-name替代,也可以混用
dispatcher:访问的方式
REQUEST:默认值,代表直接访问某个资源时执行filter
FORWARD:转发时才执行filter
INCLUDE: 包含资源时执行filter
ERROR:发生错误时进行跳转是执行filter
Filter的作用?
1)公共代码的提取
2)可以对request和response中的方法进行增强(装饰者模式/动态代理)
3)进行权限控制
2、自动登录
public class LoginServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
HttpSession session = request.getSession();
//获取数据
String username = request.getParameter("username");//中文 张三
String password = request.getParameter("password");
UserService service = new UserService();
User user = null;
try {
user = service.login(username,password);
} catch (SQLException e) {
e.printStackTrace();
}
if(user!=null){
//登录成功
//判断用户是否勾选自动登录
String autoLogin = request.getParameter("autoLogin");
if(autoLogin!=null){
//对中文张三进行编码
String username_code = URLEncoder.encode(username, "UTF-8");// %AE4%kfj
Cookie cookie_username = new Cookie("cookie_username",username_code);
Cookie cookie_password = new Cookie("cookie_password",password);
//设置cookie的持久化时间
cookie_username.setMaxAge(60*60);
cookie_password.setMaxAge(60*60);
//设置cookie的携带路径
cookie_username.setPath(request.getContextPath());
cookie_password.setPath(request.getContextPath());
//发送cookie
response.addCookie(cookie_username);
response.addCookie(cookie_password);
}
//将登录的用户的user对象存到session中
session.setAttribute("user", user);
//重定向到首页
response.sendRedirect(request.getContextPath());
}else{
//失败 转发到登录页面 提出提示信息
request.setAttribute("loginInfo", "用户名或密码错误");
request.getRequestDispatcher("/login.jsp").forward(request, response);
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
public class UserService {
public User login(String username, String password) throws SQLException {
UserDao dao = new UserDao();
return dao.login(username,password);
}
}
public class User {
private int id;
private String username;
private String password;
private String email;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
public class UserDao {
public User login(String username, String password) throws SQLException {
QueryRunner runner = new QueryRunner(DataSourceUtils.getDataSource());
String sql = "select * from user where username=? and password=?";
return runner.query(sql, new BeanHandler<User>(User.class), username,password);
}
}
public class AutoLoginFilter implements Filter{
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException {
HttpServletRequest req = (HttpServletRequest) request;
HttpServletResponse resp = (HttpServletResponse) response;
HttpSession session = req.getSession();
//获得cookie中用户名和密码 进行登录的操作
//定义cookie_username
String cookie_username = null;
//定义cookie_password
String cookie_password = null;
//获得cookie
Cookie[] cookies = req.getCookies();
if(cookies!=null){
for(Cookie cookie : cookies){
//获得名字是cookie_username和cookie_password
if("cookie_username".equals(cookie.getName())){
cookie_username = cookie.getValue();
//恢复中文用户名
cookie_username = URLDecoder.decode(cookie_username, "UTF-8");
}
if("cookie_password".equals(cookie.getName())){
cookie_password = cookie.getValue();
}
}
}
//判断username和password是否是null
if(cookie_username!=null&&cookie_password!=null){
//登录的代码
UserService service = new UserService();
User user = null;
try {
user = service.login(cookie_username,cookie_password);
} catch (SQLException e) {
e.printStackTrace();
}
//将登录的用户的user对象存到session中
session.setAttribute("user", user);
}
//放行
chain.doFilter(req, resp);
}
@Override
public void init(FilterConfig filterConfig) throws ServletException {
}
@Override
public void destroy() {
}
}