时间:2021-05-20
过滤器是什么玩意?
所谓过滤器,其实就是一个服务端组件,用来截取用户端的请求与响应信息。
过滤器的应用场景:
1.对用户请求进行统一认证,保证不会出现用户账户安全性问题
2.编码转换,可在服务端的过滤器中设置统一的编码格式,避免出现乱码
3.对用户发送的数据进行过滤替换
4.转换图像格式
5.对响应的内容进行压缩
其中,第1,2场景经常涉及。
login.jsp
<%@ page language="java" import="java.util.*" contentType="text/html; charset=utf-8"%><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html> <head> <base href="<%=basePath%>"> <title>My JSP 'login.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> <form action="<%=path %>/servlet/LoginServlet" method="post" > 用户名:<input type="text" name="username" /> 密码:<input type="password" name="password" /> <input type="submit" value="登录" /> </form> </body></html>success.jsp
failure.jsp
<%@ page language="java" import="java.util.*" contentType="text/html; charset=utf-8"%><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html> <head> <base href="<%=basePath%>"> <title>My JSP 'login.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> 登录失败,请检查用户名或密码! </body></html>LoginFilter.java
package com.cityhuntshou.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;public class LoginFilter implements Filter { private FilterConfig config; public void destroy() { } public void doFilter(ServletRequest arg0, ServletResponse arg1, FilterChain arg2) throws IOException, ServletException { HttpServletRequest request = (HttpServletRequest) arg0; HttpServletResponse response = (HttpServletResponse) arg1; HttpSession session = request.getSession(); //过滤器实际应用场景之二-----编码转换 String charset = config.getInitParameter("charset"); if(charset == null) { charset = "UTF-8"; } request.setCharacterEncoding(charset); String noLoginPaths = config.getInitParameter("noLoginPaths"); if(noLoginPaths != null) { String[] strArray = noLoginPaths.split(";"); for(int i = 0; i < strArray.length; i++) { //空元素,放行 if(strArray[i] == null || "".equals(strArray[i])) continue; if(request.getRequestURI().indexOf(strArray[i]) != -1) { arg2.doFilter(arg0, arg1); return; } } } if(request.getRequestURI().indexOf("login.jsp") != -1 || request.getRequestURI().indexOf("LoginServlet") != -1) { arg2.doFilter(arg0, arg1); return; } if(session.getAttribute("username") != null) { arg2.doFilter(arg0, arg1); } else { response.sendRedirect("login.jsp"); } } public void init(FilterConfig arg0) throws ServletException { config = arg0; }}LoginServlet.java
web.xml
<?xml version="1.0" encoding="UTF-8"?><web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://.cityhuntshou.filter.LoginFilter</filter-class> <init-param> <param-name>noLoginPaths</param-name> <param-value>login.jsp;failure.jsp;loginServlet</param-value> </init-param> <init-param> <param-name>charset</param-name> <param-value>UTF-8</param-value> </init-param> </filter> <filter-mapping> <filter-name>LoginFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping></web-app>运行效果:
访问结果:
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
JavaWebServlet中Filter过滤器的详解1.简述Filter过滤器,对web服务器所有web资源进行过滤,从而实现一些特殊的功能(权限访问控制、过
java过滤器模式(Filter/CriteriaPattern)过滤器模式(FilterPattern)或标准模式(CriteriaPattern)是一种设计
AngularJS过滤器过滤器可以使用一个管道字符(|)添加到表达式和指令中。AngularJS过滤器AngularJS过滤器可用于转换数据:过滤器描述curr
vue2.0里,不再有自带的过滤器,需要自己定义过滤器。定义的方法如下:注册一个自定义过滤器,它接收两个参数:过滤器ID和过滤器函数。Vue.filter('f
vue2.0里,不再有自带的过滤器,需要自己定义过滤器。定义的方法如下:注册一个自定义过滤器,它接收两个参数:过滤器ID和过滤器函数。Vue.filter('f