码迷,mamicode.com
首页 > 编程语言 > 详细

javaweb----Filter

时间:2018-08-08 19:55:28      阅读:177      评论:0      收藏:0      [点我收藏+]

标签:odi   配置   etc   初始化   username   check   重点   技术分享   webapp   

定义

过滤器:基本功能是对servlet容器调用Servlet的过程进行拦截,从而在Servlet响应前后实现一些特殊功能
javaAPI提供三个接口,Filter,FilterChain,FilterConfig

实现接口Filter

public class LoginFilter2 implements Filter {

    public LoginFilter2() {
        // TODO Auto-generated constructor stub
    }
    public void destroy() {
        // TODO Auto-generated method stub
    }

    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
    
         chain.doFilter(request,response);
        
    }

    public void init(FilterConfig fConfig) throws ServletException {
        // TODO Auto-generated method stub
    }

}

生命周期:加载----->调用构造器(调用一次)-------->初始化init()(调用一次)------->doFilter()(可以调用多次和servlet中的service一样)

实例

1.login.jsp代码 ,hello.jsp就不给出了,随便写点

技术分享图片

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
  <form action="${pageContext.request.contextPath }/hello.jsp"  method="post">
  用户:<input type="text" name="username">${user_msg }<br>
  密码:<input type="password" name="password">${pass_msg}<br>
  <button>提交</button>
  </form>
</body>
</html>
  1. LoginFilter拦截器用于检查用户名 ,获取初始化参数“Tom”来比较
package com.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;

public class LoginFilter implements Filter{

    @Override
    public void destroy() {
        // TODO Auto-generated method stub
        
    }
    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain arg2)
            throws IOException, ServletException {
        String username=request.getParameter("username");
        String password=request.getParameter("password");
        String name=config.getInitParameter("Username");
        if(username.equals(name)) {
            arg2.doFilter(request, response);
        }else {
            request.setAttribute("user_msg", "用户名错误");
            request.getRequestDispatcher("login.jsp").forward(request, response);
        }
        
    }

    FilterConfig config;
    @Override
    public void init(FilterConfig config) throws ServletException {
        // TODO Auto-generated method stub
        this.config=config;
    }

}

3.web.XML配置代码,和Servlet配置一样,只是名不一样
注意:自己配置的Filter拦截顺序就是代码书写顺序,如果使用注解自动配置的话拦截顺序是按Filter类的名来排序的,

<?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">
  <context-param>
    <param-name>Password</param-name>
    <param-value>1234</param-value>
  </context-param>
  
  <filter>
    <filter-name>loginfilter</filter-name>
    <filter-class>com.filter.LoginFilter</filter-class>
    <init-param>
      <param-name>Username</param-name>
      <param-value>Tom</param-value>
    </init-param>
  </filter>
  <filter-mapping>
    <filter-name>loginfilter</filter-name>
    <url-pattern>/hello.jsp</url-pattern>
  </filter-mapping>
  <filter>
    <filter-name>checkfilter</filter-name>
    <filter-class>com.filter.CheckFilter</filter-class>
  </filter>
  <filter-mapping>
    <filter-name>checkfilter</filter-name>
    <url-pattern>/hello.jsp</url-pattern>
  </filter-mapping>
</web-app>

4.检查密码的代码,重点练习获取全局变量

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

public class CheckFilter implements Filter{

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

    @Override
    public void doFilter(ServletRequest arg0, ServletResponse arg1, FilterChain arg2)
            throws IOException, ServletException {
        String password=arg0.getParameter("password");
        String pass=config.getServletContext().getInitParameter("Password");
        if(password.equals(pass)) {
            arg2.doFilter(arg0, arg1);
        }else {
            arg0.setAttribute("pass_msg", "密码错误");
            arg0.getRequestDispatcher("login.jsp").forward(arg0, arg1);
        }
        
    }

    FilterConfig config;
    @Override
    public void init(FilterConfig config) throws ServletException {
        // TODO Auto-generated method stub
        this.config=config;
    }

}

javaweb----Filter

标签:odi   配置   etc   初始化   username   check   重点   技术分享   webapp   

原文地址:https://www.cnblogs.com/liu-ya/p/9445203.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!