简体   繁体   English

运行Servlet时出错(Apache Tomcat无法找到.properties文件)

[英]Error while running a servlet (apache tomcat cannot find .properties file)

Any other servlet is running fine on eclipse-apache/tomcat integration. 任何其他servlet在eclipse-apache / tomcat集成上都运行良好。 But when I add a properties file in one of the classes, i get this error- 但是,当我在其中一个类中添加属性文件时,出现此错误-

HTTP Status 500 - type Exception report HTTP状态500-类型异常报告

MessageDescription: The server encountered an internal error () that prevented it from fulfilling this request. MessageDescription:服务器遇到内部错误(),阻止其满足此请求。

exception 例外

java.io.FileNotFoundException: xyz.properties(The system cannot find the file specified) java.io.FileInputStream.open(Native Method) java.io.FileInputStream.(Unknown Source) java.io.FileInputStream.(Unknown Source) xyz.DatabaseConnection(DatabaseAccess.java:23) xyz.HelloServlet.doGet(HelloServlet.java:22) javax.servlet.http.HttpServlet.service(HttpServlet.java:621) javax.servlet.http.HttpServlet.service(HttpServlet.java:722) java.io.FileNotFoundException:xyz.properties(系统找不到指定的文件)java.io.FileInputStream.open(本机方法)java.io.FileInputStream。(未知源)java.io.FileInputStream。(未知源)xyz .DatabaseConnection(DatabaseAccess.java:23)xyz.HelloServlet.doGet(HelloServlet.java:22)javax.servlet.http.HttpServlet.service(HttpServlet.java:621)javax.servlet.http.HttpServlet.service(HttpServlet.java) :722)

Do I have to do some kind of modification in the web.xml file to let it know where the xyz.properties file exists ? 我是否必须在web.xml文件中进行某种修改以使其知道xyz.properties文件的位置?

PS - file and package name has been censored with "xyz". PS-文件和程序包名称已被“ xyz”检查。

You don not need to edit the web.xml. 您不需要编辑web.xml。

ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
Properties properties = new Properties();
properties.load(classLoader.getResourceAsStream("xyz.properties"));

This assumes that the xyz.properties file is inside the classes folder. 假设xyz.properties文件位于classes文件夹内。

I think the property file is not in the classpath. 我认为属性文件不在类路径中。 Please try putting the properties file WEIB-INF/classes folder and see if this resolves this issue. 请尝试将属性文件WEIB-INF/classes文件夹放入,看看是否可以解决此问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM