`
- 浏览:
46465 次
- 来自:
...
-
java 代码
- 一、使浏览器不缓存页面的过滤器
- import javax.servlet.*;
- import javax.servlet.http.HttpServletResponse;
- import java.io.IOException;
-
-
-
-
- public class ForceNoCacheFilter implements Filter {
-
- public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain) throws IOException, ServletException
- {
- ((HttpServletResponse) response).setHeader("Cache-Control","no-cache");
- ((HttpServletResponse) response).setHeader("Pragma","no-cache");
- ((HttpServletResponse) response).setDateHeader ("Expires", -1);
- filterChain.doFilter(request, response);
- }
-
- public void destroy()
- {
- }
-
- public void init(FilterConfig filterConfig) throws ServletException
- {
- }
- }
-
- 二、检测用户是否登陆的过滤器
-
- import javax.servlet.*;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import javax.servlet.http.HttpSession;
- import java.util.List;
- import java.util.ArrayList;
- import java.util.StringTokenizer;
- import java.io.IOException;
-
-
-
-
-
-
-
-
- public class CheckLoginFilter
- implements Filter
- {
- protected FilterConfig filterConfig = null;
- private String redirectURL = null;
- private List notCheckURLList = new ArrayList();
- private String sessionKey = null;
-
- public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException
- {
- HttpServletRequest request = (HttpServletRequest) servletRequest;
- HttpServletResponse response = (HttpServletResponse) servletResponse;
-
- HttpSession session = request.getSession();
- if(sessionKey == null)
- {
- filterChain.doFilter(request, response);
- return;
- }
- if((!checkRequestURIIntNotFilterList(request)) && session.getAttribute(sessionKey) == null)
- {
- response.sendRedirect(request.getContextPath() + redirectURL);
- return;
- }
- filterChain.doFilter(servletRequest, servletResponse);
- }
-
- public void destroy()
- {
- notCheckURLList.clear();
- }
-
- private boolean checkRequestURIIntNotFilterList(HttpServletRequest request)
- {
- String uri = request.getServletPath() + (request.getPathInfo() == null ? "" : request.getPathInfo());
- return notCheckURLList.contains(uri);
- }
-
- public void init(FilterConfig filterConfig) throws ServletException
- {
- this.filterConfig = filterConfig;
- redirectURL = filterConfig.getInitParameter("redirectURL");
- sessionKey = filterConfig.getInitParameter("checkSessionKey");
-
- String notCheckURLListStr = filterConfig.getInitParameter("notCheckURLList");
-
- if(notCheckURLListStr != null)
- {
- StringTokenizer st = new StringTokenizer(notCheckURLListStr, ";");
- notCheckURLList.clear();
- while(st.hasMoreTokens())
- {
- notCheckURLList.add(st.nextToken());
- }
- }
- }
- }
-
- 三、字符编码的过滤器
-
- import javax.servlet.*;
- import java.io.IOException;
-
-
-
-
- public class CharacterEncodingFilter
- implements Filter
- {
- protected FilterConfig filterConfig = null;
- protected String encoding = "";
-
- public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException
- {
- if(encoding != null)
- servletRequest.setCharacterEncoding(encoding);
- filterChain.doFilter(servletRequest, servletResponse);
- }
-
- public void destroy()
- {
- filterConfig = null;
- encoding = null;
- }
-
- public void init(FilterConfig filterConfig) throws ServletException
- {
- this.filterConfig = filterConfig;
- this.encoding = filterConfig.getInitParameter("encoding");
-
- }
- }
-
- 四、资源保护过滤器
-
-
- package catalog.view.util;
-
- import javax.servlet.Filter;
- import javax.servlet.FilterConfig;
- import javax.servlet.ServletRequest;
- import javax.servlet.ServletResponse;
- import javax.servlet.FilterChain;
- import javax.servlet.ServletException;
- import javax.servlet.http.HttpServletRequest;
- import java.io.IOException;
- import java.util.Iterator;
- import java.util.Set;
- import java.util.HashSet;
-
- import org.apache.commons.logging.Log;
- import org.apache.commons.logging.LogFactory;
-
-
-
-
-
-
-
-
- public class SecurityFilter implements Filter {
-
- private static final String LOGIN_PAGE_URI = "login.jsf";
-
-
- private Log logger = LogFactory.getLog(this.getClass());
-
-
- private Set restrictedResources;
-
-
-
-
- public void init(FilterConfig filterConfig) throws ServletException {
- this.restrictedResources = new HashSet();
- this.restrictedResources.add("/createProduct.jsf");
- this.restrictedResources.add("/editProduct.jsf");
- this.restrictedResources.add("/productList.jsf");
- }
-
-
-
-
- public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain)
- throws IOException, ServletException {
- this.logger.debug("doFilter");
-
- String contextPath = ((HttpServletRequest)req).getContextPath();
- String requestUri = ((HttpServletRequest)req).getRequestURI();
-
- this.logger.debug("contextPath = " + contextPath);
- this.logger.debug("requestUri = " + requestUri);
-
- if (this.contains(requestUri, contextPath) && !this.authorize((HttpServletRequest)req)) {
- this.logger.debug("authorization failed");
- ((HttpServletRequest)req).getRequestDispatcher(LOGIN_PAGE_URI).forward(req, res);
- }
- else {
- this.logger.debug("authorization succeeded");
- chain.doFilter(req, res);
- }
- }
-
- public void destroy() {}
-
- private boolean contains(String value, String contextPath) {
- Iterator ite = this.restrictedResources.iterator();
-
- while (ite.hasNext()) {
- String restrictedResource = (String)ite.next();
-
- if ((contextPath + restrictedResource).equalsIgnoreCase(value)) {
- return true;
- }
- }
-
- return false;
- }
-
- private boolean authorize(HttpServletRequest req) {
-
-
-
-
-
-
-
-
-
-
-
- }
- }
转自:http://hi.baidu.com/samxx8/blog/item/291a788ddb438c10b31bba35.html
分享到:
Global site tag (gtag.js) - Google Analytics
相关推荐
Filter-四个有用的Java过滤器
java过滤器 过滤器 J2EE过滤器 filter过滤器
servlet四个有用的过滤器,包括中文转码,缓存过滤等等
四个有用的Java过滤器收藏,实用开发的工具类。
例如,在上述示例中,我们需要使用 "*0" 查询出四个数据记录(1011、010、201、301),但是使用默认的过滤器机制无法实现这种查询操作。这时,我们需要自定义过滤器以满足特定的查询需求。 自定义过滤器的实现 ...
这是写的filter过滤器的使用,在maven项目和在springboot项目里面都能使用,并且有使用方法。
在 Struts2 中,过滤器是通过 FilterDispatcher 来实现的,FilterDispatcher 负责四个方面的功能:执行 Actions、清除 ActionContext、维护静态内容和清除 request 生命周期内的 XWork 的 interceptors。 拦截器和...
Servlet过滤器大全,各种详细使用的代码! 一、字符编码的过滤器 二、使浏览器不缓存页面的过滤器 三、检测用户是否登陆的过滤器 四、资源保护过滤器 五 利用Filter限制用户浏览权限
有各种基准可以评估不同负载下的错误概率,并可以通过四个参数进行速度测试:插入,统一查找(标准情况下统一查找结果为“ no” whp),真实查找(过滤器中的元素)和删除。 用法 依存关系 由于CF使用openssl库,...
过滤器的四种拦截方式三:过滤器的应用场景1.案例一:分IP统计访问次数2.案例二:粗粒度权限管理3.案例三:全站编码问题4.案例四:页面静态化(图书管理小项目) 一:过滤器概述 1.什么是过滤器 过滤器是JavaWeb三大...
添加了字符过滤器,因此用户可以根据字符进行过滤,并且可以选择网站默认过滤器所提供的四个字符限制以外的字符。 还添加了无限滚动功能,因此现在页面将自动加载和过滤,而不必手动浏览有时只有一两个结果的页面。 ...
表过滤器基于 Bootstrap + JQuery 的 HTML 表格过滤器插件,您只需将表格的 id 传递给 tableFilter.filter('id of the table') 方法,点击此按钮将打开一个过滤器弹出窗口一个二 1 三1 one2 二 2 三 2 一个3 二三三...
快速导引过滤器 何凯明的快速导引过滤器 我们已将其上传到github,目的是使其易于访问开发人员。 原始代码(与该存储库相同)存储在 用于“快速引导过滤器”的Matlab演示代码...运行四个示例以查看本文中显示的结果。
粒子过滤器 雷茂 芝加哥大学 介绍 粒子滤波器是用于解决统计推断问题的蒙特卡洛算法。 在该项目中,使用粒子过滤器推断了乌龟在迷宫中的位置和前进方向。 绿海龟是实际位置,而橙色海龟是估计位置。 箭头是粒子。 ...
这个库带有四个或多或少的可重用过滤器,并且很容易实现自定义过滤器。也许最受欢迎的一个是 Locale 路由过滤器: Locale- 预先添加页面的 :locale(例如 /de/products) Pagination- 附加 page/:num(例如 /...
c) Filter:过滤器 d) BeanUtils:数据封装 e) Jackson:json序列化工具 4.2 Service层 f) Javamail:java发送邮件工具 g) Redis:nosql内存数据库 h) Jedis:java的redis客户端 4.3 Dao层 i) Mysql:数据库 j) ...
PF_PACKET类型的socket允许我们把一个名为LPF的过滤器直接放到处理过程中,过滤器在网卡接收中断执行后立即执行。 三、setfilter函数 setfilter函数是Libpcap中用于设置过滤器的函数。该函数首先检查句柄和过滤器...
Intent Filter就是用来注册 Activity 、Service 、 Broadcast、Receiver(四大组件) 具有能在某种数据上执行一个动作的能力。使用 Intent Filter ,应用程序组件告诉 Android ,它们能为其它程序的组件的动作请求...
过滤器安装是包过滤机制的一个重要步骤,涉及到过滤器的安装和配置。在Libpcap中,过滤器安装可以使用setfilter函数来实现,该函数可以将过滤器安装到内核空间中,以便高效地过滤网络数据包。 六、结论 Linux下的...
dispatcher元素用于定义过滤器的请求方式,可以是REQUEST、INCLUDE、FORWARD或ERROR 四种,默认为 REQUEST。 web.xml文件是Java Web应用程序的核心配置文件,用于定义Web应用程序的各种配置信息。通过学习web.xml...