新建一个过滤器命名为:FilterDemo,该类继承自:javax.servlet.Filter
public class FilterDemo implements Filter{
public void destroy() {}
public void doFilter(ServletRequest sreq, ServletResponse response,
FilterChain filterChain)
throws IOException, ServletException {
HttpServletRequest request=(HttpServletRequest)sreq;
HttpSession session = request.getSession(false);
if(session == null) {
//如果是session超时,在此处做处理
}
if(request.getRequestURI().endsWith("download.do")) {
//此处可以针对不同的请求根据用户是否具有权限来做处理
}
request.getRequestDispatcher("/Error.jsp").
forward(request,response);
}
public void init(FilterConfig filterConfig)
throws ServletException {}
}
然后将新建的Filter添加到web.xml中,部署Filter需要添加两部分:
1.filter元素
filter元素位于部署描述符文件(web.xml)的前部,所有filter-mapping、servlet或servlet-mapping元素之前。
<filter>
<filter-name>SessionFilter</filter-name>
<filter-class>myPackage.FilterDemo</filter-class>
</filter>
2.filter-mapping元素
filter-mapping元素位于web.xml文件中filter元素之后serlvet元素之前
<filter-mapping>
<filter-name>SessionFilter</filter-name>
<url-pattern>*.do</url-pattern>
</filter-mapping>
以上内容表示所以的请求中以.do结尾的都有首先通过我们这里指定的过滤器:
分享到:
相关推荐
先写一个统一的过滤器,或者拦截器,针对Ajax请求进行过滤处理,下面示例以Filter为例: public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ...
假设过期时间为30分钟,校验发生在服务器,借助过滤器,可以这样写 public class PowerFilter : AuthorizeAttribute { public override void OnAuthorization(AuthorizationContext filterContext) { var cookie ...
当session过期后可以用过滤器来设置重定向页面 代码如下:public class ActionFilter extends HttpServlet implements Filter {private FilterConfig filterConfig;public void init(FilterConfig config) {this....
(3)使用parser根据filter或者visitor来取得所有符合条件的节点 (4)对节点内容进行处理 3.3常用Api 3.3.1使用Parser的构造函数创建解释器 Parser() Zero argument constructor. Parser(Lexer lexer) ...
最后我们还需要在web.xml中加入spring的session过滤器,我个人理解这个过滤器的作用是告诉spring来接管对session管理与创建工作。 1 <!--session过滤器--> 2 <filter> 3 <filter-name>springSessionRepositoryFilter...
7.4. 使用其他过滤器 —— 基于框架 8. 核心安全过滤器 8.1. FilterSecurityInterceptor 8.2. ExceptionTranslationFilter 8.2.1. AuthenticationEntryPoint 8.2.2. AccessDeniedHandler 8.3. ...
7.4. 使用其他过滤器 —— 基于框架 8. 核心安全过滤器 8.1. FilterSecurityInterceptor 8.2. ExceptionTranslationFilter 8.2.1. AuthenticationEntryPoint 8.2.2. AccessDeniedHandler 8.3. ...
7.4. 使用其他过滤器—— 基于框架 8. 核心安全过滤器 8.1. FilterSecurityInterceptor 8.2. ExceptionTranslationFilter 8.2.1. AuthenticationEntryPoint 8.2.2. AccessDeniedHandler 8.3. ...
13.5与其他基于过滤器的框架一起使用 118 13.6高级命名空间配置 118 14.核心安全筛选器 119 14.1 FilterSecurityInterceptor 119 14.2 ExceptionTranslationFilter 121 14.2.1 AuthenticationEntryPoint 122 14.2.2 ...
1.6.1 监听器(Listener)顺序 ...............................................................................................................14 1.6.2 注解处理 .............................................
1.6.1 监听器(Listener)顺序 ...............................................................................................................14 1.6.2 注解处理 ...............................................