属性文件db.properties
driver=com.microsoft.sqlserver.jdbc.SQLServerDriver url=jdbc:sqlserver://localhost:1433;DatabaseName=addressBook username=sa password=accp
?
读取属性文件
public class ReadProperties extends Properties {
?// 静态的私有的静态变量 ?private static ReadProperties readPro = null;
?// 私有构造函数 ?private ReadProperties() { ??try { ???InputStream is = getClass().getResourceAsStream("/db.properties"); ???load(is); ??} catch (IOException e) { ???e.printStackTrace(); ??} ?}
?// 获取本类实例的方法(同步锁,只能创建一个实例) ?public static? ReadProperties getInstance() { ??if (readPro == null) { ???readPro=new ReadProperties(); ??? ??} ??return readPro; ?} }
Servlet控制器
public void doPost(HttpServletRequest request,HttpServletResponse response) ???throws ServletException,IOException { ??try { ???// 获取数据完整路径 ???String path = request.getServletPath(); ???// 截取完整路径 ???path = path.substring(1,path.indexOf(".")); ???// 得到数据类型(类) ???String straction = pro.getProperty(path); ???// 加载数据(类) ???Class classaction = Class.forName(straction); ???// 实例化(类)对象 ???IAction action = (IAction) classaction.newInstance(); ???// 获取目的地 ???String dest = action.execute(request,response); ???// 进行转发 ???request.getRequestDispatcher(dest).forward(request,response); ??} catch (ClassNotFoundException e) { ???e.printStackTrace(); ??} catch (InstantiationException e) { ???e.printStackTrace(); ??} catch (IllegalAccessException e) { ???e.printStackTrace(); ??} ?}
@Override?public void init(ServletConfig config) throws ServletException {??InputStream is = this.getClass().getResourceAsStream(????config.getInitParameter("pro"));??try {???pro.load(is);??} catch (IOException e) {???e.printStackTrace();??}?}
(编辑:李大同)
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!
|