<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                企業??AI智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                Windows的IIS,是用UI界面進行站點的配置;Linux下面的幾乎所有系統,都是使用配置文件來進行配置,Java容器(JBoss/Tomcat/Jetty/WebSphere/WebLogic等等)也不例外,它們使用一個部署在WEB-INFO目錄下面的web.xml來作為站點配置文件。 本文參考互聯網文章,學習并記錄web.xml的加載順序及配置詳解。 # web.xml加載順序 應用服務器啟動時web.xml的加載過程,和這些節點在xml文件中的前后順序沒有關系,不過有些應用服務器,比如WebSphere,就嚴格要求web.xml的節點順序,否則部署不成功,所以最好還是按照web.xml的標準格式寫,即:context-param --> listener --> filter --> servlet 。 或者根據IDE的提示,比如,如果順序不對,IDE可能有如下提示: The content of element type "web-app" must match "(icon?,display- ?name?,description?,distributable?,context-param *,filter *,filter-mapping *,listener *,servlet *,servlet- ?mapping *,session-config?,mime-mapping *,welcome-file-list?,error-page *,taglib *,resource-env-ref *,resource- ?ref *,security-constraint *,login-config?,security-role *,env-entry*,ejb-ref*,ejb-local-ref *)". 1. 啟動WEB項目的時候,應用服務器會去讀它的配置文件web.xml,讀兩個節點:<listener></listener> 和 <context-param></context-param> ?? 1. 緊接著,容器創建一個ServletContext(上下文),這個WEB項目所有部分都將共享這個上下文 1. 容器將<context-param></context-param>轉化為鍵值對,并交給ServletContext 1. 容器創建<listener></listener>中的類實例,即創建監聽 1. 在監聽中會有contextInitialized(ServletContextEvent args)初始化方法,在這個方法中獲得: ?ServletContext = ServletContextEvent.getServletContext(); ?context-param的值 = ServletContext.getInitParameter("context-param的鍵");? 1. 得到這個context-param的值之后,就可以做一些操作了。注意,這個時候WEB項目還沒有完全啟動完成,這個動作會比所有的Servlet都要早。換句話說,這個時候,你對<context-param>中的鍵值做的操作,將在你的WEB項目完全啟動之前被執行,如果想在項目啟動之前就打開數據庫,那么就可以在<context-param>中設置數據庫的連接方式,在監聽類中初始化數據庫的連接,這個監聽是自己寫的一個類,除了初始化方法,它還有銷毀方法,用于關閉應用前釋放資源,比如說數據庫連接的關閉。 對于某類配置節而言,與它們出現的順序是有關的。 以 filter 為例,web.xml 中當然可以定義多個 filter,與 filter 相關的一個配置節是 filter-mapping,這里一定要注意,對于擁有相同 filter-name 的 filter 和 filter-mapping 配置節而言,filter-mapping 必須出現在 filter 之后,否則當解析到 filter-mapping 時,它所對應的 filter-name 還未定義。 web 容器啟動時初始化每個 filter 時,是按照 filter 配置節出現的順序來初始化的,當請求資源匹配多個 filter-mapping 時,filter 攔截資源是按照 filter-mapping 配置節出現的順序來依次調用 doFilter() 方法的。? servlet 同 filter 類似,此處不再贅述。 比如filter 需要用到 bean ,但加載順序是: 先加載filter 后加載spring,則filter中初始化操作中的bean為null;所以,如果過濾器中要使用到 bean,可以將spring 的加載 改成 Listener的方式: ~~~ <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> ~~~ # web.xml節點解析 ### <context-param /> 用來設定web站點的環境參數 它包含兩個子元素:<param-name></param-name> 用來指定參數的名稱;<param-value></param-value> 用來設定參數值 在此設定的參數,可以在servlet中用 getServletContext().getInitParameter("my_param") 來取得 例子: ~~~ <context-param> <param-name>webAppRootKey</param-name> <param-value>privilege.root</param-value> </context-param> <context-param> <param-name>contextConfigLocation</param-name> <param-value> classpath*:/applicationContext*.xml classpath*:/cas-authority.xml </param-value> </context-param> <context-param> <param-name>log4jConfigLocation</param-name> <param-value>/WEB-INF/classes/log4j.xml</param-value> </context-param> <context-param> <param-name>spring.profiles.default</param-name> <param-value>dev</param-value> </context-param> ~~~ ### <listener /> 用來設定Listener接口 它的主要子元素為?<listener-class></listener-class> ,用來定義Listener的類名稱 例子: ~~~ <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> ~~~ ### <filter /> 用來聲明filter的相關設定 <filter-name></filter-name> 指定filter的名字 <filter-class></filter-class> 用來定義filter的類的名稱 <init-param></init-param> 用來定義參數,它有兩個子元素: <param-name></param-name> 用來指定參數的名稱, <param-value></param-value> 用來設定參數值 與<filter></filter>一起使用的是 <filter-mapping></filter-mapping> 用來定義filter所對應的URL,包含兩個子元素: <filter-name></filter-name> 指定filter的名稱 <url-pattern></url-pattern> 指定filter所對應的URL ?例子: ~~~ <!-- 解決中文亂碼問題 --> <filter> <filter-name>encodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> <init-param> <param-name>forceEncoding</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>encodingFilter</filter-name> <url-pattern>*.do</url-pattern> </filter-mapping> ~~~ ### <servlet />? 用來聲明一個servlet的數據,主要有以下子元素: <servlet-name></servlet-name> 指定servlet的名稱 <servlet-class></servlet-class> 指定servlet的類名稱 <jsp-file></jsp-file> 指定web站臺中的某個JSP網頁的完整路徑 <init-param></init-param> 用來定義參數 與<servlet></servlet>一起使用的是 <servlet-mapping></servlet-mapping> 用來定義servlet所對應的URL,包含兩個子元素: <servlet-name></servlet-name> 指定servlet的名稱 <url-pattern></url-pattern> 指定servlet所對應的URL 例子: ~~~ <servlet> <servlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> <!-- 用dubbo提供hessian服務 --> <servlet> <servlet-name>dubbo</servlet-name> <servlet-class>com.alibaba.dubbo.remoting.http.servlet.DispatcherServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>dubbo</servlet-name> <url-pattern>/hessian/*</url-pattern> </servlet-mapping> ~~~ ### 基本節點 1、<description/> 是對站點的描述 例子:<description>傳道、授業、解惑</description>? 2、<display-name/> 定義站點的名稱 例子:<display-name>我的站點</display-name> 3、<icon>? icon元素包含small-icon和large-icon兩個子元素,用來指定web站點中小圖標和大圖標的路徑。 <small-icon>/路徑/smallicon.gif</small-icon> small-icon元素應指向web站臺中某個小圖標的路徑,大小為16 X 16 pixel,但是圖象文件必須為GIF或JPEG格式,擴展名必須為:.gif或.jpg。 <large-icon>/路徑/largeicon-jpg</large-icon> large-icon元素應指向web站臺中某個大圖表路徑,大小為32 X 32 pixel,但是圖象文件必須為GIF或JPEG的格式,擴展名必須為: gif或jpg。 例子: <icon>? ?<small-icon>/images/small.gif</small-icon>? ?<large-icon>/images/large.gir</large-icon>? </icon> 4、 <distributable/> 是指定該站點是否可分布式處理 5、 <session-config/> 用來定義web站臺中的session參數 包含一個子元素: <session-timeout></session-timeout> 用來定義這個web站臺所有session的有效期限,單位為 分鐘 6、 <mime-mapping /> 定義某一個擴展名和某一個MIME Type做對應,它包含兩個子元素: <extension></extension> 擴展名的名稱 <mime-type></mime-type> MIME格式 例子: ~~~ <mime-mapping> <extension>csv</extension> <mime-type>application/octet-stream</mime-type> </mime-mapping> ~~~ 7、 <error-page> 通過錯誤碼來配置error-page ~~~ <error-page> <error-code>404</error-code> <location>/message.jsp</location> </error-page> ~~~ 通過異常類來配置error-page ~~~ <error-page> <exception-type>java.lang.NullException</exception-type> <location>/error.jsp</location> </error-page> ~~~ 8、 <welcome-file-list/> ~~~ <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.jsp</welcome-file> </welcome-file-list> ~~~ 9、 <resource-ref></resource-ref> 定義利用JNDI取得站臺可利用的資源 有五個子元素: <description></description> 資源說明 <rec-ref-name></rec-ref-name> 資源名稱 <res-type></res-type> 資源種類 <res-auth></res-auth> 資源經由Application或Container來許可 <res-sharing-scope></res-sharing-scope> 資源是否可以共享,有Shareable和Unshareable兩個值,默認為Shareable 比如,配置數據庫連接池就可在此配置 <resource-ref> <description>JNDI JDBC DataSource of shop</description> <res-ref-name>jdbc/sample_db</res-ref-name> <res-type>javax.sql.DataSource</res-type> <res-auth>Container</res-auth> </resource-ref>
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看