1、filter 过滤器的概述
filter过滤器:是面向切面编程的一种实现策略,在不影响原来的程序流程的前提下,将一些业务逻辑切入流程中,在请求达到目标之前进行处理,一般用于编码过滤、权限过滤、....
2、filter 过滤器的创建步骤
1、创建一个普通的java类实现filter接口
2、实现接口中的三个方法(init,doFilter,destory)
3、配置过滤器(web.xml)
3、实例_编码过滤器
package com.shore.a_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 CharsetEncoding implements Filter{
private String encoding;
public void init(FilterConfig config) throws ServletException {//过滤器初始化
encoding = config.getInitParameter("encoding");//获取web.xml配置文件中的参数
} public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
request.setCharacterEncoding(encoding);
response.setCharacterEncoding(encoding);
chain.doFilter(request, response);//放行
} public void destroy() {//过滤器被销毁(服务器停止,才会被销毁) }
}
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://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <!-- 编码过滤 -->
<filter>
<!-- 类名 -->
<filter-name>CharsetEncoding</filter-name>
<!-- 类路径 -->
<filter-class>com.shore.a_filter.CharsetEncoding</filter-class>
<init-param>
<!-- 参数名 -->
<param-name>encoding</param-name>
<!-- 编码 -->
<param-value>UTF-8</param-value>
</init-param>
</filter> <filter-mapping>
<!-- 类名 -->
<filter-name>CharsetEncoding</filter-name>
<!-- /* 表示过滤(拦截)所有页面(请求) *.xxx 表示拦截该指定后缀结尾的页面(请求) -->
<url-pattern>/*</url-pattern>
</filter-mapping> <welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
附录
过滤器
package com.shore.b_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; public class LoginFilter implements Filter{
private String[] passlist;
public void init(FilterConfig Config) throws ServletException {
passlist=Config.getInitParameter("passList").split(",");
System.out.println("第一步");
} public void doFilter(ServletRequest req, ServletResponse resp,
FilterChain chain) throws IOException, ServletException {
//会重复执行一次
System.out.println("第二步");
//获取request对象
HttpServletRequest request=(HttpServletRequest)req;
HttpServletResponse response=(HttpServletResponse)resp;
//获取客户端的资源路径
String uri = request.getRequestURI();
//获取请求资源路径
int index = uri.lastIndexOf("/");//获取路径中的最后一个斜杠的下标
String source = uri.substring(index+1);//截取斜杠后面的所有字符串
System.out.println(source);
boolean flag = false;//定义标记
for(String s:passlist){//遍历
if(s.trim().equals(source)){
flag = true;
}
}
if(flag){//如果flag=true则放行
chain.doFilter(request, response);
}else{
//获取session的内容
Object obj = request.getSession().getAttribute("user");
if(obj == null){
//未登录
request.setAttribute("msg","请先登录");
request.getRequestDispatcher("index.jsp").forward(request, response);
}else{
System.out.println("第五步");
chain.doFilter(request, response);//放行
}
}
} public void destroy() { }
}
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://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <!-- 配置过滤器-->
<filter>
<!-- 类名 -->
<filter-name>LoginFilter</filter-name>
<!-- 类路径 -->
<filter-class>com.shore.b_filter.LoginFilter</filter-class>
<init-param>
<!-- 参数名 -->
<param-name>passList</param-name>
<!-- passList参数的值(放行index和reg页面,即:不用登陆也可直接访问这两个页面) -->
<param-value>index.jsp,reg.jsp</param-value>
</init-param>
</filter>
<filter-mapping>
<!-- 类名 -->
<filter-name>LoginFilter</filter-name>
<!-- 拦截所有jsp页面 -->
<url-pattern>*.jsp</url-pattern>
</filter-mapping> <!-- 编码过滤 -->
<filter>
<filter-name>CharsetEncoding</filter-name>
<filter-class>com.shore.a_filter.CharsetEncoding</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CharsetEncoding</filter-name>
<!-- /* 表示过滤(拦截)所有页面(请求) *.xxx 表示拦截该指定后缀结尾的页面(请求) -->
<url-pattern>/*</url-pattern>
</filter-mapping> <servlet>
<description>This is the description of my J2EE component</description>
<display-name>This is the display name of my J2EE component</display-name>
<servlet-name>LoginServlet</servlet-name>
<servlet-class>com.shore.b_servlets.LoginServlet</servlet-class>
</servlet>
<servlet>
<description>This is the description of my J2EE component</description>
<display-name>This is the display name of my J2EE component</display-name>
<servlet-name>RegisterServlet</servlet-name>
<servlet-class>com.shore.b_servlets.RegisterServlet</servlet-class>
</servlet> <servlet-mapping>
<servlet-name>LoginServlet</servlet-name>
<url-pattern>/LoginServlet</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>RegisterServlet</servlet-name>
<url-pattern>/RegisterServlet</url-pattern>
</servlet-mapping> <welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
servlet
package com.shore.b_servlets; import java.io.IOException;
import java.io.PrintWriter; import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; public class LoginServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
} public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String name=request.getParameter("uname");
String pass=request.getParameter("upass");
System.out.println("第四步");
if(!"".equals(name.trim())){
//将当前登录的用户存储到session中
request.getSession().setAttribute("user",name);
//跳转到主页
response.sendRedirect("main.jsp"); }else{
request.setAttribute("msg","请输入账号和密码");
request.getRequestDispatcher("index.jsp").forward(request, response);
}
}
}
登录页面
<%@ page language="java" import="java.util.*" pageEncoding="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 'index.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>
<%
System.out.println("第三步");
%>
<form action="LoginServlet1" method="post">
<input type="text" name="uname"/>
<input type="password" name="upass"/>
<input type="submit" value="登录"/>
<span>没有账号?<a href="reg.jsp">立即注册</a></span>
</form>
<hr>
<span style="color: red;">${requestScope.msg}</span>
</body>
</html>
注册页面
<%@ page language="java" import="java.util.*" pageEncoding="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 'reg.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="RegisterServlet">
<input type="text" name="uname" placeholder="请输入账号"/>
<input type="password" name="upass" placeholder="请输入密码"/>
<input type="password" name="upass" placeholder="请输入重复密码"/>
<input type="submit" value="注册"/>
</form>
</body>
</html>
主页面
<%@ page language="java" import="java.util.*" pageEncoding="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 'main.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>
Struts2下的拦截器(interceptor)和 过滤器(Filter)
原创作者:DSHORE 作者主页:http://www.cnblogs.com/dshore123/ 原文出自:https://www.cnblogs.com/dshore123/p/10703859.html 欢迎转载,转载务必说明出处。(如果本文对您有帮助,可以点击一下右下角的 推荐,或评论,谢谢!) |