我是靠谱客的博主 风中热狗,这篇文章主要介绍IDEA使用maven创建普通javaweb项目,调用properties文件出现NullPointerException,现在分享给大家,希望可以做个参考。
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
ServletContext context = this.getServletContext();
InputStream inputStream = context.getResourceAsStream("aa.properties");
Properties pro = new Properties();
pro.load(inputStream);
String user = pro.getProperty("username");
String pwd = pro.getProperty("password");
resp.getWriter().print(user + ": " + pwd);
}
如果路径直接使用aa.properties会出现空指针现象,无法正常调用配置文件,需要将其修改为
InputStream inputStream = context.getResourceAsStream("/WEB-INF/classes/aa.properties");
既调用target目录下生成的aa.properties配置文件,这个target目录默认是不可见的,可以通过Project Srtucture-Modules-当前项目-target-Excluded-Apply-OK使其显示

最后
以上就是风中热狗最近收集整理的关于IDEA使用maven创建普通javaweb项目,调用properties文件出现NullPointerException的全部内容,更多相关IDEA使用maven创建普通javaweb项目内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复