@WebServlet("/testEvery")public class testEveryServlet extendsHttpServlet {private static final long serialVersionUID = 1L;/***@seeHttpServlet#HttpServlet()*/

publictestEveryServlet() {super();//TODO Auto-generated constructor stub

}/*** 访问url:http://localhost:8080/demoProj/testEveryServlet*/

/***@seeHttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)*/

protected void doGet(HttpServletRequest request, HttpServletResponse response) throwsServletException, IOException {//TODO Auto-generated method stub

response.getWriter().append("Served at: ").append(request.getContextPath());

String path= request.getSession().getServletContext().getRealPath("/config/sysconfig.properties");

System.out.println("doGet读取到的/WEB-INF/config/sysconfig.properties:path:"+path);

String url= request.getSession().getServletContext().getRealPath("/WEB-INF/config/config.properties");

System.out.println("doGet读取到的/WEB-INF/config/config.properties:url:"+url);/*** 结果:

* doGet:path:D:\tomcat7\wtpwebapps\demoProj\config\sysconfig.properties

* doGet:url:D:\tomcat7\wtpwebapps\demoProj\WEB-INF\config\config.properties*/

//只能获取src下面的

InputStream in = this.getServletContext().getResourceAsStream("/WEB-INF/classes/config/test.properties");

Properties prop= new Properties(); //map

prop.load(in);

String url1= prop.getProperty("url");

System.out.println("获取到的url1:"+url1);//获取到的url1:www.baidu.com//不可获取

InputStream in2 = this.getServletContext().getResourceAsStream("/WEB-INF/config.properties");

Properties prop2= new Properties(); //map

prop.load(in2);

String url2= prop2.getProperty("url");

System.out.println("获取到的url2:"+url2);//获取到的url2:null//不可获取

InputStream in3 = this.getServletContext().getResourceAsStream("/webcontent.properties");

Properties prop3= new Properties(); //map

prop.load(in3);

String url3= prop3.getProperty("url");

System.out.println("获取到的url3:"+url3);//获取到的url3:null//不可获取

InputStream in4 = this.getServletContext().getResourceAsStream("/config/wcc.properties");

Properties prop4= new Properties(); //map

prop.load(in4);

String url4= prop4.getProperty("url");

System.out.println("获取到的url4:"+url4);//获取到的url4:null//读取src下config包中的testJava.java//InputStream in = ReadFile.class.getResourceAsStream("/config/testJava.java");//in为null//byte[] a=new byte[100];//in.read(a, 0, 900);//System.out.println("读取src下config包中的testJava.java的输入流in的内容toString:"+in.toString());//System.out.println("读取到的a:"+a);

String fileName3 = ReadFile.class.getResource("/config/test.properties").getFile();

System.out.println("读取src下config包中的test.properties:"+fileName3);//输出:读取src下config包中的test.properties:/D:/tomcat7/wtpwebapps/demoProj/WEB-INF/classes/config/test.properties//in.close();//读取src下 基名为myproperties的properties文件,获取其中name配置值

String value = ResourceBundle.getBundle("myproperties").getString("name");

System.out.println("获取到的myproperties.properties的值value:"+value);//输出:获取到的myproperties.properties的值value:myname//读取src下myproperties.properties

InputStream in1 = ReadFile.class.getResourceAsStream("/myproperties.properties");

Properties properties= newProperties();

properties.load(in1);

String value2= properties.getProperty("name"); //获得name属性

System.out.println("获取到的myproperties.properties的值value2:"+value2);//获取到的myproperties.properties的值value2:myname//读取src下的

String sensitiveWordsServerPath1 = SysConfig.getSysParam("sensitiveWords_server_path1");

System.out.println("获取的sensitiveWordsServerPath1:"+sensitiveWordsServerPath1);//获取的sensitiveWordsServerPath1:/datacms/htdocs/html/cctv/sensitiveWords/sws.xlsx//读取src下的

String pp = prop("sensitiveWords_server_path1");

System.out.println("pp:"+pp);//pp:/datacms/htdocs/html/cctv/sensitiveWords/sws.xlsx

}publicString prop(String url){

InputStream inputStream= this.getClass().getClassLoader().getResourceAsStream("config/sysconfig.properties");

Properties p= newProperties();try{

p.load(inputStream);

}catch(Exception e) {

e.printStackTrace();

}

System.out.println("p:"+p);returnp.getProperty(url);

}/***@seeHttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)*/

protected void doPost(HttpServletRequest request, HttpServletResponse response) throwsServletException, IOException {//TODO Auto-generated method stub

doGet(request, response);

String path= request.getSession().getServletContext().getRealPath("/config/sysconfig.properties");

System.out.println("doPost:path:"+path);

}

}

