下面是该Filter的源代码。
程序清单:codes/02/2.12/ filterTest/WEB-INF/src/lee/AuthorityFilter.java
上面Filter的doFilter方法里3行斜体字代码用于获取Filter的配置参数,而程序中粗体字代码则是此Filter的核心,①号代码按配置参数设置了request编码所用的字符集,接下来的粗体字代码判断session范围内是否有user属性——没有该属性即认为没有登录,如果既没有登录,而且请求地址也不是登录页和处理登录页,系统直接跳转到登录页面。
在web.xml文件中配置该Filter,使用init-param元素为该Filter配置参数,init-param可接受如下两个子元素:
param-name:指定参数名。
param-value:指定参数值。
该Filter的配置片段如下:
上面配置片段中粗体字代码为该Filter指定了3个配置参数,指定loginPage为/login.jsp,proLogin为/proLogin.jsp,这表明:如果没有登录该应用,普通用户只能访问/login.jsp和/proLogin.jsp页面。只有当用户登录该应用后才可自由访问其他页面。
<servlet> <servlet-name>action<servlet-name> <servlet-class>org.apache.struts.action.ActionServlet</servlet-class> <init-param> <param-name>config</param-name> <param-value>/WEB-INF/struts-config.xml</param-value> </init-param> <init-param> <param-name>detail</param-name> <param-value>2</param-value> </init-param> <init-param> <param-name>debug</param-name> <param-value>2</param-value> </init-param> <load-on-startup>2</load-on-startup> </servlet> <servlet-mapping> <servlet-name>action</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping>
<load-on-startup>2</load-on-startup>配置的作用
1)load-on-startup元素标记容器是否在启动的时候就加载这个servlet(实例化并调用其init()方法)。
2)它的值必须是一个整数,表示servlet应该被载入的顺序
3)当值为0或者大于0时,表示容器在应用启动时就加载并初始化这个servlet;
4)当值小于0或者没有指定时,则表示容器在该servlet被选择时才会去加载。
5)正数的值越小,该servlet的优先级越高,应用启动时就越先加载。
6)当值相同时,容器就会自己选择顺序来加载。
本文来自CSDN博客,转载出处:http://blog.csdn.net/doymm2008/archive/2009/11/09/4791435.aspx
相关推荐
Filter过滤器的代码及其web.xml配置代码 很好的解决网页乱码问题,很方便,只要按照名字添加就可以了
web.xml 配置详解,需要的下载了。。。Web.xml常用元素和在java web规范中说明.按照listner,filter,servlet的顺序初始化
本源码将详细介绍web.xml配置中servlet,filter,listener的加载顺序,可以让学习者更好的了解web.xml各种属性配置,自己写的东西,不足之处请大家见谅,顺便收点积分也好下资料,谢谢
关于filter、servlet在web.xml配置及加载顺序
用于 web.xml 配置详解。例如: <web-app> <display-name></display-name>定义了WEB应用的名字 <description></description> 声明WEB应用的描述信息 <context-param></context-param> context-param元素声明应用...
flex4,struts2.3兼容配置web.xml中的filter
Web后端开发-使用Filter过滤器技术,实现访问量统计-方法二使用web.xml配置的方式
java servlet spring web filter listenr
web.xml文件是用来配置:欢迎页、servlet、filter等的。当你的web工程没用到这些时,你可以不用web.xml文件来配置你的web工程。 那么web.xml能做的所有事情都有那些? 其实,web.xml的模式(Schema)文件中定义了多少...
web.xml文件是用来初始化工程配置信息的,比如说welcome页面,filter,listener,servlet,servlet-mapping,启动加载级别等等,当你的web工程中没用到这些当然也就不需要这个xml文件来配置你的apllication了 ...
3.在工程中web.xml配置过滤器 <filter> <filter-name>SimplePageCachingFilter</filter-name> <filter-class>net.sf.ehcache.constructs.web.filter.SimplePageCachingFilter </filter-class> </filter> ...
1、修改tomcat下的Conf/web.xml文件,在该文件内容中新增以下配置,注意,若该web.xml中存在其它filter,则需要将该filter放在所有filter前面; <filter> <filter-name>CorsFilter</filter-name> <filter-class>...
web.xml配置filter和filtermapping,编写类 读取或写死filter的init-params中的字符编码传入 reqeust中 调用dofilter方法,乱码解决
jsp的web.xml配置说明 Web.xml常用元素<web-app><display-name></display-name>定义了WEB应用的名字<description></description> 声明WEB应用的描述信息<context-param></context-param> context-param元素声明应用...
本书共分4部分,从xml、servlet、jsp和应用的角度向读者展示了java web开发中各种技术的应用,循序渐进地引导读者快速掌握java web开发。. 本书内容全面,涵盖了从事java web开发所应掌握的所有知识。在知识的讲解...
而POST请求中,如果出现中文时,会导致程序无法正常运行,会出现中文乱码问题,所以就必须在web.xml中配置POST请求中文乱码问题 <filter> <filter-name>encodingFilter</filter-name> <filter-class>org.spring...
-- authc:该过滤器下的页面必须验证后才能访问,它是Shiro内置的一个拦截器org.apache.shiro.web.filter.authc.FormAuthenticationFilter --> <property name="filterChainDefinitions"> /statics/**=anon ...
配置Filter过滤器处理JSP中文乱码 参考Tomcat服务器目录webapps的examples示例 简单配置步骤: 1.在项目web.xml文件添加过滤器标记<filter>和<filter-mapping>; 2.实现过滤器代码; 3.对Tomcat服务器conf目录里的...
除了添加两个JsFilter.java和GetHttpServletRequestWrapper.java之外,需要在web.xml添加对应的配置。 <!-- 特殊字符过滤 --> <filter> <filter-name>sqlFilter</filter-name> <filter-class>weixin.idea.waiting...
2.配置web.xml <filter> <filter-name>struts2</filter-name> <!-- 过滤器的名字 --> <filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class> <!-- 引用个...