0
我在嘗試調用會話bean的方法時,首先從JSP調用servlet,然後調用會話bean的方法到servlet獲取一些數據似乎得到一個NullPointerException。另一方面,如果我只是自己運行servlet,它會完成這項工作並從會話bean中檢索數據。從servlet調用會話bean與jsp-> servlet授予不同的結果
以下是servlet的代碼;
package web;
import java.io.IOException;
import java.util.ArrayList;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import auction.ItemRegistrationSessionBeanRemote;
/**
* Servlet implementation class processItem
*/
@WebServlet("/processItem")
public class processItem extends HttpServlet {
private static final long serialVersionUID = 1L;
public ArrayList<String> categories = new ArrayList<String>();
@EJB ItemRegistrationSessionBeanRemote itemRegistrationSession;
/**
* @see HttpServlet#HttpServlet()
*/
public processItem() {
super();
}
@PostConstruct
public
void init() {
System.out.println("init()");
categories = itemRegistrationSession.getCategories();
System.out.println("init() categories = " + categories.toString());
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {}
@PostConstruct
public void obtainCategories() {
categories = itemRegistrationSession.getCategories();
}
@PostConstruct
public ArrayList<String> getCategories() {
return categories;
}
}
這是調用servlet的JSP的部分:從JSP-致電時>
package auction;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.LocalBean;
import javax.ejb.Stateful;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import com.sun.jersey.spi.resource.Singleton;
import entity.Category;
/**
* Session Bean implementation class ItemRegistrationSessionBean
*/
@Stateful @Singleton
public class ItemRegistrationSessionBean implements ItemRegistrationSessionBeanRemote {
@PersistenceContext(name = "MiniEbayEJB")
private EntityManager emgr;
/**
* Default constructor.
*/
public ItemRegistrationSessionBean() {
// TODO Auto-generated constructor stub
}
@Override
public ArrayList<String> getCategories() {
System.out.println("Hello from SessionBean getCategories");
ArrayList<String> categories = new ArrayList<String>();
List<Category> obtainedCategories = (List<Category>) emgr.createNamedQuery("Category.findAll", Category.class).getResultList();
for (int i = 0; i < obtainedCategories.size(); i++) {
categories.add(obtainedCategories.get(i).toString());
System.out.println("Current ArrayList of categories = " + categories);
}
return categories;
}
}
這裏是堆棧跟蹤:
<jsp:useBean id="obj" scope="page" class="web.processItem" />
<select name="affiliation" id="categories-dropdown">
<% obj.init(); %>
<c:forEach var="aff" items="${obj.getCategories()}"
<%System.out.println("categories = " + obj.getCategories()); %>
<option value="${aff}">${aff}</option>
</c:forEach>
</select>
這是會話Bean servlet->會話bean:
2014-02-28T14:48:21.696+0000|WARNING: StandardWrapperValve[jsp]: PWC1406: Servlet.service() for servlet jsp threw exception
java.lang.NullPointerException
at web.processItem.init(processItem.java:35)
at org.apache.jsp.insert_jsp._jspService(insert_jsp.java:166)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:111)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:770)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:411)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:473)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:377)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:770)
at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1550)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:281)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:655)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:595)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:161)
at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:331)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:231)
at com.sun.enterprise.v3.services.impl.ContainerMapper$AdapterCallable.call(ContainerMapper.java:317)
at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:195)
at com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:860)
at com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:757)
at com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:1056)
at com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:229)
at com.sun.grizzly.DefaultProtocolChain.executeProtocolFilter(DefaultProtocolChain.java:137)
at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:104)
at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:90)
at com.sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:79)
at com.sun.grizzly.ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:54)
at com.sun.grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:59)
at com.sun.grizzly.ContextTask.run(ContextTask.java:71)
at com.sun.grizzly.util.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:532)
at com.sun.grizzly.util.AbstractThreadPool$Worker.run(AbstractThreadPool.java:513)
at java.lang.Thread.run(Thread.java:744)
有沒有辦法從jsp中調用servlet以使其受到資源管理器的管理?感謝您的解釋 – Adrian
通常是相反的。您有一個URL映射到一個Servlet,並調用JSP來表示View。如果您想調用Servlet,只需將您的JSP轉發或重定向到servlet即可。或者只需將您的列表放入servlet中的pageContext並使用您的JSP進行呈現。只需使用您在servlet中未實現的doGet方法,將結果(您的列表等)放置到pageContext(通過請求對象上的setAttribute)並在jsp中呈現您的html並完成。 –