0734w-月下竹音

          0734是來自家鄉的聲音

          常用鏈接

          統計

          最新評論

          用java從properties配置文件里面讀數據

          第一步:配置文件
          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:當應用起動時加載要讀取的數據放入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:加載和設置數據</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);
                    //需要讀取的數據每一個數據都要從這里 定義       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;            
                  }
                  
              }
              
          }

          第三步,在應用利用:
          在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)  編輯  收藏


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


          網站導航:
           
          主站蜘蛛池模板: 扬中市| 长葛市| 宁安市| 忻州市| 容城县| 噶尔县| 邯郸市| 孝义市| 涪陵区| 扎兰屯市| 漯河市| 大英县| 彭州市| 福海县| 南木林县| 怀安县| 阳高县| 鄂伦春自治旗| 白银市| 若羌县| 通江县| 蒙城县| 阳高县| 日喀则市| 灵川县| 彰武县| 筠连县| 荣成市| 平遥县| 河南省| 昌都县| 潞城市| 贵溪市| 开阳县| 玛纳斯县| 柳林县| 托克托县| 霍邱县| 茌平县| 西华县| 济南市|