java servlet 3.0空指针转发jena查询

时间:2012-04-10 06:19:45

标签: jsp servlets nullpointerexception

我尝试通过servlet和bean将jena查询的数据转发给jsp。 当我尝试在jsp.file中遍历bean时,会发生NPE。当我在迭代之前检查非null时,jsp.file显示没有内容。

的servlet

    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.file

<%

        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说,豆子是空的。但这是不可能的。 jena查询工作正常。 我尝试将servlet放在web-inf / classes中,而不是工作。昨天,当我再一次设置这个东西时,我第一次在服务器上运行它,它起作用了。第二次,不再工作了。

我正在使用tomcat 7,jdk 7和servlets 3.0。

提前感谢。

1 个答案:

答案 0 :(得分:0)

您的Servlet不会将请求转发给JSP。所以我猜你是直接访问JSP(即http://yourhost/path/yourfile.jsp)。

相反,我希望您的Servlet重定向到JSP(使用request.getRequestDispatcher("yourfile.jsp"))。当您使用浏览器访问Servlet时(即http://yourhost/path/YourServlet),您的Servlet将运行,最终重定向到JSP。