程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> 關於JAVA >> 淺談Listener、Filter、Servlet初始化順序

淺談Listener、Filter、Servlet初始化順序

編輯:關於JAVA

Listener、Filter、Servlet都有一個初始化的過程,對應的方法分別為:

contextInitialized(ServletContextEvent arg0)

init(FilterConfig filterConfig)

init(ServletConfig config)

那麼它們的初始化順序是什麼呢?

Listener > Filter > Servlet

TestServlet.Java

Java代碼

  1. package com.cos;
  2. import Java.io.IOException;
  3. import Javax.servlet.GenericServlet;
  4. import Javax.servlet.ServletConfig;
  5. import Javax.servlet.ServletException;
  6. import Javax.servlet.ServletRequest;
  7. import Javax.servlet.ServletResponse;
  8. public class TestServlet extends GenericServlet {
  9. @Override
  10. public void init(ServletConfig config){
  11. System.out.println("Servlet 初始化 。。。");
  12. }
  13. @Override
  14. public void service(ServletRequest arg0, ServletResponse arg1) throws ServletException, IOException {
  15. System.out.println("Servlet service 。。。");
  16. }
  17. }

TestFilter.Java

Java代碼

  1. package com.cos;
  2. import Java.io.IOException;
  3. import Javax.servlet.Filter;
  4. import Javax.servlet.FilterChain;
  5. import Javax.servlet.FilterConfig;
  6. import Javax.servlet.ServletException;
  7. import Javax.servlet.ServletRequest;
  8. import Javax.servlet.ServletResponse;
  9. public class TestFilter implements Filter{
  10. public void init(FilterConfig filterConfig) throws ServletException {
  11. System.out.println("Filter 初始化。。。");
  12. }
  13. public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
  14. System.out.println("doFilter 。。。");
  15. chain.doFilter(request, response);
  16. }
  17. public void destroy() {
  18. System.out.println("Filter 銷毀。。。");
  19. }
  20. }

TestListener.Java

Java代碼

  1. package com.cos;
  2. import Javax.servlet.ServletContextEvent;
  3. import Javax.servlet.ServletContextListener;
  4. public class TestListener implements ServletContextListener{
  5. public void contextInitialized(ServletContextEvent arg0) {
  6. System.out.println("Listener 初始化。。。");
  7. }
  8. public void contextDestroyed(ServletContextEvent arg0) {
  9. System.out.println("Listener 銷毀。。。");
  10. }
  11. }

web.XML

XML代碼

  1. <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">
  2. <servlet>
  3. <servlet-name>sservlet-name>
  4. <servlet-class>com.cos.TestServletservlet-class>
  5. servlet>
  6. <servlet-mapping>
  7. <servlet-name>sservlet-name>
  8. <url-pattern>/loginurl-pattern>
  9. servlet-mapping>
  10. <filter>
  11. <filter-name>ffilter-name>
  12. <filter-class>com.cos.TestFilterfilter-class>
  13. filter>
  14. <filter-mapping>
  15. <filter-name>ffilter-name>
  16. <url-pattern>/*url-pattern>
  17. filter-mapping>
  18. <listener>
  19. <listener-class>com.cos.TestListenerlistener-class>
  20. listener>
  21. web-app>
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved