zoukankan      html  css  js  c++  java
  • 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类的构建就为大家总结完毕,对于这些功能具体使用,还需大家自己好好摸索。如有疑问,欢迎留言讨论。

  • 相关阅读:
    windows 按时自动化任务
    Linux libusb 安装及简单使用
    Linux 交换eth0和eth1
    I.MX6 GPS JNI HAL register init hacking
    I.MX6 Android mmm convenient to use
    I.MX6 GPS Android HAL Framework 调试
    Android GPS GPSBasics project hacking
    Python windows serial
    【JAVA】别特注意,POI中getLastRowNum() 和getLastCellNum()的区别
    freemarker跳出循环
  • 原文地址:https://www.cnblogs.com/AndroidJotting/p/4351835.html
Copyright © 2011-2022 走看看