码迷,mamicode.com
首页 > 其他好文 > 详细

循循渐进02篇 maven Properties 读取资源文件

时间:2019-11-24 00:02:04      阅读:80      评论:0      收藏:0      [点我收藏+]

标签:ring   rect   cto   except   include   需要   main   extend   extends   

Properties

<!--在pom build中配置resources,来防止我们资源导出失败的问题-->
<build>
<resources>
<resource>
<directory>src/main/resouces</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>true</filtering>
</resource>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>true</filtering>
</resource>
</resources>
</build>

  1.在java目录下properties

  2.在resources目录下新建properties

发现:都被打包到了同一个路径下:classes 我们俗称这个路径为classpath

思路:需要一个文件流:

   properties:

uersname=asdas
password=1asdasd

class:
public class Servletdome01 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
InputStream is = this.getServletContext().getResourceAsStream("/WEB-INF/classes/com/hao/selever02/aa.properties");
Properties properties = new Properties();
properties.load(is);
String uersname = properties.getProperty("uersname");
String password = properties.getProperty("password");
resp.getWriter().print(uersname+password);


}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req, resp);
}
}

 

循循渐进02篇 maven Properties 读取资源文件

标签:ring   rect   cto   except   include   需要   main   extend   extends   

原文地址:https://www.cnblogs.com/huhao2000/p/11920711.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!