posts - 6,  comments - 3,  trackbacks - 0


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

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


            下面來逐個討論:

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

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

                n           <param-value>:定義Context起始參數(shù)值

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

                n           driver_typeWeb應(yīng)用程序欲使用的JDBC驅(qū)動程序名稱

                n           url:目標(biāo)數(shù)據(jù)庫位置

          <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起始參數(shù)的方式:
              
                        表1 ServletContext接口中用來存取Context起始參數(shù)的方法

          方法名稱

          回傳類型

           

          getInitParameter()

          String

          取得某個Context起始參數(shù)值

          getInitParameterNames()

          java.util.Enumeration

          取得所有Context起始參數(shù)


             1.調(diào)用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. 直接調(diào)用getServletContext()方法取得ServletContext對象。
                           ServletContext context = getServletContext();
                          
                          //獲得配置的參數(shù)
                           String driver_type = context.getInitParameter("drvier_type");
                           String url=context.getInitParameter("url");
                        //獲得當(dāng)前WebApp的路徑
                         String path=context.getRealPath("/");

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

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

                n           <init-value>:定義Config起始參數(shù)值

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

                n           driver_typeWeb應(yīng)用程序欲使用的JDBC驅(qū)動程序名稱

                n           url:目標(biāo)數(shù)據(jù)庫位置    

          <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()方法中,應(yīng)該:
            

          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 閱讀(1793) 評論(0)  編輯  收藏 所屬分類: J2EE技術(shù)

          <2025年7月>
          293012345
          6789101112
          13141516171819
          20212223242526
          272829303112
          3456789

          常用鏈接

          留言簿(1)

          隨筆分類(3)

          隨筆檔案(6)

          文章分類(24)

          文章檔案(22)

          收藏夾(2)

          經(jīng)常去的Blog

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

          搜索

          •  

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 札达县| 曲靖市| 靖州| 方正县| 临清市| 榆社县| 舞钢市| 潍坊市| 刚察县| 靖江市| 衡南县| 称多县| 和田市| 五家渠市| 溧水县| 哈尔滨市| 阳山县| 莒南县| 灌云县| 泗阳县| 安图县| 广宗县| 米脂县| 冷水江市| 南澳县| 迁安市| 乐至县| 贵溪市| 大埔区| 无棣县| 喀喇沁旗| 绥宁县| 北辰区| 邵东县| 芷江| 江西省| 鲁山县| 化州市| 荥经县| 安岳县| 布拖县|