从servlet调用会话bean vs jsp-> servlet授予不同的结果

时间:2014-02-28 15:02:22

标签: java jsp servlets ejb session-bean

我在尝试调用会话bean的方法时遇到问题,当首先从JSP调用servlet时,然后servlet调用会话bean的方法来获取一些似乎得到NullPointerException的数据。另一方面,如果我只是自己运行servlet,它会完成工作并从会话bean中检索数据。

以下是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: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:

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-&gt; servlet-&gt;会话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)

1 个答案:

答案 0 :(得分:0)

您无法像在JSP中那样调用“servlet”实例。 你实际做了什么:

<jsp:useBean id="obj" scope="page" class="web.processItem" />

是创建一个web.processItem的“新”实例,它根本不是容器管理的 - 因此它没有你的依赖项。

servlet only调用确实有效,因为容器确实创建并管理了processItem的实例,并且你已经注入了依赖项。