javaweb----Filter

Posted 菜鸡变小鸟

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了javaweb----Filter相关的知识,希望对你有一定的参考价值。

定义

过滤器:基本功能是对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的主要内容,如果未能解决你的问题,请参考以下文章

JavaWeb--Filter(过滤器)学习

javaweb----Filter

JavaWeb Filter

JavaWeb--Filter(过滤器)学习(附加)

微信小程序代码片段

VSCode自定义代码片段——CSS选择器