星空网 > 软件开发 > Java

JSP网站开发基础总结《十》

  经过上一篇的介绍相信大家对JSP提供的过滤器一定有了一个概念,本篇我们就一起再来研究一下关于创建多个过滤器时,如果有两个以上过滤器的过滤规则相同,那么这些过滤器的执行顺序如何呢?答案是根据我们在web.

 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.

  这里需要注意的时,要达到上面的效果,我们需要在声明过滤规则中,保证两个过滤器匹配的请求一致。

<? ="http://www.w3.org/2001/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控制台的输出:

  JSP网站开发基础总结《十》

  到这里我想大家对于多个Filter执行顺序的问题,应该已经明白其中的道理了吧。

 4、404、500错误过滤:

  大家在上面的web.

  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、当我们在地址栏输入一个不存在页面时:

  JSP网站开发基础总结《十》

  b、当我们在地址栏输入http://localhost:8080/HelloWord/Test.jsp:

  JSP网站开发基础总结《十》

  到这里对于JSP提供Filter类的构建就为大家总结完毕,对于这些功能具体使用,还需大家自己好好摸索。如有疑问,欢迎留言讨论。

 




原标题:JSP网站开发基础总结《十》

关键词:JS

JS
*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流