至于串行过滤器有什么作用,我实在不知。我的理解是它只是说明 过滤器的串行运行方式

需求:当用户没有登录访问更新页面的时候,跳转到登录页面

1.登录页面:login.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="UserLoginServlet" method="post">
username:<input type="text" value="<%=null == request.getAttribute("username")?"" : request.getAttribute("username") %>" name="username"><br>
password:<input type="text" name="password"><br> authority:<select name="authority"> <option value="1" <%="1".equals(request.getAttribute("authority"))?"selected = selected" : "" %>>common user</option>
<option value="2" <%="2".equals(request.getAttribute("authority"))?"selected = selected" : "" %>>adminstrator</option>
</select>
<br>
<input type="submit">
</form> </body>
</html>

login.jsp

2.登录页面的servet容器

package com.filter;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession; import com.javabean.User; public class UserLoginServlet extends HttpServlet
{
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException
{ String username = (String)req.getParameter("username");
String password = (String)req.getParameter("password");
String authority = (String)req.getParameter("authority"); if("1".equals(authority) && "zhangsan".equals(username) && "123".equals(password))
{
User user = new User();
user.setUsername(username);
user.setPassword(password);
user.setAuthority(authority); HttpSession session = req.getSession(); session.setAttribute("user",user); req.getRequestDispatcher("index.jsp").forward(req, resp); return ;
}
if("2".equals(authority) && "lisi".equals(username) && "456".equals(password))
{
User user = new User();
user.setUsername(username);
user.setPassword(password);
user.setAuthority(authority); HttpSession session = req.getSession(); session.setAttribute("user",user); req.getRequestDispatcher("index.jsp").forward(req, resp); return ;
} System.out.println("meiyou denglu"); resp.sendRedirect("login.jsp?error=true&username="+username+"&authority="+authority); } }

UserLoginServlet

3.主页面index.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="com.javabean.User" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<a href="QueryServletFilter">query</a><br>
<%
User user = (User)session.getAttribute("user");
if(null == user)
{
response.sendRedirect("login.jsp");
return;
}
if("2".equals(((User)session.getAttribute("user")).getAuthority())){ %>
<a href="UpdateServletFilter">update</a> <%} %>
</body>
</html>

index.jsp

4.查询servlet. QueryServletFilter

package com.filter;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; public class QueryServletFilter extends HttpServlet
{
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException
{
System.out.println("QueryServletFilter run");
}
}

5.更新servlet容器. UpdateServletFilter

package com.filter;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; public class UpdateServletFilter extends HttpServlet
{
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException
{
System.out.println("UpdateServletFilter run");
}
}

UpdateServletFilter

6.web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>filter</display-name> <servlet>
<servlet-name>UserLoginServlet</servlet-name>
<servlet-class>com.filter.UserLoginServlet</servlet-class>
</servlet> <servlet-mapping>
<servlet-name>UserLoginServlet</servlet-name>
<url-pattern>/filter/UserLoginServlet</url-pattern>
</servlet-mapping> <servlet>
<servlet-name>QueryServletFilter</servlet-name>
<servlet-class>com.filter.QueryServletFilter</servlet-class>
</servlet> <servlet-mapping>
<servlet-name>QueryServletFilter</servlet-name>
<url-pattern>/filter/QueryServletFilter</url-pattern>
</servlet-mapping> <servlet>
<servlet-name>UpdateServletFilter</servlet-name>
<servlet-class>com.filter.UpdateServletFilter</servlet-class>
</servlet> <servlet-mapping>
<servlet-name>UpdateServletFilter</servlet-name>
<url-pattern>/filter/UpdateServletFilter</url-pattern>
</servlet-mapping>
<!-- filter start -->
<filter>
<description>this is the servlet filter example</description>
<filter-name>userLoginServletFilter1</filter-name>
<filter-class>com.filter.UserLoginServletFilter</filter-class>
</filter> <!-- 演示过滤器串行运行 -->
<filter>
<description>this is the servlet filter example</description>
<filter-name>userLoginServletFilter2</filter-name>
<filter-class>com.filter.UserLoginServletFilter2</filter-class>
</filter> <!-- url-pattern表示只过滤 UpdateServletFilter这一个servlet容器-->
<filter-mapping>
<filter-name>userLoginServletFilter1</filter-name>
<url-pattern>/filter/UpdateServletFilter</url-pattern>
</filter-mapping> <!-- url-pattern表示只过滤 UpdateServletFilter这一个servlet容器-->
<filter-mapping>
<filter-name>userLoginServletFilter2</filter-name>
<url-pattern>/filter/UpdateServletFilter</url-pattern>
</filter-mapping> <!-- filter end -->
</web-app>

web.xml

7.filter拦截器 UserLoginServletFilter

package com.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession; public class UserLoginServletFilter implements Filter
{ @Override
public void destroy()
{
System.out.println("destroy");
} @Override
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain filterChain) throws IOException, ServletException
{ HttpServletRequest req = (HttpServletRequest)request;
HttpSession s = req.getSession(); if(req.getRequestURI().endsWith("login.jsp") || req.getRequestURI().endsWith("UserLoginServlet"))
{
System.out.println("Filter1 start");
filterChain.doFilter(request, response);
System.out.println("filter1 end");
return ;
} if(null == s.getAttribute("user"))
{
System.out.println("Filter1 start..");
((HttpServletResponse)response).sendRedirect("login.jsp");
return;
}
else
{
System.out.println("Filter1 start....");
filterChain.doFilter(request, response); } } @Override
public void init(FilterConfig filterConfig) throws ServletException
{
System.out.println("init");
} }

UserLoginServletFilter

8.filter拦截器2 UserLoginServletFilter2