java获取webcontent_JAVA Web项目获取src和WebContent目录下的配置文件相关推荐

  1. java 获取webcontent目录_java web项目获取src和WebContent目录下的配置文件

    1.首先上目录结构: 2.部署到tomcat,在servlet中测试,测试代码: package test; import java.io.IOException; import java.io.In ...

  2. java resources 目录_Maven项目中读取src/main/resources目录下的配置文件的方法

    在Maven项目的开发中,当需要读取src/下的配置文件时,该怎么做? 我们假设Resources下有一个文件名为kafka.properties的配置文件(为什么用kafka.properties, ...

  3. idea中src/main/resources目录下的applicationContext.xml文件访问src/main/webap目录下的配置文件

    如图所示,想要在src/main/resources目录下的applicationContext.xml文件中访问src/main/webap目录下的db.properties,需要两步: appli ...

  4. 首次创建maven web项目,没有生成WebContent目录解决方案

    已选Dynamic Web Module,但是并没有生成WebContent目录. 试了很多次,都没有,要崩溃了! 解决方案如下: 再次进入 Properties -> Project Face ...

  5. java application 路径_java项目获取根路径(web项目和application项目的区分)

    Java项目中经常要读取配置文件,涉及到读取配置文件的地方,就会要读定位文件的路径.因此,在项目如何正确获取文件路径尤为关键. 根据不同的java项目,在获取文件路径时候有一些 小区别 测试环境:E: ...

  6. Weblogic部署web项目获取项目根目录为null

    写在前面 图片上传功能, web项目部署在本地Tomcat上并没有问题, 但是打成war包部署到Linux服务器Weblogic下却出现如题问题, 导致图片上传失败. 问题代码 String real ...

  7. java gradle 资源访问_java在gradle工程访问src/test/resources目录下的资源配置文件

    package com.jiepu; import java.io.File; import java.net.URISyntaxException; import java.util.Map; im ...

  8. java在gradle工程访问src/test/resources目录下的资源配置文件

    package com.jiepu; import java.io.File; import java.net.URISyntaxException; import java.util.Map; im ...

  9. android 获取图片上某一个文字位置_android 获取手机中的所有图片或某一目录下的图片方法...

    获取手机中的所有图片,并过滤获取某一目录下的图片.(注释掉的代码可以按照目录分组) private void getAllPhotoInfo() { new Thread(new Runnable() ...

最新文章

  1. linux python fcntl模块 程序加锁 简介
  2. 搜索引擎语法简介 google shodan censys zoomeye fofa dnsdb
  3. TSM对Oracle数据库备份脚本
  4. zeppelin报错master is set as yarn, but spark.submit.deployMode is not specified
  5. P7276-送给好友的礼物【dp】
  6. 大数据入门:各种大数据技术的介绍
  7. 数学思想 —— 推广和泛化
  8. 最新编程语言排行榜出炉:Swift和Kotlin好像开始凉了
  9. 【linux】-bash: vim: 未找到命令
  10. clock多少合适 core_修改Core Clock(核心频率)和Memory Clock(显存频率)
  11. python世界你好的输出_Python语句print( ;世界,你好”)的输出是________。
  12. java飞翔的小鸟游戏实验报告,BirdGame 飞翔的小鸟游戏源码,仿照 编写的java版本 ,所有的 功能完整。 s 249万源代码下载- www.pudn.com...
  13. VDA6.3认证辅导,VDA6.3认证以保证汽车零部件生产过程中的质量保证
  14. 蓝湖能导入html文件么,axure怎么导入蓝湖
  15. VMware中性能分配的问题
  16. [ML-03] Matplotlib-3
  17. AI实验-七种动物识别系统
  18. 用递归和非递归求斐波那契数列
  19. 计算机大赛志愿者心得体会,志愿者的心得体会
  20. bzoj4774 修路

热门文章

  1. centos7搭建CA服务器颁发ssl证书
  2. [Linux] 老是记不住的Mount用法
  3. RLE算法机制、缺点及哈夫曼算法和莫尔斯编码
  4. {小结}GDOI2016骗分记
  5. 换华为路由器后,不能连接pptp
  6. 考证的光盘怎么复制到电脑上
  7. 阿里云牵手英伟达推出初创加速计划,算力触手可得
  8. 性能工具gperftools使用说明
  9. Unity动态添加tag
  10. 区块链应用:椭圆曲线数字签名算法ECDSA