JavaWeb总结(十)
Filter配置详解
web项目目录示意图
<!-- Filter配置 --> <filter> <display-name>Filter_one</display-name> <filter-name>Filter_one</filter-name> <filter-class>com.my.filter.Filter_one</filter-class> </filter> <filter-mapping> <filter-name>Filter_one</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <filter> <display-name>Filter_two</display-name> <filter-name>Filter_two</filter-name> <filter-class>com.my.filter.Filter_two</filter-class> </filter> <filter-mapping> <filter-name>Filter_two</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <filter> <display-name>Filter_three</display-name> <filter-name>Filter_three</filter-name> <filter-class>com.my.filter.Filter_three</filter-class> </filter> <filter-mapping> <filter-name>Filter_three</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- servlet配置 --> <servlet> <description></description> <display-name>Servlet_one</display-name> <servlet-name>Servlet_one</servlet-name> <servlet-class>com.my.servlet.Servlet_one</servlet-class> </servlet> <servlet-mapping> <servlet-name>Servlet_one</servlet-name> <url-pattern>/Servlet_one</url-pattern> </servlet-mapping> <servlet> <description></description> <display-name>Servlet_two</display-name> <servlet-name>Servlet_two</servlet-name> <servlet-class>com.my.servlet.Servlet_two</servlet-class> </servlet> <servlet-mapping> <servlet-name>Servlet_two</servlet-name> <url-pattern>/Servlet_two</url-pattern> </servlet-mapping> <servlet> <description></description> <display-name>Servlet_three</display-name> <servlet-name>Servlet_three</servlet-name> <servlet-class>com.my.servlet.Servlet_three</servlet-class> </servlet> <servlet-mapping> <servlet-name>Servlet_three</servlet-name> <url-pattern>/Servlet_three</url-pattern> </servlet-mapping>
//Filter_one重写的doFilter语句 public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { System.err.println("Filter_one前====>"); chain.doFilter(request, response); System.err.println("Filter_one后====>"); } //Filter_two重写的doFilter语句 public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { System.err.println("Filter_two前====>"); chain.doFilter(request, response); System.err.println("Filter_two后====>"); } //Filter_three重写的doFilter语句 public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { System.err.println("Filter_three前====>"); chain.doFilter(request, response); System.err.println("Filter_three后====>"); } //Servlet_one重写doGet方法 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { System.err.println("Servlet_one===>"); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } //Servlet_two重写doGet方法 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { System.err.println("Servlet_two===>"); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } //Servlet_three重写doGet方法 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { System.err.println("Servlet_three===>"); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); }
已知:过滤器Filter_one、Filter_two、Filter_three都会对Servlet_one进行拦截,那么当我们在浏览器地址输入Servlet_one,客户端会打印什么呢?
输入Servlet_one地址
客户端打印结果
传递资源会经过Filter_one、Filter_two、Filter_three三个过滤器,到达Servlet_one,访问结束后响应又会经过Filter_one、Filter_two、Filter_three三个过滤器,所以才有上图的结果
修改Servlet_one代码
//Servlet_one重写doGet方法 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { System.err.println("Servlet_one===>");
request.getRequestDispatcher("/Servlet_two").forward(request, response);
重定向到Servlet_two结果又会是怎样呢?结果是如下表左边还是右边呢?
Filter_one前====> Filter_two前====> Filter_three前====> Servlet_one===> Servlet_two===> Filter_three后====> Filter_two后====> Filter_one后====> |
Filter_one前====> Filter_two前====> Filter_three前====> Servlet_one===> Filter_one前====> Filter_two前====> Filter_three前====> Servlet_two===> Filter_three后====> Filter_two后====> Filter_one后====> Filter_three后====> Filter_two后====> Filter_one后====> |
结果如上表(结果左)所示
这是为什么呢?过滤器不是对项目内所有文件都要进行过滤吗?为什么这里没有对Servlet_two进行过滤?原因就在这里<dispatcher>标签,内含四个参数:FORWARD、ERROR、INCLUDE、REQUEST;REQUEST为默认标签,并且一个<filter-mapping>内可以包含多个<dispatcher>标签;
修改Filter_one代码
<filter-mapping> <filter-name>Filter_one</filter-name> <url-pattern>/*</url-pattern> <dispatcher>FORWARD</dispatcher> <dispatcher>REQUEST</dispatcher> </filter-mapping>
修改后结果如图所示
结果示意图
这样就可以在Servlet_one跳转到Servlet_two的时候,对Servlet_two也进行过滤。
ERROR参数又有什么作用呢?已知我们在程序调试的时候难免出现异常,或者url路径填写错误,那么网页就会提示这些错误
Error Status 500
Error Status 404
这样的错误提示信息较为相信,明眼人一眼就能看见错误信息,这样你的后台信息就会暴露出来,这样是非常不好。那么怎么替代这些错误信息呢?就要用到<dispatcher>标签中的ERROR参数。
修改Filter_one代码
<!--在标签<filter-mapping>中添加子标签<dispatcher>并且参数为ERROR--> <dispatcher>ERROR</dispatcher> <!--在标签<web-app>中添加子标签<error-page>--> <error-page> <!--状态码--> <error-code>404</error-code> <!--连接路径--> <location>/Test/WebContent/jsp/error_404_page.jsp</location> </error-page> <error-page> <error-code>500</error-code> <location>/Test/WebContent/jsp/error_500_page.jsp</location> </error-page>
当程序再出现异常的时候,就会跳转到我们自己设计的错误提示页面,不会将源代码错误信息显示在页面上。既美化又更加安全、健壮!
Error Status 500
Error Status 404
Filter配置详解
web项目目录示意图
<!-- Filter配置 --> <filter> <display-name>Filter_one</display-name> <filter-name>Filter_one</filter-name> <filter-class>com.my.filter.Filter_one</filter-class> </filter> <filter-mapping> <filter-name>Filter_one</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <filter> <display-name>Filter_two</display-name> <filter-name>Filter_two</filter-name> <filter-class>com.my.filter.Filter_two</filter-class> </filter> <filter-mapping> <filter-name>Filter_two</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <filter> <display-name>Filter_three</display-name> <filter-name>Filter_three</filter-name> <filter-class>com.my.filter.Filter_three</filter-class> </filter> <filter-mapping> <filter-name>Filter_three</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> |
<!-- servlet配置 --> <servlet> <description></description> <display-name>Servlet_one</display-name> <servlet-name>Servlet_one</servlet-name> <servlet-class>com.my.servlet.Servlet_one</servlet-class> </servlet> <servlet-mapping> <servlet-name>Servlet_one</servlet-name> <url-pattern>/Servlet_one</url-pattern> </servlet-mapping> <servlet> <description></description> <display-name>Servlet_two</display-name> <servlet-name>Servlet_two</servlet-name> <servlet-class>com.my.servlet.Servlet_two</servlet-class> </servlet> <servlet-mapping> <servlet-name>Servlet_two</servlet-name> <url-pattern>/Servlet_two</url-pattern> </servlet-mapping> <servlet> <description></description> <display-name>Servlet_three</display-name> <servlet-name>Servlet_three</servlet-name> <servlet-class>com.my.servlet.Servlet_three</servlet-class> </servlet> <servlet-mapping> <servlet-name>Servlet_three</servlet-name> <url-pattern>/Servlet_three</url-pattern> </servlet-mapping> |
//Filter_one重写的doFilter语句 public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { System.err.println("Filter_one前====>"); chain.doFilter(request, response); System.err.println("Filter_one后====>"); } //Filter_two重写的doFilter语句 public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { System.err.println("Filter_two前====>"); chain.doFilter(request, response); System.err.println("Filter_two后====>"); } //Filter_three重写的doFilter语句 public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { System.err.println("Filter_three前====>"); chain.doFilter(request, response); System.err.println("Filter_three后====>"); } //Servlet_one重写doGet方法 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { System.err.println("Servlet_one===>"); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } //Servlet_two重写doGet方法 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { System.err.println("Servlet_two===>"); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } //Servlet_three重写doGet方法 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { System.err.println("Servlet_three===>"); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } |
已知:过滤器Filter_one、Filter_two、Filter_three都会对Servlet_one进行拦截,那么当我们在浏览器地址输入Servlet_one,客户端会打印什么呢?
输入Servlet_one地址
客户端打印结果
|
传递资源会经过Filter_one、Filter_two、Filter_three三个过滤器,到达Servlet_one,访问结束后响应又会经过Filter_one、Filter_two、Filter_three三个过滤器,所以才有上图的结果。 |
修改Servlet_one代码
//Servlet_one重写doGet方法 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { System.err.println("Servlet_one===>");request.getRequestDispatcher("/Servlet_two").forward(request, response); }
|
|
重定向到Servlet_two结果又会是怎样呢? 结果是如左边还是右边呢? |
|
Filter_one前====> Filter_two前====> Filter_three前====> Servlet_one===> Servlet_two===> Filter_three后====> Filter_two后====> Filter_one后====> |
Filter_one前====> Filter_two前====> Filter_three前====> Servlet_one===> Filter_one前====> Filter_two前====> Filter_three前====> Servlet_two===> Filter_three后====> Filter_two后====> Filter_one后====> Filter_three后====> Filter_two后====> Filter_one后====> |
结果如上表(结果左)所示
这是为什么呢?过滤器不是对项目内所有文件都要进行过滤吗?为什么这里没有对Servlet_two进行过滤?原因就在这里<dispatcher>标签,内含四个参数:FORWARD、ERROR、INCLUDE、REQUEST;REQUEST为默认标签,并且一个<filter-mapping>内可以包含多个<dispatcher>标签;
修改Filter_one代码
<filter-mapping> <filter-name>Filter_one</filter-name> <url-pattern>/*</url-pattern> <dispatcher>FORWARD</dispatcher> <dispatcher>REQUEST</dispatcher> </filter-mapping> |
修改后结果如图所示
结果示意图
这样就可以在Servlet_one跳转到Servlet_two的时候,对Servlet_two也进行过滤。
ERROR参数又有什么作用呢?已知我们在程序调试的时候难免出现异常,或者url路径填写错误,那么网页就会提示这些错误
Error Status 500
Error Status 404
这样的错误提示信息较为相信,明眼人一眼就能看见错误信息,这样你的后台信息就会暴露出来,这样是非常不好。那么怎么替代这些错误信息呢?就要用到<dispatcher>标签中的ERROR参数。
修改Filter_one代码
在标签<filter-mapping>中添加子标签<dispatcher>并且参数为ERROR <dispatcher>ERROR</dispatcher> 在标签<web-app>中添加子标签<error-page> <error-page> <!--状态码--> <error-code>404</error-code> <!--连接路径--> <location>/Test/WebContent/jsp/error_404_page.jsp</location> </error-page> <error-page> <error-code>500</error-code> <location>/Test/WebContent/jsp/error_500_page.jsp</location> </error-page> |
当程序再出现异常的时候,就会跳转到我们自己设计的错误提示页面,不会将源代码错误信息显示在页面上。既美化又更加安全、健壮!
Error Status 500
Error Status 404
我不作恶
但有权拒绝为善
我不赞同
但是我捍卫你不为善的权力