`

ServletConfig FilterConfig作用

 
阅读更多

web.xml里的struts2的配置:

 

<filter>
        <filter-name>struts2</filter-name>
        <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
    </filter>

    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

 

StrutsPrepareAndExecuteFilter这个类是个filter,是个总入口,进来后,容器启动默认加载init方法。

 

public void init(FilterConfig filterConfig) throws ServletException {

。。。。。。

}

 

init方法的参数FilterConfig filterConfig介绍下:

 

FilterConfig对象提供对servlet环境及web.xml文件中指派的过滤器名的访问。
FilterConfig对象具有一个getInitParameter方法,它能够访问部署描述符文件(web.xml)中分配的过滤器初始化参数。

实例:
将下面的代码加入到web.xml中,试用FilterConfig就可以获得以 filter 作为描述标签内的参数。

 

<!-- The Cache Filter -->
<filter>
<!-- 设计过滤处理类,生成静态页面 -->
<filter-name>CacheFilter</filter-name>
<filter-class>com.jspbook.CacheFilter</filter-class>

<!-- 不需要缓存的URL -->
<init-param>
<param-name>/TimeMonger.jsp</param-name>
<param-value>nocache</param-value>
</init-param>

<init-param>
<param-name>/TestCache.jsp</param-name>
<param-value>nocache</param-value>
</init-param>

<!-- 缓存超时时间, 单位为秒 -->
<init-param>
<param-name>cacheTimeout</param-name>
<param-value>600</param-value>
</init-param>

<!-- 是否根据浏览器不同的地区设置进行缓存(生成的缓存文件为 test.jspid=1_zh_CN 的格式) -->
<init-param>
<param-name>locale-sensitive</param-name>
<param-value>true</param-value>
</init-param>

</filter>

<filter-mapping>
<filter-name>CacheFilter</filter-name>
<url-pattern>*.jsp</url-pattern>
</filter-mapping> 

用法:

filterConfig.getInitParameter("locale-sensitive"); 得到的就是 ture
filterConfig.getInitParameter("cacheTimeout"); 得到的就是 600
filterConfig.getInitParameter(request.getRequestURI()); 得到的就是param-name 对应的 param-value 值


过滤处理类:

public class CacheFilter implements Filter {
ServletContext sc;
FilterConfig fc;
long cacheTimeout = Long.MAX_VALUE;

public void doFilter(ServletRequest req, ServletResponse res,
FilterChain chain) throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest) req;
HttpServletResponse response = (HttpServletResponse) res;

// check if was a resource that shouldn't be cached.
String r = sc.getRealPath("");
String path = fc.getInitParameter(request.getRequestURI());
if (path != null && path.equals("nocache")) {
chain.doFilter(request, response);
return;
}
path = r + path;

}

public void init(FilterConfig filterConfig) {
this.fc = filterConfig;
String ct = fc.getInitParameter("cacheTimeout");
if (ct != null) {
cacheTimeout = 60 * 1000 * Long.parseLong(ct);
}
this.sc = filterConfig.getServletContext();
}

public void destroy() {
this.sc = null;
this.fc = null;
}
}

 

分享到:
评论

相关推荐

    ServletContext与ServletConfig关系

    在 Servlet 编程中,ServletConfig 和 ServletContext 两个对象经常被混淆,然而它们有着截然不同的作用域和用途。 首先, lets 看看 ServletConfig 对象。ServletConfig 对象是 Servlet 的配置对象,用于存储 ...

    javaWEB总结(3):ServletConfig对象

    ServletConfig对象的试验代码

    ServletConfig与ServletContext.docx

    注意:每个servlet有一个ServletConfig,每个Web应用有一个ServletContext.要把初始化参数认为是部署时常量,可用在运行时得到这些初始化参数,但不能设置初始化参数,根本没有setInitParameter().如果修改XML来改变...

    ServletConfig的描述

    NULL 博文链接:https://shoushounihao.iteye.com/blog/1919541

    ServletContext与ServletConfig的深度分析

    很清楚的讲解了ServletContext和ServletConfig,不光有理论上的讲解,还有例子。

    ServletConfig

    博文链接:https://hotfisher.iteye.com/blog/92177

    超全面javaweb教程28天第9天 6 ServletConfig介绍

    超全面javaweb教程28天第9天_6_ServletConfig介绍

    JavaEE中关于ServletConfig的小结

    ServletConfig是针对特定的Servlet的参数或属性。ServletConfig是表示单独的Servlet的配置和参数,只是适用于特定的Servlet。从一个servlet被实例化后,对任何客户端在任何时候访问有效,但仅对本servlet有效,一个...

    servletAPI中文版(TXT格式,自己翻译)

    ServletConfig getServletConfig() 返回传递到servlet的init()方法的ServletConfig对象 void service(ServletRequest request, ServletResponse response)throws ServletException,IOException 处理request对象中...

    有关ServletConfig与ServletContext的访问

    下面小编就为大家带来一篇有关ServletConfig与ServletContext的访问。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧

    JSP的九个内置对象及四个作用域

    Config 对象是javax.servlet.ServletConfig类型,“Config”对象提供了一些配置信息,常用的方法有getInitParameter和getInitParameterNames,以获得servlet初始化时的参数。 6. Response 对象(响应对象) ...

    JavaWeb新版教程Servlet-1vlet-ServletConfig类使用介绍.avi

    JavaWeb新版教程Servlet-1vlet-ServletConfig类使用介绍.avi

    JavaWeb新版教程Servlet-1vlet-ServletConfig类的补充说明.avi

    JavaWeb新版教程Servlet-1vlet-ServletConfig类的补充说明.avi

    jsp servlet面试题总结

    7. config 配置对象,类型 javax.servlet.ServletConfig,作用域 Page 8. page 页面对象,类型 javax.lang.Object,作用域 Page 9. exception 例外对象,类型 javax.lang.Throwable,作用域 Page jsp 共有六种基本...

    jsp有哪些内置对象?作用分别是什么?分别有什么方法?

    JSP共有以下9个内置的对象:  request 用户端请求,此请求会包含来自GET/... config表示一个javax.servlet.ServletConfig对象。该对象用于存取servlet实例的初始化参数。  page表示从该页面产生的一个servlet实例

    JAVA J2EE 类库文档

    FilterConfig GenericServlet HttpServlet HttpServletRequest HttpServletRequestWrapper HttpServletResponse HttpServletResponseWrapper HttpSession HttpSessionActivationListener ...

    servlet-api-2.4.jar.zip

    javax.servlet.FilterConfig javax.servlet.ServletConfig javax.servlet.GenericServlet javax.servlet.ServletContext javax.servlet.ServletRequest javax.servlet.http.HttpUtils javax.servlet.ServletResponse...

    jsp期末概念总结

    通过ServletConfig提供的getSerletContext()  方式三 通过HttpSession提供的getSerletContext()  方式四 通过FilterConfig提供的getSerletContext() Servlet上下文绑定的数可以被整个应用上的所有组建共享,...

Global site tag (gtag.js) - Google Analytics