servlet串行拦截器实现例子
至于串行过滤器有什么作用,我实在不知。我的理解是它只是说明 过滤器的串行运行方式
需求:当用户没有登录访问更新页面的时候,跳转到登录页面
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串行拦截器实现例子的更多相关文章
- 过滤器(servlet.filter)和拦截器(springmvc.interceptor)区别
①拦截器是基于java的反射机制的,而过滤器是基于函数回调. ②拦截器不依赖与servlet容器,过滤器依赖与servlet容器. ③拦截器只能对action请求起作用,而过滤器则可以对几乎所有的请求 ...
- Servlet 过滤器、拦截器、监听器以及文件上传下载
在学习之初,总是对过滤器.拦截器.监听器这三个搞不清楚,现在进行一些记录,方便大家交流,也为了提高自身的学习能力! 如果想要了解这三个的作用,首先对servlet流程进行熟悉了解,servlet是客户 ...
- struts2动态调用+Servlet过滤器+struts2拦截器
周末真的是懒到心慌...... 本文是在完整s2sh项目基础上添加的,不太了解s2sh项目构建的朋友可以先参考一下这几篇文章: eclipse环境下基于tomcat-7.0.82构建struts2项目 ...
- AngularJS 拦截器和应用例子(转)
$httpAngularJS 的 $http 服务允许我们通过发送 HTTP 请求方式与后台进行通信.在某些情况下,我们希望可以俘获所有的请求,并且在将其发送到服务端之前进行操作.还有一些情况是,我们 ...
- AngularJS 拦截器和好棒例子
目录[-] 什么是拦截器? 异步操作 例子 Session 注入(请求拦截器) 时间戳(请求和响应拦截器) 请求恢复 (请求异常拦截) Session 恢复 (响应异常拦截器) 总结 Intercep ...
- Spring MVC - 拦截器实现 和 用户登陆例子
1.拦截器 SpringMvc中的拦截器实现了HandlerInterceptor接口,通常使用与身份认证,授权和校验,模板视图,统一处理等: public class HanderIntercept ...
- java 拦截器、过滤器、监听器
一.理解Struts2拦截器 1. Struts2拦截器是在访问某个Action或Action的某个方法,字段之前或之后实施拦截,并且Struts2拦截器是可插拔的,拦截器是AOP的一种实现. 2. ...
- Struts2拦截器和标签
一.struts2拦截器 1.struts2是框架,封装了很多的功能,struts2里面封装的功能都是在拦截器里面. 2 struts2里面封装了很多的功能,有很多拦截器,不是每次这些拦截器都执行,每 ...
- 1.5(Spring MVC学习笔记) 拦截器(Interceptor)
一.拦截器 1.1拦截器简介 Spring中的拦截器类似Servlet中的过滤器(Filter),主要用于拦截用户请求, 并进行一定的处理操作(如验证权限.记录日志.设置编码方式等). 1.2拦截器实 ...
随机推荐
- debian6之eclipse和jdk安装
安装JDK 目前最新的JDK版本是:Java SE Development Kit 7u5 下载地址:http://www.oracle.com/technetwork/java/javase/dow ...
- spring事务再次理解
2.2.3 只读 事务的第三个特性是它是否为只读事务.如果事务只对后端的数据库进行该操作,数据库可以利用事务的只读特性来进行一些特定的优化.通过将事务设置为只读,你就可以给数据库一个机会,让它应用它认 ...
- SQL-基础学习1--SELECT,LIMIT,DISTINCT,注释
所使用的数据库资料在:数据库资料 1.1 基础概念 1.数据库(database) 保存有组织的数据的容器(通常是一个文件或一组文件) 注意:常用的mysql,等是数据库管理系统DBMS:由这些软件创 ...
- Windows 如何为绿色软件运行时添加参数 如最小化,无窗口运行
1 有些软件运行的时候需要或者可以添加参数来实现一些特殊要求,比如开机自启动,运行时不显示主界面,不显示托盘图标等,比如下面的这个流量精灵软件,"urlcore.exe /h /r /t 4 ...
- Distinct Subsequences(不同子序列的个数)——b字符串在a字符串中出现的次数、动态规划
Given a string S and a string T, count the number of distinct subsequences ofT inS. A subsequence of ...
- python--面向对象封装
from collectoins import namedtuple Point=namedtuple('point',['x','y']) t1=Point(1,2) print(t1.x) pri ...
- python--面向对象—接口
开放封闭原则依赖导致原则接口隔离原则继承多态抽象类和接口类 编程思想:为子类做规范 归一化设计:几个类都实现了相同的方法 抽象类:最好单继承,且可以简单的实现功能 接口类:可以多继承,且最好不实 ...
- kubernetes之pod中断
系列目录 目标读者: 想要构建高可用应用的应用所有者,因此需要知道pod会发生哪些类型的中断 想要执行自动化(比如升级和自动扩容)的集群管理员. 自愿和非自愿的中断 pod不会自动消息,除非有人(可能 ...
- angularJS contenteditable 指令双向绑定
项目遇到需求有点奇葩:双击div使其可编辑,失去焦点后进行数据绑定 通过自定义指令完成 好了上代码: .directive('contentEditable', function() { return ...
- 根据ip识别地区
<script src="http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js"></scri ...