Java_web项目的资源文件一般有两种:

一种是存放数据之间有联系的文件,使用xml文件

另一种是存放数据之间没有联系的文件,使用properties文件

这里我们对properties文件读写做示范:

1、首先在eclipse的src目录下创建一个资源文件properties

我们可以看到没有创建file文件的选项,那就选Other

然后点finish就可以了。

文件里面随便放点数据:

url=127.0.0.1
name=root
password=root

之后在src的test包里面创建一个ServletContextDemo2.Java

文件内容如下:

package test;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties; import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; /**
* Servlet implementation class ServletContextDemo2
*/
@WebServlet("/ServletContextDemo2") //注意有了这个就不需要往web.xml文件里面添加路径映射
public class ServletContextDemo2 extends HttpServlet {
private static final long serialVersionUID = 1L; /**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
//路径上第一个斜杠/相对于项目day02的相对路径
InputStream in = this.getServletContext().getResourceAsStream("/build/classes/db.properties");
//FileInputStream in = new FileInputStream("classes/db.properties");
Properties pro = new Properties();
pro.load(in); //这个文件的值会以map的形式存放 String url = pro.getProperty("url");
String name = pro.getProperty("name");
String password = pro.getProperty("password"); System.out.println(url);
System.out.println(name);
System.out.println(password);
} /**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
} }

要特别注意一下这一条语句:

InputStream in = this.getServletContext().getResourceAsStream("/build/classes/db.properties");

这个文件路径可不是你看到的路径,什么意思呢?

我们在src目录下创建的db.properties,但是我们的项目发布之后是没有src这个目录的。所以你可不能把路径写成src/db.properties

那么我们怎么看自己项目发布之后的路径呢?

打开项目属性:

到这里大家应该知道了为什么路径要写成

/build/classes/db.properties  东西了吧!

如果你想要在一个Java包里面创建资源文件,之后访问。那就把路径改成

/build/classes/包/db.properties

注意:

如果上面的不行,那就把文件地址换成

/WEB-INF/classes/包/db.properties

也就是把build都换成WEB-INF

不知道怎么回事,之前build使用谷歌浏览器可以,但是又不行了(我就搞了搞其他东西,又回来试了试,就不行了)。

换成WEB-INF之后,你使用谷歌浏览器访问可能会抛出

java.lang.NullPointerException
java.util.Properties$LineReader.readLine(Properties.java:434)
java.util.Properties.load0(Properties.java:353)
java.util.Properties.load(Properties.java:341)
test.ServletContextDemo2.doGet(ServletContextDemo2.java:86)
javax.servlet.http.HttpServlet.service(HttpServlet.java:634)
javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)

但是换成IE浏览器(window10自带)可以。我算是服了

不知道谷歌浏览器怎么回事,一脸懵

好像是不能在原网页上刷新,你可以试一试重新打开一个页面

---------------------------------------------------------分隔------------------------------------------------

你可以通过类装载器方式读取资源文件,这样是没有问题的,IE、谷歌都可以 ,具体见:

Java_web项目中在Java文件里面通过类装载器对资源文件读取

你也可以得到文件发布之后的绝对路径,这样的话还可以得到文件名

改一下ServletContext.java文件内容

//package test;
//
//import java.io.FileInputStream;
//import java.io.IOException;
//import java.io.InputStream;
//import java.util.Properties;
//
//import javax.servlet.ServletException;
//import javax.servlet.annotation.WebServlet;
//import javax.servlet.http.HttpServlet;
//import javax.servlet.http.HttpServletRequest;
//import javax.servlet.http.HttpServletResponse;
//
///**
// * Servlet implementation class ServletContextDemo2
// */
//@WebServlet("/ServletContextDemo2") //注意有了这个就不需要往web.xml文件里面添加路径映射
//public class ServletContextDemo2 extends HttpServlet {
// private static final long serialVersionUID = 1L;
//
// /**
// * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
// */
// protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// // TODO Auto-generated method stub
// //路径上第一个斜杠/相对于项目day02的相对路径
//// String path = this.getServletContext().getRealPath("/build/classes/db.properties");
//// FileInputStream in = new FileInputStream(path);
//// //文件路径
//// System.out.println(path);
//// String file_name = path.substring(path.lastIndexOf("\\")+1);
//// //文件名
//// System.out.println(file_name);
//
// InputStream in = this.getServletContext().getResourceAsStream("/build/classes/db.properties");
// Properties pro = new Properties();
// pro.load(in); //这个文件的值会以map的形式存放
//
// String url = pro.getProperty("url");
// String name = pro.getProperty("name");
// String password = pro.getProperty("password");
//
// System.out.println(url);
// System.out.println(name);
// System.out.println(password);
// }
//
// /**
// * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
// */
// protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// // TODO Auto-generated method stub
// doGet(request, response);
// }
//
//}
package test; import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties; import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; /**
* Servlet implementation class ServletContextDemo2
*/
@WebServlet("/ServletContextDemo2") //注意有了这个就不需要往web.xml文件里面添加路径映射
public class ServletContextDemo2 extends HttpServlet {
private static final long serialVersionUID = 1L; /**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
//路径上第一个斜杠/相对于项目day02的相对路径
String path = this.getServletContext().getRealPath("/build/classes/db.properties");
FileInputStream in = new FileInputStream(path);
Properties pro = new Properties();
pro.load(in); //这个文件的值会以map的形式存放
//文件路径
System.out.println(path);
//文件名
String file_name = path.substring(path.lastIndexOf("\\")+1);
System.out.println(file_name); String url = pro.getProperty("url");
String name = pro.getProperty("name");
String password = pro.getProperty("password"); System.out.println(url);
System.out.println(name);
System.out.println(password);
} /**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
} }

在eclipse完成对Java_web项目里面资源文件的读取的更多相关文章

  1. idea中maven项目xml资源文件无法读取

    解决方法:编辑pom.xml文件 添加如下标签 <build> <resources> <resource> <directory>src/main/j ...

  2. 以Jar形式为Web项目提供资源文件

    以Jar形式为Web项目提供资源文件 http://www.webjars.org/ Web前端使用了越来越多的JS或CSS如jQuery, Backbone.js 和Twitter Bootstra ...

  3. java 资源文件的读取

    Java将配置文件当作一种资源(resource)来处理,并且提供了两个类来读取这些资源,一个是Class类,另一个是ClassLoader类. gradle 项目 项目目录结构  用Class类加载 ...

  4. 说说Java中的资源文件的读取

    最近在看spring的资源获取时发现JDK里存在几种不同方式的资源获取,因比较混乱特地总结起来帮助和我一样混乱的人理解.下面是我项目的类结构图,在 src/main/java 下有两个类 Resour ...

  5. 文件_ _android从资源文件中读取文件流并显示的方法

    ======== 1   android从资源文件中读取文件流并显示的方法. 在android中,假如有的文本文件,比如TXT放在raw下,要直接读取出来,放到屏幕中显示,可以这样: private ...

  6. Eclipse 下如何引用另一个项目的资源文件

    为什么要这么做?可参考:Eclipse 下如何引用另一个项目的Java文件 下面直接说下步骤:(项目A 引用 项目B的资源文件) 1.右键 项目A,点击菜单 Properties 2.在弹出的框中,点 ...

  7. eclipse导入maven项目,资源文件位置显示不正确

    eclipse导入maven项目后,资源文件位置显示不正确,如下图所示 解决方法: 在resources上右键Build Path,选择Use as Source Folder即可正确显示资源文件

  8. web项目获取资源文件

    首页 博客 学院 CSDN学院 下载 论坛 APP CSDN 问答 商城 活动 VIP会员 专题 招聘 ITeye GitChat GitChat 图文课 写博客 消息 1 评论 关注 点赞 回答 系 ...

  9. eclipse下建立 android 项目,相关文件夹介绍

    今天开始进入ANDROID开发,之前一直做些JAVA的WEBSERVICE之类的文件,第一次从头开始整理ANDROID项目,我会把最近遇到的问题做一一梳理. 现在来说一下建立ANDROID项目后产生的 ...

随机推荐

  1. 人生苦短我用Python,本文助你快速入门

    目录 前言 Python基础 注释 变量 数据类型 浮点型 复数类型 字符串 布尔类型 类型转换 输入与输出 运算符 算术运算符 比较运算符 赋值运算符 逻辑运算符 if.while.for 容器 列 ...

  2. XSS-labs通关挑战(xss challenge)

    XSS-labs通关挑战(xss challenge) 0x00 xss-labs   最近在看xss,今天也就来做一下xss-labs通过挑战.找了好久的源码,终于被我给找到了,因为在GitHub上 ...

  3. 【Linux】系统打开文件最大数量限制(进程打开的最大文件句柄数设置)

    利用ulimit命令可以对资源的可用性进行控制. -H选项和-S选项分别表示对给定资源的硬限制(hard limit)和软限制(soft limit)进行设置. 硬限制(hard limit)一旦被设 ...

  4. 关于postgresql中numeric和decimal的精度和标度问题

    精度即数的有效数字个数 2.5的有效数字个数是2,但是053.2的有效数字个数是3 标度是小数点的位数 例如numeric(2,1),即这个数必须是两位,并且小数后面最多有一位,多出来的小数会被四舍五 ...

  5. Goby资产扫描工具安装及报错处理

    官网: https://cn.gobies.org/index.html 产品介绍: 帮企业梳理资产暴露攻击面,新一代网络安全技术,通过为目标建立完整的资产数据库,实现快速的安全应急. 已有功能: 扫 ...

  6. HTML5表格详细教程

    HTML5表格 文章目录 HTML5表格 5.1 定义表格 5.1.1 普通表格.列标题 5.1.2 表格标题 5.1.3 表格行分组.表格列分组 5.2 表格属性 5.2.1 单线表格.分离单元格 ...

  7. python7、8章

    目录 第七章 用户输入和while循环 7.1 函数input()的工作原理 7.1.1 编写清晰的程序 7.1.2 使用int()来获取数值输入 分析: 结果: 7.1.3 求模运算符 7.1.4 ...

  8. Jmeter-插件扩展及性能监控插件的安装

    需要对http服务进行大数据量的传值测试:看看产品中的http服务,能支持传多少字符:目标值是希望能到10w+: 上次测试中,服务器总是内存满导致服务不响应,因此想增加对服务端的性能监控:查阅了smi ...

  9. 关于jmeter客户端实现中HttpClient4与Java的区别

    如上图:jmeter客户端实现方式有三种,一种是java,一种是httpclient4,还有一种默认,我们来看一下java与httpclient4的区别: Java:选择压测时,链接是复用的(代码中的 ...

  10. timeout of 20000ms exceeded

    前端:axios请求超时 axios 中有两种超时错误,一种是 HTTP 的超时,另一种是你自定的超时时间. 请求多次,设置拦截器 ---在main.js设置全局的请求次数,请求的间隙 推荐解决方法: ...