我尝试通过servlet和bean将jena查询的数据转发到jsp。当我尝试遍历 jsp.file 中的 bean 时,会发生 NPE。当我在迭代之前检查 not null 时,jsp.file 显示没有内容。
小服务程序
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Query query = QueryFactory.read(querySPARQL);
// String co = request.getParameter("country");
query = QueryFactory.create(query.toString().replaceAll("\\?country", "a:Netherlands"));
QueryExecution qexec = QueryExecutionFactory.sparqlService(endpoint,
query);
try {
ResultSet results = qexec.execSelect();
PlantsBean bean = new PlantsBean(results);
HttpSession session = request.getSession();
session.setAttribute("PlantsBean", bean);
} finally {
qexec.close();
}
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
jsp.文件
<%
PlantsBean pb = (PlantsBean)session.getAttribute("PlantsBean");
for(PowerPlantInfo ppi : pb) {
%>
<tr>
<td><%=ppi.getFuel() %></td>
</tr>
<%
}
%>
</table>
错误信息
Servlet.service() for servlet [jsp] in context with path [/SemTec1] threw exception
[java.lang.NullPointerException] with root cause
java.lang.NullPointerException
at org.apache.jsp.te_jsp._jspService(te_jsp.java:107)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
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:722)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:225)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:169)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:168)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:927)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:999)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:565)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:307)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
at java.lang.Thread.run(Thread.java:722)
NPE 发生在一行中:for (PowerPlantInfo : ...)。所以 NPE 说,这个 bean 是空的。但这不可能。耶拿查询工作正常。我也试图将 servlet 放在 web-inf/classes 中,但不起作用。昨天,当我再次设置这整个东西时,我第一次在服务器上运行它,它工作了。第二次,不再工作。
我正在使用 tomcat 7、jdk 7 和 servlets 3.0。
提前致谢。