赞
踩
Filter过滤器它是JavaWeb的三大组件之一,三大组件分别是:Servlet程序、Listener监听器、Filter过滤器
Filter过滤器它是JavaEE的规划,也是接口
Filter过滤器它的作用是:请求拦截
在WEB-INF的目录下,有很多资源,如html、jsp、txt、jpg等,都必须是用户登录之后才允许访问
简单的实现不用Filter过滤器就是可以看Session中是否包含用户登录信息即可
先从jsp文件里面进行Session的判断,是否有用户的Cookie信息,但这种只能在jsp文件里面而html是不能写Java代码的
这就需要到Filter过滤器
<%-- Created by IntelliJ IDEA. User: 程序员小徐同学 Date: 2022-01-12 Time: 16:41 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Cookie</title> <base href="http://localhost:8888/day2/"> <style type="text/css"> ul li { list-style: none; } </style> </head> <body> <h3>开始了新的测试</h3> <% Object user = session.getAttribute("user"); if (user == null) { request.getRequestDispatcher("/loGin.jsp").forward(request,response); return; } %> 这是index.jsp页面 </body> </html>
<%-- Created by IntelliJ IDEA. User: 86176 Date: 2022-01-12 Time: 16:50 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> </head> <body> <h4>这是登录页面</h4> <form> 用户名: <input type="text" name="username"> <br> 密 码: <input type="password" name="password" value=""><br> <input type="submit" value="登录"> </form> </body> </html>
Filter过滤器执行的方式
获取用户Cookie
<%-- Created by IntelliJ IDEA. User: 86176 Date: 2022-01-12 Time: 16:50 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> </head> <body> <h4>这是登录页面</h4> <form action="http://localhost:8888/day2/loginServlet" method="get"> 用户名: <input type="text" name="username"> <br> 密 码: <input type="password" name="password" value=""><br> <input type="submit" value="登录"> </form> </body> </html>
配置下的Servlet响应
public class LoginServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setCharacterEncoding("UTF-8");
resp.setContentType("text/html;character=UTF-8");
String username = req.getParameter("username");
String password = req.getParameter("password");
if ("root".equals(username) && "root".equals(password)){
req.getSession().setAttribute("user",username);
resp.getWriter().write("登录成功");
} else {
req.getRequestDispatcher("/loGin.jsp").forward(req,resp);
}
}
}
给Servlet程序配置和Filter过滤器也进行配置,配置Filter就是要拦截的目标路径
<!--Filter标签用于配置一个Filter过滤器--> <filter> <!--给Filter起一个别名--> <filter-name>imFilter</filter-name> <!--配置Filter的全类名--> <filter-class>com.Demo.Filter.imFilter</filter-class> </filter> <!--Filter-mapping 配置Filter过滤器的拦截路径--> <filter-mapping> <!--Filter-name表示当前拦截那个路径给Filter过滤使用--> <filter-name>imFilter</filter-name> <!--urlpattern配置拦截路径 斜杠就是表示http://ip:port/工程路径/ 映射到web目录下 --> <url-pattern>/admin/*</url-pattern> </filter-mapping> <servlet> <servlet-name>LoginServlet</servlet-name> <servlet-class>com.Demo.Filter.LoginServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>LoginServlet</servlet-name> <url-pattern>/loginServlet</url-pattern> </servlet-mapping>
Filter过滤器代码段
public class imFilter implements Filter { @Override public void init(FilterConfig filterConfig) throws ServletException { } @Override public void destroy() { } @Override public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { HttpServletRequest httpServletRequest = (HttpServletRequest) servletRequest; HttpSession session = httpServletRequest.getSession(); Object user = session.getAttribute("user"); //如果等于null ,说明还没有登录 if (user == null) { servletRequest.getRequestDispatcher("/loGin.jsp").forward(servletRequest,servletResponse); return; } else { //不等于空,让用户继续向下访问资源 filterChain.doFilter(servletRequest,servletResponse); } } }
Filter过滤器包含几个方法
public class imFilter implements Filter { public imFilter() { System.out.println("Filter构造器方法"); } @Override public void init(FilterConfig filterConfig) throws ServletException { System.out.println("Filter初始化方法"); } @Override public void destroy() { } @Override public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { System.out.println("doFilter拦截方法"); } @Override public void destory() { System.out.println("Filter的destory方法"); } }
FilterConfig类就是Filter过滤器的配置文件
Tomcat每次创建Filter的时候,也会同时创建一个FilterConfig类,这里包含了Filter配置文件的配置信息
FilterConfig类的作用是获取Filter过滤器的配置内容
public class imFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
System.out.println("Filter-name的值是" + filterConfig.getFilterName());
//获取在web.xml中的init-param初始化参数
System.out.println("初始化参数username" + filterConfig.getInitParameter("username"));
//获取ServletContext对象
System.out.println(filterConfig.getServletContext());
}
}
FilterChain过滤链就是多个过滤器进行
FilterChain.doFilter()方法作用
public class Filter1 implements Filter {
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
System.out.println("Filter2前置代码");
filterChain.doFilter(servletRequest,servletResponse);
System.out.println("Filter2后置代码");
}
}
public class imFilter implements Filter { @Override public void init(FilterConfig filterConfig) throws ServletException { } @Override public void destroy() { } @Override public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { System.out.println("Filter1前置代码"); filterChain.doFilter(servletRequest,servletResponse); System.out.println("Filter1后置代码"); } public void destory() { System.out.println("Filter的destory方法"); } }
两个Filter‘在web.xml的配置
<!--Filter标签用于配置一个Filter过滤器--> <filter> <!--给Filter起一个别名--> <filter-name>imFilter</filter-name> <!--配置Filter的全类名--> <filter-class>com.Demo.Filter.imFilter</filter-class> </filter> <!--Filter-mapping 配置Filter过滤器的拦截路径--> <filter-mapping> <!--Filter-name表示当前拦截那个路径给Filter过滤使用--> <filter-name>imFilter</filter-name> <!--urlpattern配置拦截路径 斜杠就是表示http://ip:port/工程路径/ 映射到web目录下 --> <url-pattern>/loGin.jsp</url-pattern> </filter-mapping> <filter> <filter-name>Filter1</filter-name> <filter-class>com.Demo.Filter.Filter1</filter-class> </filter> <filter-mapping> <filter-name>Filter1</filter-name> <url-pattern>/loGin.jsp</url-pattern> </filter-mapping>
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。