Java中Filter与Servlet的区别和联系

xiaoxiao2021-02-27  179

1.filter是一个可以复用的代码片段,可以用来转换HTTP请求、响应和头信息。Filter不像Servlet,它不能产生一个请求或者响应,它只是修改对某一资源的请求,或者修改从某一的响应。最近使用插装的时候,改用cookie对计算机进行识别,加入了过滤,仔细研究了一下servlet和filter,区别主要是:过滤器的生命周期一般都要经过下面三个阶段:servlet的特点是:初始化当容器第一次加载该过滤器时,init() 方法将被调用。该类在这个方法中包含了一个指向 Filter Config 对象的引用。我们的过滤器实际上并不需要这样做,因为其中没有使用初始化信息,这里只是出于演示的目的。过滤过滤器的大多数时间都消耗在这里。doFilter方法被容器调用, 同时传入分别指向这个请求/响应链中的 Servlet Request、Servlet Response 和 Filter Chain 对象的引用。然后过滤器就有机会处理请求,将处理任务传递给链中的下一个资源(通过调用 Filter Chain 对象引用上的 doFilter方法),之后在处理控制权返回该过滤器时处理响应。析构容器紧跟在垃圾收集之前调用 destroy()方法,以便能够执行任何必需的清理代码。关于chain.doFilter(request,response)他的作用是将请求转发给过滤器链上下一个对象。这里的下一个指的是下一个filter,如果没有filter那就是你请求的资源。 一般filter都是一个链,web.xml 里面配置了几个就有几个。一个一个的连在一起 request -> filter1 -> filter2 ->filter3 -> .... -> request resource.filter是链式操作,那么在处理单个filter时必须最后跳转到servlet对请求进行响应。如果走chain的话,通过chain.doFilter(request,response)这个方法会立即跳转到被拦截的servlet并且执行完还要再返回filter.chain相当于一扇门,从这扇门出去再从这扇门回来.调用filter的方法就是在web.xml中配置,需要配置一个与你需要拦截的servlet相同的url-pattern.

2.package com.qmsk.crm.sys.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.HttpSession; import com.qmsk.crm.util.Constant; import com.qmsk.crm.util.KouYangUtil; import com.qmsk.crm.xitongguanli.po.Path; import com.qmsk.crm.xitongguanli.po.RenYuan; import com.qmsk.crm.xitongguanli.po.RiZhi; import com.qmsk.crm.xitongguanli.service.IPathService; import com.qmsk.crm.xitongguanli.service.IRiZhiService; public class QXFilter implements Filter { @Override public void destroy() { } @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { HttpServletRequest request2 = (HttpServletRequest) request; String link = request2.getServletPath(); if(!link.endsWith(".jsp") && (!link.endsWith(".action"))){ chain.doFilter(request, response); } else if((link.endsWith(".jsp") && "/index.jsp".equals(link)) || (link.endsWith(".jsp") && "/yzm.jsp".equals(link)) || (link.endsWith(".jsp") && "/login.jsp".equals(link)) || (link.endsWith(".action") && ("/Login.action".equals(link) || "/TuiChu.action".equals(link)))){ chain.doFilter(request, response); } else { HttpSession session = request2.getSession(); RenYuan renYuan = (RenYuan) session.getAttribute(Constant.RENYUAN); if(renYuan != null){ //插入日志 IPathService pathService = (IPathService) KouYangUtil.getObjectService("PathService"); Path path = pathService.getPathByLink(link.substring(1)); if(path != null){ IRiZhiService riZhiService = (IRiZhiService) KouYangUtil.getObjectService("RiZhiService"); RiZhi riZhi = new RiZhi(); riZhi.setLink(path.getLink()); riZhi.setName(path.getName()); riZhi.setDescription(path.getDescription()); riZhi.setCaoZuoRYId(renYuan.getRenYuanId()); riZhi.setCaoZuoRYMC(renYuan.getName()); riZhiService.saveRiZhi(riZhi); } chain.doFilter(request, response); }else{ //request.getRequestDispatcher("/Forbidden.jsp").forward(request, response); request.getRequestDispatcher("/login.jsp").forward(request, response); } } @Override public void init(FilterConfig filterConfig) throws ServletException { } }
转载请注明原文地址: https://www.6miu.com/read-10417.html

最新回复(0)