2012-04-10 25 views
1

我嘗試通過servlet和bean將jena查詢的數據轉發給jsp。 當我嘗試遍歷jsp.file中的bean時,發生NPE。當我在迭代之前檢查不爲null時,jsp.file顯示沒有內容。java servlet 3.0空指針轉發jena查詢

的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在線路中發生:對於(PowerPlantInfo:...)。所以NPE說,這個bean是空的。但那是不可能的。 jena查詢工作正常。 als我試圖把servlet放在web-inf/classes中,不工作。昨天,當我再次設置這個事情時,我第一次在服務器上運行它,它工作。第二次,不再工作。

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

在此先感謝。

回答

0

您的Servlet不會將請求轉發給JSP。所以我猜你直接訪問JSP(即http://yourhost/path/yourfile.jsp)。

相反,我希望你的Servlet重定向到JSP(使用request.getRequestDispatcher("yourfile.jsp"))。當您使用瀏覽器訪問您的Servlet時(即,http://yourhost/path/YourServlet),您的Servlet將運行,最終重定向到JSP。