JSP网站开发基础总结《十》
经过上一篇的介绍相信大家对JSP提供的过滤器一定有了一个概念,本篇我们就一起再来研究一下关于创建多个过滤器时,如果有两个以上过滤器的过滤规则相同,那么这些过滤器的执行顺序如何呢?答案是根据我们在web.xml中声明的先后顺序进行执行,也就是先声明的先执行,后声明的后执行。文字的描述大家可能还是不明白,下面就让我们用程序验证一下。
1、新建Filter类:
因为我们需要完成对于多个过滤器的,执行时的先后顺序判断,所以我们至少需要新建两个Filter类。
a、firstFilter.java:
public class firstFilter implements Filter { public void destroy() {
System.out.println("Destory-----first");
} public void doFilter(ServletRequest request, ServletResponse response,
FilterChain arg) throws IOException, ServletException {
System.out.println("start-----first");
arg.doFilter(request, response);//没有该方法,页面将一直处于加载状态。
System.out.println("end-----first");
} public void init(FilterConfig arg0) throws ServletException {
System.out.println("Init-----first");
} }
b、secondFilter.java:
public class secondFilter implements Filter { public void destroy() {
System.out.println("Destroy----second");
} public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
System.out.println("start----second");
chain.doFilter(request, response);
System.out.println("end----second");
} public void init(FilterConfig filterConfig) throws ServletException {
System.out.println("Init----second");
} }
2、web.xml声明:
这里需要注意的时,要达到上面的效果,我们需要在声明过滤规则中,保证两个过滤器匹配的请求一致。
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<!-- 过滤器对象firstFilter声明 -->
<filter>
<filter-name>firstFilter</filter-name>
<!-- 过滤器名 -->
<filter-class>cn.imcook.filter.firstFilter</filter-class>
<!-- 指定我们新建的过滤器对象的地址 -->
</filter> <!-- 过滤器对象secondFilter声明 -->
<filter>
<filter-name>secondFilter</filter-name>
<filter-class>cn.imcook.filter.secondFilter</filter-class>
</filter> <!-- 过滤器firstFilter的规则声明 -->
<filter-mapping>
<filter-name>firstFilter</filter-name>
<!-- 指定规则对于的过滤器对象 -->
<url-pattern>/index.jsp</url-pattern>
<dispatcher>REQUEST</dispatcher>
<!-- 该处有四个值可选,默认是REQUEST -->
</filter-mapping> <!-- 过滤器secondFilter的规则声明 -->
<filter-mapping>
<filter-name>secondFilter</filter-name>
<url-pattern>/index.jsp</url-pattern>
<dispatcher>REQUEST</dispatcher>
</filter-mapping> <!-- 错误处理 -->
<error-page>
<error-code>404</error-code>
<location>/error404.jsp</location>
</error-page>
<error-page>
<error-code>500</error-code>
<location>/error500.jsp</location>
</error-page> <welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<login-config>
<auth-method>BASIC</auth-method>
</login-config>
</web-app>
3、启动项目测试:
在浏览器地址栏输入:http://localhost:8080/HelloWord/index.jsp,观察myeclipse控制台的输出:
到这里我想大家对于多个Filter执行顺序的问题,应该已经明白其中的道理了吧。
4、404、500错误过滤:
大家在上面的web.xml中一定看到了,两个关于404、500错误的过滤监听声明,这是如何实现的呢?我们只需要在我们的web.xml中对这两个错误进行一下声明,系统就会开始监听,一旦出现错误,将会跳转到我们实现设置好的错误提醒页面。
a、error404.jsp:
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>"> <title>404</title> <meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
--> </head> <body>
<center>
<h1>您访问的地址不存在。<a href="index.jsp" style="color: red">返回首页</a></h1>
</center>
</body>
</html>
b、error500.jsp:
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>"> <title>500</title> <meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
--> </head> <body>
<center>
<h1>页面出错了,程序猿正在努力修复中...<a href="index.jsp" style="color: red">返回首页</a></h1>
</center>
</body>
</html>
c、用于测试500错误的Test.jsp:
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>"> <title>测试</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my one web!">
</head> <body>
<center>
<h1>500错误验证页面</h1>
<%=2/0 %><!-- 0不能作为被除数 -->
</center>
</body>
</html>
5、效果:
a、当我们在地址栏输入一个不存在页面时:
b、当我们在地址栏输入http://localhost:8080/HelloWord/Test.jsp:
到这里对于JSP提供Filter类的构建就为大家总结完毕,对于这些功能具体使用,还需大家自己好好摸索。如有疑问,欢迎留言讨论。
JSP网站开发基础总结《十》的更多相关文章
- JSP网站开发基础总结《二》
有了上一篇的学习,我相信大家对于JSP一定有了一定的认识,从今天开始我们真正开启JSP模式,如果你有HTML的基础,那学起JSP来也就方便了很多了,首先JSP做为网站开发语言,它与HTML有很多相似的 ...
- JSP网站开发基础总结《一》
经过JAVASE的学习相信大家对JAVA已经不再陌生,那么JAVA都可以干什么呢?做算法.应用程序.网站开发都可以,从今天开始为大家奉上JAVAEE之JSP动态网站开发基础总结. 既然是动态网站开发, ...
- JSP网站开发基础总结《八》
JSP的学习总结到本篇已经八篇了,内容比较多,但都是实战,本篇最后为大家介绍一个小效果:百度分页.就是当我们遍历的数据对象较多时,这时我们就会看到了这个效果了,那他是如何实现的呢?下面我们就一起学习一 ...
- JSP网站开发基础总结《七》
按照计划本篇将为大家总结搜索功能的两种实现:确定搜索与模糊搜索.所谓精确搜索便是指,根据用户的输入的搜索内容,在数据库中寻找具有一一对应的关系的数据,一般都是用户在数据库中的主键值.而模糊搜索,是一种 ...
- JSP网站开发基础总结《十二》
前两篇已经简单为大家介绍了一下,有关Filter接口的知识,本篇就让我们以一个登录小功能,来具体实现一下过滤器的作用,便于大家掌握.具体为大家介绍一下如何使用Filter对访问进行过滤,及如何防止中文 ...
- JSP网站开发基础总结《四》
经过前几篇的摸爬滚打,下面我们就开始我们真正的数据库操作了,本篇重点在于如何在网站端编写数据库操作语句,内容不多,就是我们常见的增删改查. 0.数据库对象创建: 在JAVASE基础知识总结时,就为大家 ...
- JSP网站开发基础总结《十三》
继上一篇为大家补充过滤器类后,本篇为大家简单介绍一下如何实现验证码效果.验证码的作用是什么呢?1.防止恶意注册2.防止恶意提交3.防止盗取账户等等,总的来说验证码的存在就是为了,防止非人为的操作,不过 ...
- JSP网站开发基础总结《九》
本篇属于附加篇,在之前的总结中给大家提到过一个关于登录状态验证的效果,当时是通过Session对象完成的,今天我查了一下,JSP为我们封装了一个用于过滤用的过滤器类Filter,通过它我们就可以非常轻 ...
- JSP网站开发基础总结《六》
对于本篇需要总结的内容,量估计有点大,大家好好看哈,绝对全是干货,代码的已经运行测试,不存在问题,大家可以参考学习,下面开始本篇的内容. 1.添加数据到数据库: 如何用户在JSP页面的填写的信息输入到 ...
- JSP网站开发基础总结《九》(转)
本篇属于附加篇,在之前的总结中给大家提到过一个关于登录状态验证的效果,当时是通过Session对象完成的,今天我查了一下,JSP为我们封装了一个用于过滤用的过滤器类Filter,通过它我们就可以非常轻 ...
随机推荐
- [UE4][Custom Animation Graph Node]Evaluate Pose by Curve
目的:根据曲线值获得当前动作帧.用于实现各种通过曲线同步的功能. 方法:继承FAnimNode_Base创建自定义动画节点.重写Evaluate部分.创建相应的AnimGraphNode.可参考前一篇 ...
- [UE4]Animation Techniques used in Paragon部分翻译及索引
视频地址:https://www.youtube.com/watch?v=1UOY-FMm-xo 主要内容:该视频由Paragon游戏制作者Laurent Delayen(Senior Program ...
- dom4j解析xml的增加信息
想要在xml中增加信息,那么就要先找到你要加信息的节点 前三行是固定模式,主要是找到xml文件的地址,并且得到根节点,再从根节点中便利出来movie的所有节点之后用集合接收 SAXReader rea ...
- [XAF] How to use the Allow/Deny permissions policy in the existing project
https://www.devexpress.com/Support/Center/Question/Details/T418166 Clear [C#] using DevExpress.Persi ...
- 头部加mead(便于seo优化)
<meta name="Keywords" content="关键词,关键词" /> <meta name="description ...
- oracle数据库导入导出
简单记录下数据泵导出导入expdp .impdp 和 普通导出导入 exp.imp 一.数据泵导出数据库(按用户)步骤: 1.以oracle用户登录oracle所在服务器,创建数据库备份文件目录 &g ...
- sicily1024 Magic Island(图的遍历)
Description There are N cities and N-1 roads in Magic-Island. You can go from one city to any other. ...
- myrocks 之数据字典
data dictionary rocksdb作为mysql的一个新的存储引擎,在存储引擎层,会维护自已的元数据信息.在innodb存储引擎中,我们通过information_schema下的INNO ...
- jQuery+ASP.NET MVC基于CORS实现带cookie的跨域ajax请求
这是今天遇到的一个实际问题,在这篇随笔中记录一下解决方法. ASP.NET Web API提供了CORS支持,但ASP.NET MVC默认不支持,需要自己动手实现.可以写一个用于实现CORS的Acti ...
- 算法:x的n次方
该题是用来公司教学,并无难度.用于说明算法效率差异以及循环和递归的效率差别. package practice; import java.math.BigDecimal; /** * @author ...