188bet官网2018

稍后讲到项目目录结构如下:代码范例:使用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,这样虽然可以实现,但是,这样损坏了我们编代码的设计原则,就是层之间不能有交织在一起的东西。走进小店,第一眼看上去有点日式风格,环境整洁清新,餐具也极具文艺范,墙体更是颠覆地用砖头直接堆砌。数十名国内旅游业专家学者齐聚南昌,围绕“中国旅游度假4.0时代已经全面到来”进行深入探讨。站在商铺门口,看着一地狼藉,张先生低落地说,我现在只想哭。选择一个好的命名命名有两个目标:清晰:你要知道该命名与什么有关精确:你要知道该命名与什么无关当一个命名完成上面两个目标之后,其余的字符就是多余的了。

(新课标)2018高考化学一轮复习夯基提能作业(打包35套)

上传时间:2017-07-28 年级: 高三 费用:45E币 | 充值
资源简介