fitter 如何只过滤jsp
发布网友
发布时间:2023-07-26 05:37
我来回答
共1个回答
热心网友
时间:2023-09-17 17:43
Servlet过滤器开发步骤开发Servlet过滤器的步骤如下:①编写实现Filter接口的Servlet类。②在web.xml中配置Filter。开发一个过滤器需要实现Filter接口,Filter接口定义了以下方法:①destory()由Web容器调用,初始化此Filter。②init(FilterConfig filterConfig)由Web容器调用,初始化此Filter。③doFilter(ServletRequest request,ServletResponse response,FilterChain chain)具体过滤处理代码。3.一个过滤器框架实例SimpleFilter1.java
package com.zj.sample;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 SimpleFilter1 implements Filter { @SuppressWarnings("unused") private FilterConfig filterConfig; public void init(FilterConfig config) throws ServletException { this.filterConfig = config; } public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) { try { System.out.println("Within SimpleFilter1:Filtering the Request..."); chain.doFilter(request, response);// 把处理发送到下一个过滤器 System.out .println("Within SimpleFilter1:Filtering the Response..."); } catch (IOException ioe) { ioe.printStackTrace(); } catch (ServletException se) { se.printStackTrace(); } } public void destroy() { this.filterConfig = null; }}
SimpleFilter2.java
package com.zj.sample;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 SimpleFilter2 implements Filter { @SuppressWarnings("unused") private FilterConfig filterConfig; public void init(FilterConfig config) throws ServletException { this.filterConfig = config; } public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) { try { System.out.println("Within SimpleFilter2:Filtering the Request..."); chain.doFilter(request, response); // 把处理发送到下一个过滤器 System.out.println("Within SimpleFilter2:Filtering the Response..."); } catch (IOException ioe) { ioe.printStackTrace(); } catch (ServletException se) { se.printStackTrace(); } } public void destroy() { this.filterConfig = null; }}
web.xml
<filter> <filter-name>filter1</filter-name> <filter-class>com.zj.sample.SimpleFilter1</filter-class></filter><filter-mapping> <filter-name>filter1</filter-name> <url-pattern>/*</url-pattern>//为所有的访问做过滤</filter-mapping> <filter> <filter-name>filter2</filter-name> <filter-class>com.zj.sample.SimpleFilter2</filter-class></filter><filter-mapping> <filter-name>filter2</filter-name> <url-pattern>/*</url-pattern>//为所有的访问做过滤</filter-mapping>
打开web容器中任意页面输出结果:(注意过滤器执行的请求/响应顺序)
Within SimpleFilter1:Filtering the Request...
Within SimpleFilter2:Filtering the Request...
Within SimpleFilter2:Filtering the Response...
Within SimpleFilter1:Filtering the Response...
4.报告过滤器我们来试验一个简单的过滤器,只要调用相关的servlet或JSP页面,它就打印一条消息到标准输出。为实现此功能,在doFilter方法中执行过滤行为。每当调用与这个过滤器相关的servlet或JSP页面时,doFilter方法就生成一个打印输出,此输出列出请求主机和调用的URL。因为getRequestURL方法位于HttpServletRequest而不是ServletRequest中,所以把ServletRequest对象构造为HttpServletRequest类型。我们改动一下章节3的SimpleFilter1.java。SimpleFilter1.java
package com.zj.sample;import java.io.IOException;import java.util.Date;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; public class SimpleFilter1 implements Filter { @SuppressWarnings("unused") private FilterConfig filterConfig; public void init(FilterConfig config) throws ServletException { this.filterConfig = config; } public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) { try { System.out.println("Within SimpleFilter1:Filtering the Request..."); HttpServletRequest req = (HttpServletRequest) request; System.out.println(req.getRemoteHost() + " tried to access " + req.getRequestURL() + " on " + new Date() + "."); chain.doFilter(request, response); System.out.println("Within SimpleFilter1:Filtering the Response..."); } catch (IOException ioe) { ioe.printStackTrace(); } catch (ServletException se) { se.printStackTrace(); } } public void destroy() { this.filterConfig = null; }}
web.xml设置不变,同章节3。
热心网友
时间:2023-09-17 17:43
Servlet过滤器开发步骤开发Servlet过滤器的步骤如下:①编写实现Filter接口的Servlet类。②在web.xml中配置Filter。开发一个过滤器需要实现Filter接口,Filter接口定义了以下方法:①destory()由Web容器调用,初始化此Filter。②init(FilterConfig filterConfig)由Web容器调用,初始化此Filter。③doFilter(ServletRequest request,ServletResponse response,FilterChain chain)具体过滤处理代码。3.一个过滤器框架实例SimpleFilter1.java
package com.zj.sample;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 SimpleFilter1 implements Filter { @SuppressWarnings("unused") private FilterConfig filterConfig; public void init(FilterConfig config) throws ServletException { this.filterConfig = config; } public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) { try { System.out.println("Within SimpleFilter1:Filtering the Request..."); chain.doFilter(request, response);// 把处理发送到下一个过滤器 System.out .println("Within SimpleFilter1:Filtering the Response..."); } catch (IOException ioe) { ioe.printStackTrace(); } catch (ServletException se) { se.printStackTrace(); } } public void destroy() { this.filterConfig = null; }}
SimpleFilter2.java
package com.zj.sample;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 SimpleFilter2 implements Filter { @SuppressWarnings("unused") private FilterConfig filterConfig; public void init(FilterConfig config) throws ServletException { this.filterConfig = config; } public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) { try { System.out.println("Within SimpleFilter2:Filtering the Request..."); chain.doFilter(request, response); // 把处理发送到下一个过滤器 System.out.println("Within SimpleFilter2:Filtering the Response..."); } catch (IOException ioe) { ioe.printStackTrace(); } catch (ServletException se) { se.printStackTrace(); } } public void destroy() { this.filterConfig = null; }}
web.xml
<filter> <filter-name>filter1</filter-name> <filter-class>com.zj.sample.SimpleFilter1</filter-class></filter><filter-mapping> <filter-name>filter1</filter-name> <url-pattern>/*</url-pattern>//为所有的访问做过滤</filter-mapping> <filter> <filter-name>filter2</filter-name> <filter-class>com.zj.sample.SimpleFilter2</filter-class></filter><filter-mapping> <filter-name>filter2</filter-name> <url-pattern>/*</url-pattern>//为所有的访问做过滤</filter-mapping>
打开web容器中任意页面输出结果:(注意过滤器执行的请求/响应顺序)
Within SimpleFilter1:Filtering the Request...
Within SimpleFilter2:Filtering the Request...
Within SimpleFilter2:Filtering the Response...
Within SimpleFilter1:Filtering the Response...
4.报告过滤器我们来试验一个简单的过滤器,只要调用相关的servlet或JSP页面,它就打印一条消息到标准输出。为实现此功能,在doFilter方法中执行过滤行为。每当调用与这个过滤器相关的servlet或JSP页面时,doFilter方法就生成一个打印输出,此输出列出请求主机和调用的URL。因为getRequestURL方法位于HttpServletRequest而不是ServletRequest中,所以把ServletRequest对象构造为HttpServletRequest类型。我们改动一下章节3的SimpleFilter1.java。SimpleFilter1.java
package com.zj.sample;import java.io.IOException;import java.util.Date;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; public class SimpleFilter1 implements Filter { @SuppressWarnings("unused") private FilterConfig filterConfig; public void init(FilterConfig config) throws ServletException { this.filterConfig = config; } public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) { try { System.out.println("Within SimpleFilter1:Filtering the Request..."); HttpServletRequest req = (HttpServletRequest) request; System.out.println(req.getRemoteHost() + " tried to access " + req.getRequestURL() + " on " + new Date() + "."); chain.doFilter(request, response); System.out.println("Within SimpleFilter1:Filtering the Response..."); } catch (IOException ioe) { ioe.printStackTrace(); } catch (ServletException se) { se.printStackTrace(); } } public void destroy() { this.filterConfig = null; }}
web.xml设置不变,同章节3。