package com.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse; public class UserLoginServletFilter2 implements Filter
{ @Override
public void destroy()
{
System.out.println("destory2"); } @Override
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain filterChain) throws IOException, ServletException
{
System.out.println("doFilter2 start");
filterChain.doFilter(request, response);
System.out.println("doFilter2 end"); } @Override
public void init(FilterConfig arg0) throws ServletException
{
System.out.println("init2");
} }

UserLoginServletFilter2

web.xml中url-pattern属性值表示访问的路径。

servlet串行拦截器实现例子的更多相关文章

  1. 过滤器(servlet.filter)和拦截器(springmvc.interceptor)区别

    ①拦截器是基于java的反射机制的,而过滤器是基于函数回调. ②拦截器不依赖与servlet容器,过滤器依赖与servlet容器. ③拦截器只能对action请求起作用,而过滤器则可以对几乎所有的请求 ...

  2. Servlet 过滤器、拦截器、监听器以及文件上传下载

    在学习之初,总是对过滤器.拦截器.监听器这三个搞不清楚,现在进行一些记录,方便大家交流,也为了提高自身的学习能力! 如果想要了解这三个的作用,首先对servlet流程进行熟悉了解,servlet是客户 ...

  3. struts2动态调用+Servlet过滤器+struts2拦截器

    周末真的是懒到心慌...... 本文是在完整s2sh项目基础上添加的,不太了解s2sh项目构建的朋友可以先参考一下这几篇文章: eclipse环境下基于tomcat-7.0.82构建struts2项目 ...

  4. AngularJS 拦截器和应用例子(转)

    $httpAngularJS 的 $http 服务允许我们通过发送 HTTP 请求方式与后台进行通信.在某些情况下,我们希望可以俘获所有的请求,并且在将其发送到服务端之前进行操作.还有一些情况是,我们 ...

  5. AngularJS 拦截器和好棒例子

    目录[-] 什么是拦截器? 异步操作 例子 Session 注入(请求拦截器) 时间戳(请求和响应拦截器) 请求恢复 (请求异常拦截) Session 恢复 (响应异常拦截器) 总结 Intercep ...

  6. Spring MVC - 拦截器实现 和 用户登陆例子

    1.拦截器 SpringMvc中的拦截器实现了HandlerInterceptor接口,通常使用与身份认证,授权和校验,模板视图,统一处理等: public class HanderIntercept ...

  7. java 拦截器、过滤器、监听器

    一.理解Struts2拦截器 1. Struts2拦截器是在访问某个Action或Action的某个方法,字段之前或之后实施拦截,并且Struts2拦截器是可插拔的,拦截器是AOP的一种实现. 2. ...

  8. Struts2拦截器和标签

    一.struts2拦截器 1.struts2是框架,封装了很多的功能,struts2里面封装的功能都是在拦截器里面. 2 struts2里面封装了很多的功能,有很多拦截器,不是每次这些拦截器都执行,每 ...

  9. 1.5(Spring MVC学习笔记) 拦截器(Interceptor)

    一.拦截器 1.1拦截器简介 Spring中的拦截器类似Servlet中的过滤器(Filter),主要用于拦截用户请求, 并进行一定的处理操作(如验证权限.记录日志.设置编码方式等). 1.2拦截器实 ...

随机推荐

  1. maven项目对于maven远程仓库没有资源的解决办法

    下载资源到本地 方法一:dependency scope 是system 类型 <dependency> <groupId>edu.ucar</groupId> & ...

  2. 高并发场景下System.currentTimeMillis()的性能优化

    一.前言 System.currentTimeMillis()的调用比new一个普通对象要耗时的多(具体耗时高出多少我也不知道,不过听说在100倍左右),然而该方法又是一个常用方法, 有时不得不使用, ...

  3. CodeSign error: code signing is required for product type 'Application' in SDK 'iOS 7.0'

    这个一般是证书设置的问题, 在build settings中找到 Code Signing->Code Signing Identity修改成有效的证书即可

  4. Xcode搭建真机调试环境 图文实例

    本文介绍的Xcode搭建真机调试环境 图文实例,图文并茂,使我们学习起来更方便些,我们先来看内容. AD: 2013云计算架构师峰会超低价抢票中 Xcode搭建真机调试环境 是本文要介绍的内容,不多说 ...

  5. [反汇编练习] 160个CrackMe之037

    [反汇编练习] 160个CrackMe之037. 本系列文章的目的是从一个没有任何经验的新手的角度(其实就是我自己),一步步尝试将160个CrackMe全部破解,如果可以,通过任何方式写出一个类似于注 ...

  6. Hibernate中的条件查询完毕类

    Hibernate中的条件查询有下面三个类完毕: 1.Criteria:代表一次查询 2.Criterion:代表一个查询条件 3.Restrictions:产生查询条件的工具类

  7. 【KMP】hdu1867(A + B for you again) 杭电java a题真坑

    点击打开链接 Problem Description Generally speaking, there are a lot of problems about strings processing. ...

  8. SD卡操作相关的工具SDCardUtils

    SD卡操作相关的工具 package com.flyou.utils; import java.io.File; import android.os.Environment; import andro ...

  9. BZOJ 3727 PA2014 Final Zadanie 树形DP

    题目大意:给定一棵树,令一个点到全部点的距离与点权的乘积之和为b[i].求每一个点的权值a[i] 首先假设给定a[i]我们能够非常轻松的求出b[i] 可是反过来怎么搞?高斯消元?30W? 考虑已知a[ ...

  10. jquery+css实现网页颜色主题变换(只改变已设置好的几种颜色主题)

    又遇到颜色主题变化,这次使用了jquery+css,使用了函数传值,而不是之前网站换肤改变link的方法. 首先是设置好颜色主题后,点击改变页面颜色主题.(需要自行导入jquery.js后查看效果) ...