我知道这个问题已经被问了一百万次,并且有 500 万个答案——有些或其中的信息量很大。没有一个解决了这个问题。
我的目标与其他许多目标相似——我想在 web 应用程序运行时环境中访问文件——图像、svgfiles、模板等。如果我只是硬连线目录路径,我可以让它工作。但是,我想让我的 servlet 可移植并使用相对引用来访问这些资源——如果没有其他原因我在 Windows 中开发并在 Linux 中部署。
基本问题是 getServletContext() 为空,我无法确定原因。
下面是错误消息,生成它的代码,然后是环境详细信息。在开发和生产中产生相同的错误。如果需要,我很乐意提供任何其他详细信息。
请解释我需要做什么才能使其正常工作?我将永远感激不尽。问候
SEVERE: Servlet.service() for servlet [jsp] in context with path [/HelloWorld] threw exception [An exception occurred processing JSP page /hello.jsp at line 19
16: <title>Hello World</title>
17: </head>
18: <body>
19: <%=wtGreet.getGreeting()%>
20: </body>
21: </html>
Stacktrace:] with root cause
java.lang.NullPointerException
at javax.servlet.GenericServlet.getServletContext(GenericServlet.java:125)
at HelloWorld.Greeting.getGreeting(Greeting.java:23)
at org.apache.jsp.hello_jsp._jspService(hello_jsp.java:91)
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:51)
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:408)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1041)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:603)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
HelloWorld 应用程序工作 - 产生不希望的错误 - 与真实事物完全相同。
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF 8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<jsp:useBean id="wtGreet" scope="page"
class="HelloWorld.Greeting">
<jsp:setProperty name="wtGreet" property="who" value="World"/>
<jsp:setProperty name="wtGreet" property="greet" value="Hello"/>
</jsp:useBean>
<jsp:setProperty name="wtGreet" property="*"/>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Hello World</title>
</head>
<body>
<%=wtGreet.getGreeting()%>
</body>
</html>
然后jsp调用的Servlet
package HelloWorld;
import javax.servlet.http.HttpServlet;
public class Greeting extends HttpServlet {
private static final long serialVersionUID = 1298516959968350334L;
private String who;
private String greet;
public void setWho(String who) {
this.who = who;
}
public void setGreet(String greet) {
this.greet = greet;
}
public String getGreeting() {
System.out.println("getServletContext() == null :" + getServletContext().getContextPath());
return "<p>" + this.greet + " " + this.who + "</P>";
}
}
开发环境 Windows Eclipse JEE Apache Tomcat 7 JRE 7
生产环境 Linux Apache Tomcat 7 JRE 8