我使用tomcat作为Web服务器来部署我的webApp。
jsp文件调用其中一个java类来检索内容列表。 jsp代码如下:
<%
String queryKey = request.getParameter("id");
int jobID = Integer.parseInt(queryKey);
out.println(jobID);
ArrayList<Integer> myTopList = JobRecByBoWJaccard.topJobsByBoW(jobID);
%>
在我的java类中,我使用txt文件作为参考来删除停用词。 java类中的代码如下:
public TermsExtraction() {
try {
BufferedReader br = new BufferedReader(new FileReader(
"WebContent/StopWords/stop-words-english1.txt"));
for (String line; (line = br.readLine()) != null;) {
this.stopWords.add(line.trim());
}
// System.out.println(this.stopWords);
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
在类的构造函数中调用此文件。
但是,当加载tomcat时,错误消息为:
java.io.FileNotFoundException: WebContent/StopWords/stop-words-english1.txt (No such file or directory)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(FileInputStream.java:138)
at java.io.FileInputStream.<init>(FileInputStream.java:97)
at java.io.FileReader.<init>(FileReader.java:58)
at demoApp.BOW.TermsExtraction.<init>(TermsExtraction.java:28)
at com.demoApp.jaccardRec.JobInput.inputJobForTermExtract(JobInput.java:21)
at com.demoApp.jaccardRec.JobRecByBoWJaccard.topJobsByBoW(JobRecByBoWJaccard.java:24)
at org.apache.jsp.Result_jsp._jspService(Result_jsp.java:116)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:409)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1044)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:607)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:315)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:722)
etc...
似乎tomcat已经找到了引用的文件。 HELP ...
答案 0 :(得分:1)
如果你想在servlet之外获取ServletContext而不是创建一个ContextListner类,并且在这个类中存储上下文对象在初始化时以及在使用静态方法在应用程序中的任何地方获取它之后。我正在粘贴ContextListener的一个例子
package com.y;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
public class ContextListener implements ServletContextListener {
private static ServletContext servletContext;
@Override
public void contextDestroyed(ServletContextEvent arg0) {
}
@Override
public void contextInitialized(ServletContextEvent arg0) {
servletContext = arg0.getServletContext();
}
public static ServletContext getServletContext(){
return servletContext;
}
}
在这里,您还必须将此列表器的条目输入到web.xml,如下所示
<listener>
<listener-class>com.y.ContextListener</listener-class>
</listener>
现在您可以使用此Context的getResourceAsStream方法读取任何文件,正如我在之前的评论中所提到的那样。
答案 1 :(得分:0)
我认为你需要传递绝对路径。
答案 2 :(得分:0)
你可以试试“./StopWords/stop-words-english1.txt” - “Web内容”可能是多余的。 您还可以使用以下命令获取当前工作目录,并相应地构建路径。
System.getProperty("user.dir"));