posts - 6,  comments - 3,  trackbacks - 0


              一般來說,對于整個應用的配置,為了不使用"硬編碼",應該使用ServletContext對象。
           而如果只有一個特定的Servlet需要設定的參數,其他Servlet不能訪問,那么一般要使用ServletConfig();
               PS:在使用ServletConfig對象的時候,在init()方法中,一定要用super類初始化ServletConfig對象。

          public void init(ServletConfig config) throws ServletException
              
          {
                  super.init(config);
                          
                          
          //TODO
                  }


            下面來逐個討論:

            一,  ServletContext對象
              <context-param>元素:設定Context起始參數
               web.xml中,您可以利用<context-param>元素來定義Context起始參數,它包含兩個子元素:

                n           <param-name>:定義Context起始參數名稱

                n           <param-value>:定義Context起始參數值

               以下是<context-param>元素的使用范例,在本例中筆者定義了兩個Context起始參數:

                n           driver_typeWeb應用程序欲使用的JDBC驅動程序名稱

                n           url:目標數據庫位置

          <web-app>

               
          <context-param>

                   
          <param-name>driver_type</param-name>

                   
          <param-value>oracle.jdbc.driver.OracleDriver</param-value>

               
          </context-param>

              
          <context-param>

                 
          <param-name>url</param-name>

                 
          <param-value>jdbc:oracle:thin:@IP:1521:SID</param-value>

             
          </context-param>

          </web-app>

            有兩種方式存取Context起始參數的方式:
              
                        表1 ServletContext接口中用來存取Context起始參數的方法

          方法名稱

          回傳類型

           

          getInitParameter()

          String

          取得某個Context起始參數值

          getInitParameterNames()

          java.util.Enumeration

          取得所有Context起始參數


             1.調用getServletConfig()方法取得ServletConfig對象,再利用ServletConfig接口定義的getServletContext()方法取得ServletContext對象。
                      
             ServletConfig config = getServletConfig();    
                        ServletContext context = config.getServletContext(); 

                        String driver_type = context.getInitParameter("drvier_type");
                        String url=context.getInitParameter("url");
                      
                       2. 直接調用getServletContext()方法取得ServletContext對象。
                           ServletContext context = getServletContext();
                          
                          //獲得配置的參數
                           String driver_type = context.getInitParameter("drvier_type");
                           String url=context.getInitParameter("url");
                        //獲得當前WebApp的路徑
                         String path=context.getRealPath("/");

           二,   ServletConfig對象
              <init-param>元素:設定init起始參數
                   web.xml中,您可以利用<init-param>元素來定義Config起始參數,它包含兩個子元素:

                n           <init-name>:定義Config起始參數名稱

                n           <init-value>:定義Config起始參數值

               以下是<init-param>元素的使用范例,在本例中筆者定義了兩個Config起始參數:

                n           driver_typeWeb應用程序欲使用的JDBC驅動程序名稱

                n           url:目標數據庫位置    

          <web-app>
              
          <servlet>
                  
          <servlet-name>testServlet</servlet-name>
                  
          <servlet-class>com.simon.test.servlet.initparam.testServlet</servlet-class>
                  
                  
          <init-param>
                       <param-name>driver_type</param-name>
                       
          <param-value>oracle.jdbc.driver.OracleDriver</param-value>
              </init-param>

                    <init-param>
                      
          <param-name>url</param-name>
                      
          <param-value>jdbc:oracle:thin:@IP:1521:SID</param-value>
                  
          </init-param>
                 
              
              
          <servlet-mapping>
                  
          <servlet-name>testServlet</servlet-name>
                  
          <url-pattern>/testServlet</url-pattern>
              
          </servlet-mapping>
          </web-app>

          在init()方法中,應該:
            

          public void init(ServletConfig config) throws ServletException
              
          {
                          
          //必須要繼承super類的init()方法
                  super.init(config);
                          
                  String filename
          =getServletConfig().getInitParameter("config-file");
                  
                   
          //TODO
               }


           

          posted on 2005-08-16 12:01 Java&Inter 閱讀(1786) 評論(0)  編輯  收藏 所屬分類: J2EE技術

          <2025年5月>
          27282930123
          45678910
          11121314151617
          18192021222324
          25262728293031
          1234567

          常用鏈接

          留言簿(1)

          隨筆分類(3)

          隨筆檔案(6)

          文章分類(24)

          文章檔案(22)

          收藏夾(2)

          經常去的Blog

          • Eclipse/GEF
          • 關于Eclipse插件(plugins)開發的心得,主要包括:SWT/JFACE/GEF/EMF/RCP

          搜索

          •  

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 黄陵县| 土默特右旗| 谢通门县| 河源市| 得荣县| 阿合奇县| 新竹市| 荔波县| 涟源市| 贵定县| 白沙| 乃东县| 都兰县| 东乡县| 年辖:市辖区| 五指山市| 陇西县| 马边| 澄迈县| 泽普县| 南郑县| 深州市| 屏南县| 平陆县| 海盐县| 特克斯县| 宁晋县| 八宿县| 舞钢市| 滨州市| 新绛县| 大港区| 兴宁市| 阜宁县| 洛川县| 大同县| 滕州市| 乡城县| 阿拉善右旗| 西乌珠穆沁旗| 高碑店市|