必威电子游戏

该网站发布各技术领域的高技术含量内容,例如大数据,云计算,Java,物联网,web开发等。稍后讲到项目目录结构如下:代码范例:使用servletContext读取资源文件packagegacl.servlet.study;?importjava.io.FileInputStream;importjava.io.FileNotFoundException;importjava.io.IOException;importjava.io.InputStream;importjava.text.MessageFormat;importjava.util.Properties;importjavax.servlet.ServletException;importjavax.servlet.http.HttpServlet;importjavax.servlet.http.HttpServletRequest;importjavax.servlet.http.HttpServletResponse;?/**?*使用servletContext读取资源文件?*??*@authorgacl?*??*/publicclassServletContextDemo6extendsHttpServlet{???publicvoiddoGet(HttpServletRequestrequest,HttpServletResponseresponse)??????throwsServletException,IOException{?????/**?????*response.setContentType("text/html;charset=UTF-8");目的是控制浏览器用UTF-8进行解码;?????*这样就不会出现中文乱码了?????*/????response.setHeader("content-type","text/html;charset=UTF-8");????readSrcDirPropCfgFile(response);//读取src目录下的properties配置文件????response.getWriter().println("");????readWebRootDirPropCfgFile(response);//读取WebRoot目录下的properties配置文件????response.getWriter().println("");????readPropCfgFile(response);//读取src目录下的db.config包中的db3.properties配置文件????response.getWriter().println("");????readPropCfgFile2(response);//读取src目录下的gacl.servlet.study包中的db4.properties配置文件???}???/**???*读取src目录下的gacl.servlet.study包中的db4.properties配置文件???*@paramresponse???*@throwsIOException???*/??privatevoidreadPropCfgFile2(HttpServletResponseresponse)??????throwsIOException{????InputStreamin=this.getServletContext().getResourceAsStream("/WEB-INF/classes/gacl/servlet/study/db4.properties");????Propertiesprop=newProperties();????prop.load(in);????Stringdriver=prop.getProperty("driver");????Stringurl=prop.getProperty("url");????Stringusername=prop.getProperty("username");????Stringpassword=prop.getProperty("password");????response.getWriter().println("读取src目录下的gacl.servlet.study包中的db4.properties配置文件:");????response.getWriter().println(????????MessageFormat.format(????????????"driver={0},url={1},username={2},password={3}",?????????????driver,url,username,password));??}???/**???*读取src目录下的db.config包中的db3.properties配置文件???*@paramresponse???*@throwsFileNotFoundException???*@throwsIOException???*/??privatevoidreadPropCfgFile(HttpServletResponseresponse)??????throwsFileNotFoundException,IOException{????//通过ServletContext获取web资源的绝对路径????Stringpath=this.getServletContext().getRealPath("/WEB-INF/classes/db/config/db3.properties");????InputStreamin=newFileInputStream(path);????Propertiesprop=newProperties();????prop.load(in);????Stringdriver=prop.getProperty("driver");????Stringurl=prop.getProperty("url");????Stringusername=prop.getProperty("username");????Stringpassword=prop.getProperty("password");????response.getWriter().println("读取src目录下的db.config包中的db3.properties配置文件:");????response.getWriter().println(????????MessageFormat.format(????????????"driver={0},url={1},username={2},password={3}",?????????????driver,url,username,password));??}???/**???*通过ServletContext对象读取WebRoot目录下的properties配置文件???*@paramresponse???*@throwsIOException???*/??privatevoidreadWebRootDirPropCfgFile(HttpServletResponseresponse)??????throwsIOException{????/**?????*通过ServletContext对象读取WebRoot目录下的properties配置文件?????*“/”代表的是项目根目录?????*/????InputStreamin=this.getServletContext().getResourceAsStream("/db2.properties");????Propertiesprop=newProperties();????prop.load(in);????Stringdriver=prop.getProperty("driver");????Stringurl=prop.getProperty("url");????Stringusername=prop.getProperty("username");????Stringpassword=prop.getProperty("password");????response.getWriter().println("读取WebRoot目录下的db2.properties配置文件:");????response.getWriter().print(????????MessageFormat.format(????????????"driver={0},url={1},username={2},password={3}",?????????????driver,url,username,password));??}???/**???*通过ServletContext对象读取src目录下的properties配置文件???*@paramresponse???*@throwsIOException???*/??privatevoidreadSrcDirPropCfgFile(HttpServletResponseresponse)throwsIOException{????/**?????*通过ServletContext对象读取src目录下的db1.properties配置文件?????*/????InputStreamin=this.getServletContext().getResourceAsStream("/WEB-INF/classes/db1.properties");????Propertiesprop=newProperties();????prop.load(in);????Stringdriver=prop.getProperty("driver");????Stringurl=prop.getProperty("url");????Stringusername=prop.getProperty("username");????Stringpassword=prop.getProperty("password");????response.getWriter().println("读取src目录下的db1.properties配置文件:");????response.getWriter().println(????????MessageFormat.format(????????????"driver={0},url={1},username={2},password={3}",?????????????driver,url,username,password));??}???publicvoiddoPost(HttpServletRequestrequest,HttpServletResponseresponse)??????throwsServletException,IOException{????this.doGet(request,response);??}?}运行结果如下:使用类装载器读取资源文件我们在非servlet中读取资源文件时(比如在数据库的dao层读取配置文件),采用类装载器classLoader,你可以先采用servlet服务先读取,然后在把servlet传递给dao,这样虽然可以实现,但是,这样损坏了我们编代码的设计原则,就是层之间不能有交织在一起的东西。Java-levelDeadlocks真正的Java-leveldeadlocks是不太常见的,它同样可以极大程度地影响应用程序的性能和稳定性。
betway888官网 欢迎您:
加入收藏设为首页请您留言
betway888官网

团队组织

版权所有:betway888官网 更新时间:2018-04-10 07:58:55

Copyright©1998 - 2013 Tencent. All Rights Reserved

备案/许可证编号为 betway赞助英超西汉姆联队

回到顶部