把文件路径信息写到环境变量里,然后JavaBean再在环境变量里取得。

解决方案 »

  1.   

    不是,
    我的意思是 : 取得web.xml 文件的內容,像 ServletContex context= getServletContext()
    String str = context.getInitParameter("param");但這是 HttpServlet 的方法 .
      

  2.   

    大家在開發軟件時 ,都把參數放在哪裡呢,
    比如 數據庫連結參數 等,不是在 web.xml 中麼?
      

  3.   

    可作为条目(即Entry)放置于property文件中,运行时读取。
      

  4.   

    把参数放在config.ini文件,bean里读文件,放到properties中待用。
      

  5.   

    具體怎麼做呢?
    我剛接觸Jsp ,很生疏阿
      

  6.   

    我的用法
    配置文件:/WEB-INF/classes/config.confserverURL=jdbc:mysql://localhost:3306/yonghang
    userName=root2
    passWord=123456
    conMin=5
    conMax=30
    conTimeOut=3
    driver=org.gjt.mm.mysql.Driver
    LogFile=c:\\log.log
    ErrLogFile=c:\\logerr.log
    sysLog=c:\\syslog.log读写的类1:PropManager.java (/WEB-INF/classes/PropManager.class)import java.io.*;
    import java.util.*;public class PropManager { private Properties Props;
    private String path="/config.conf";
            public PropManager(String strConfig)
            {
                if(strConfig==null || strConfig.equals(""))
                    init(path);
                else
                    path=strConfig;
                    init(strConfig);
            }
            public PropManager()
            {
                init(path);
            }
    public String getProperty(String name)
    {
    try
    {
    String property=Props.getProperty(name);
    if(property==null)
    {
    property="NULL";
    }
    return property;
    }catch(Exception e)
    {
    System.err.println("CONFIG FILE ERROR! ");
    return null;
    }
    }
    private void init(String paths)
    {
    InputStream in=null;
    try
    {
    //System.out.println(paths);
                            in=getClass().getResourceAsStream(paths);
                            //System.out.println(in);
    }catch(Exception e)
    {
    System.out.println(paths);
                            System.err.println("Config.File ERROR! NO FOUND!");
    }
    try
    {
    Props=new Properties();
    Props.load(in);
    System.err.println("PropManager Load Successed!");
    }catch(IOException e)
    {
    System.err.println("FileInputStream in ERROR!");
    }
    }
    }
    读写的类2:PropertyManager.java (/WEB-INF/classes/PropertyManager.class)import PropManager;public class PropertyManager
    {
    private static PropertyManager propertyManager=null;
            private static PropManager propManager=null;
            private static String path="/config.conf";
    private static boolean staus=false;
    public PropertyManager()
    {
    init();
    }
    public static String getProperty(String name)
    {
                    checkStaus();
                    try
    {
                        return propManager.getProperty(name);
    }catch(Exception e)
    {
                        System.out.println(" DBPool.PropertyManager.getProperty() ERROR!");
                        return null;
    }
    }
    //内部方法
            private static void checkStaus()
            {
                if(!staus || propertyManager==null || propManager==null)
                {
                    propertyManager=new PropertyManager();
                }
            }
    private void init()
    {
    try
    {
    propManager=new PropManager(path);
    System.err.println("PropertyManager Init Successed!");
    staus=true;
    }catch(Exception e)
    {
    System.err.println("PropManager in ERROR!");
                            staus=false;
    }
    }
            public static void setPath(String path_)
            {
                path=path_;
            }
    }
    在JSP或SERVLET中使用
    PropertyManager.getProperty("serverURL");
    PropertyManager.getProperty("userName");
    PropertyManager.getProperty("passWord");
    来得到相关的参数
      

  7.   

    path="/config.conf"
    是以 classes 為根目錄的嗎?
    class.getResourceAsStream(paths);
    能否訪問 操作系統中的其他文件呢
      

  8.   

    /config.conf
    是以classes为根目录
    在JSP或SERVLET中程序会找到的只有CLASSPATH中的类文件可以使用
    class.getResourceAsStream(paths);
    来获得文件的路径。其它的不行