0734w-月下竹音

          0734是來自家鄉(xiāng)的聲音

          常用鏈接

          統(tǒng)計

          最新評論

          用java從properties配置文件里面讀數(shù)據(jù)

          第一步:配置文件
          1. web.xml
          <servlet>
              <servlet-name>InitServlet</servlet-name>
              <servlet-class>com.0734w.util.InitServlet</servlet-class>
              <init-param>
                  <param-name>config</param-name>
                  <param-value>/WEB-INF/config.properties</param-value>
              </init-param>
              <load-on-startup>1</load-on-startup>
            </servlet>
           
          2.config.properties
          假若在config.properties配置如下
          app1.name=dog
          app2.name=pig

          3.在Constants.java中定義
          public final static String APP1NAME = "app1.name";   
          public final static String APP2NAME = "app2.name";   

          第二步:定義InitServlet

          package com.0734w.util;

          import java.io.IOException;
          import java.io.InputStream;
          import java.util.PropertyResourceBundle;

          import javax.servlet.ServletContext;
          import javax.servlet.ServletException;
          import javax.servlet.http.HttpServlet;

          import org.apache.log4j.Logger;


          import com.cpic.adapter.adapterapp.constant.Constants;

          /**
           * <p>Title:InitServlet.java</p>
           * <p>Description:當(dāng)應(yīng)用起動時加載要讀取的數(shù)據(jù)放入context中</p>
           * @author spark 2005-12-26
           * @version 1.0
           */
          public class InitServlet extends HttpServlet {
              private Logger logger;

              public void init() throws ServletException{
                  super.init();
                  ServletContext context = getServletContext();
                  context.setAttribute(Constants.ADAPTER_INIT_STATUS,Boolean.FALSE);        
                  //initialize proxy configuration
                  initAdapter();
                  context.setAttribute(Constants.ADAPTER_INIT_STATUS,Boolean.TRUE);
                  logger.info("initAdapter initialized successfully");
              }

              
              /**
               * <p>Description:加載和設(shè)置數(shù)據(jù)</p>
               *  @throws ServletException
               *  spark 2005-12-26
               */
              private void initAdapter() throws ServletException{
                  ServletContext context = getServletContext();
                  String configFile = getInitParameter("config");
                  if (configFile == null) {
                      String errMsg="Initialize Adapter configuration  config file is not set in web.xml.";
                      logger.error(errMsg);
                      ServletException e = new ServletException(errMsg);
                      throw e;            
                  }
                  
                  InputStream in;
                  PropertyResourceBundle configBundle;
                  try {
                      in = this.getServletContext().getResourceAsStream(configFile);
                      configBundle = new PropertyResourceBundle(in);
                    //需要讀取的數(shù)據(jù)每一個數(shù)據(jù)都要從這里 定義       context.setAttribute(Constants.APP1NAME,configBundle.getString(Constants.APP1NAME));
                      context.setAttribute(Constants.
          APP2NAME,configBundle.getString(Constants..APP2NAME));

                  } catch (IOException e) {
                      String errMsg = "Initialize adapter config.properties failed.";
                      logger.error(errMsg);
                      ServletException e1 = new ServletException(errMsg);
                      throw e1;            
                  }
                   catch (Exception e) {
                      String errMsg = "Initialize adapter config.properties failed.";
                      logger.error(errMsg);
                      ServletException e1 = new ServletException(errMsg);
                      throw e1;            
                  }
                  
              }
              
          }

          第三步,在應(yīng)用利用:
          在servlet中如下:
          ServletContext context = getServletContext();
          ( String) context.getAttribute(Constants.APP1NAME)
          在STRUTS的ACTION中如下:
          ServletContext context = request.getSession().getServletContext();
          (String) context.getAttribute(Constants.APP1NAME);

          posted on 2006-01-18 16:46 sparkwu 閱讀(825) 評論(0)  編輯  收藏


          只有注冊用戶登錄后才能發(fā)表評論。


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 栾川县| 利川市| 佛冈县| 信宜市| 青州市| 青田县| 息烽县| 青河县| 廊坊市| 普宁市| 都匀市| 丹棱县| 潞西市| 收藏| 山西省| 九台市| 屏东县| 景洪市| 潞西市| 黔江区| 静安区| 广西| 麻江县| 平阳县| 新竹市| 武威市| 吉安县| 陵水| 上饶市| 琼结县| 桐庐县| 合水县| 翁牛特旗| 牟定县| 邢台县| 东平县| 增城市| 阳朔县| 外汇| 广灵县| 阿拉善盟|