Servlet Filter 过滤器 对指定页面不拦截
package niit.dxs.controller; import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException; public class LoginFilter implements Filter {
private String excludedPage;
private String[] excludedPages; @Override
public void init(FilterConfig filterConfig) throws ServletException {
excludedPage = filterConfig.getInitParameter("excludedPages");
if (excludedPage != null && excludedPage.length() > 0){
excludedPages = excludedPage.split(",");
}
} @Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
// 获取 resquest、response、session
HttpServletRequest request = (HttpServletRequest) servletRequest;
HttpServletResponse response = (HttpServletResponse) servletResponse;
HttpSession session = request.getSession(); // 定义表示变量 并验证用户请求URL 是否包含不过滤路径
boolean flag = false;
for (String page:excludedPages) {
if (request.getServletPath().equals(page)){
flag = true;
}
} // 验证用户登录
if (flag){
filterChain.doFilter(request,response);
}{
String userName= (String) session.getAttribute("userKey");
System.out.println("userid——————————》"+userName);
if (userName != null){
filterChain.doFilter(request,response);
}else {
request.getRequestDispatcher("index.jsp").forward(request,response);
}
}
} @Override
public void destroy() { }
}
web.xml
<!-- 过滤器 -->
<filter>
<filter-name>LoginFilter</filter-name>
<filter-class>niit.dxs.controller.LoginFilter</filter-class>
<init-param>
<param-name>excludedPages</param-name>
<param-value>/index.jsp,/register.html,/RegisterServlet,/LoginServlet</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>LoginFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
Servlet Filter 过滤器 对指定页面不拦截的更多相关文章
- JavaWeb -- Servlet Filter 过滤器
1. Servlet API中提供了一个Filter接口,开发web应用时,如果编写的Java类实现了这个接口,则把这个java类称之为过滤器Filter.通过Filter技术,开发人员可以实现用户在 ...
- Servlet Filter 过滤器
Filter也称之为过滤器,它是Servlet技术中最激动人心的技术,WEB开发人员通过Filter技术,对web服务器管理的所有web资源: 例如Jsp, Servlet, 静态图片文件或静态 ht ...
- 【转载】Servlet Filter(过滤器)、Filter是如何实现拦截的、Filter开发入门
Servlet Filter(过滤器).Filter是如何实现拦截的.Filter开发入门 Filter简介 Filter也称之为过滤器,它是Servlet技术中最激动人心的技术,WEB开发人员通过F ...
- Servlet Filter(过滤器)、Filter是如何实现拦截的、Filter开发入门
Servlet Filter(过滤器).Filter是如何实现拦截的.Filter开发入门 Filter简介 Filter也称之为过滤器,它是Servlet技术中最激动人心的技术,WEB开发人员通过F ...
- Servlet的过滤器Filter
Servlet 编写过滤器 Servlet 过滤器可以动态地拦截请求和响应,以变换或使用包含在请求或响应中的信息. 可以将一个或多个 Servlet 过滤器附加到一个 Servlet 或一组 Serv ...
- 【Servlet】Filter过滤器的编写和配置
Servlet的Filter介绍 在Servlet作为过滤器使用时,它可以对客户的请求进行过滤处理,当它处理完成后,它会交给下一个过滤器处理,就这样,客户的请求在过滤链里一个个处理,直到请求发送到目标 ...
- Servlet的学习之Filter过滤器技术(1)
本篇将讲诉Servlet中一项非常重要的技术,Filter过滤器技术.通过过滤器,可以对来自客户端的请求进行拦截,进行预处理或者对最终响应给客户端的数据进行处理后再输出. 要想使用Filter过滤器, ...
- [转]Servlet的学习之Filter过滤器技术
本篇将讲诉Servlet中一项非常重要的技术,Filter过滤器技术.通过过滤器,可以对来自客户端的请求进行拦截,进行预处理或者对最终响应给客户端的数据进行处理后再输出. 要想使用Filter过滤器, ...
- servlet,过滤器,监听器,拦截器的区别
一.目录 1.概念 2.生命周期 3.职责 4.执行过程 二.内容 概念 1.servlet:servlet是一种运行服务器端的java应用程序,具有独立于平台和协议的特性, 可以动态生成web页面它 ...
随机推荐
- ios之gcd
看这里吧 http://www.jianshu.com/p/3a5a55e50e84
- Frequently Asked Questions
转自:http://www.tornadoweb.org/en/stable/faq.html Frequently Asked Questions Why isn’t this example wi ...
- Vue 组件以及生命周期函数
组件相当于母版的功能 新建.vue文件,手动完善 <template><div>根节点</div></template> <script>& ...
- Dividing Infinity - Distributed Partitioning Schemes
This is the second post in a series discussing the architecture and implementation of massively para ...
- django从请求到响应的过程深入讲解
django启动 我们在启动一个django项目的时候,无论你是在命令行执行还是在pycharm直接点击运行,其实都是执行'runserver'的操作,而ruserver是使用django自带的的we ...
- VSCode编辑器编写Python代码
如何用VSCode愉快的写Python https://code.visualstudio.com/ 在学习Python的过程中,一直没有找到比较趁手的第三方编辑器,用的最多的还是Python自带 ...
- Pillow 读取图片截断错误
做图像处理的时候,Pillow是经常使用到的模块. 最近在读取图片的时候出现了一个错误. OSError: image file is truncated (461 bytes not process ...
- 分布式tensorflow
分布式Tensorflow Tensorflow的一个特色就是分布式计算.分布式Tensorflow是由高性能的gRPC框架作为底层技术来支持的.这是一个通信框架gRPC(google remote ...
- 用多个class选择元素
注意下面两个的区别: $(".role-developer.role-designer").css("color","red"); $( ...
- 35. CentOS-6.3安装拼音输入法
安装方法: su root yum install "@Chinese Support" // 安装中文输入法 exit 装好后,在“系统-->首选项”就会看到有“ ...