<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    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 閱讀(1785) 評論(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

    搜索

    •  

    最新評論

    閱讀排行榜

    評論排行榜

    主站蜘蛛池模板: 国产黄在线观看免费观看不卡| 人妻在线日韩免费视频| 亚洲高清成人一区二区三区| 成人无码精品1区2区3区免费看| 亚洲av日韩av无码| 成年女人18级毛片毛片免费观看| 在线观看亚洲电影| 亚洲AV无码专区电影在线观看| 91在线品视觉盛宴免费| 深夜福利在线视频免费| 亚洲韩国在线一卡二卡| 免费精品一区二区三区在线观看| 国产免费牲交视频免费播放| 亚洲综合久久精品无码色欲| 亚洲色欲色欲www在线丝| 国产h视频在线观看免费| av电影在线免费看| 亚洲综合欧美色五月俺也去| 亚洲国产另类久久久精品黑人| 猫咪社区免费资源在线观看 | 国产精品极品美女自在线观看免费 | av午夜福利一片免费看久久| 亚洲激情黄色小说| 区三区激情福利综合中文字幕在线一区亚洲视频1 | 四虎影视成人永久免费观看视频| 亚洲精品无码久久久久A片苍井空| 亚洲成A人片在线观看无码不卡| 国产乱子伦精品免费女| 狼群影院在线观看免费观看直播| EEUSS影院WWW在线观看免费| 亚洲AV成人一区二区三区观看| 夜夜亚洲天天久久| 国产v亚洲v天堂无码网站| 日本一道综合久久aⅴ免费| 18以下岁毛片在免费播放| 国产福利在线观看永久免费| 亚洲av日韩综合一区二区三区| 亚洲国产精品日韩在线| 久久亚洲国产成人亚| 国产精品亚洲精品日韩已方| 国产亚洲福利一区二区免费看|