Frage

I'm having a problem trying to call a method of session bean, when calling firstly the servlet from a JSP, then the servlet calling the method of session bean to obtain some data it seems to get a NullPointerException. On the other hand if I just run the servlet on it's own it does the job and retrieves the data from session bean.

The following is the code of the 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;
    }
}

This is the part of JSP that calls the servlet:

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

This is the Session 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;
    }

}

And here is the stack trace when calling from jsp->servlet->session 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)
War es hilfreich?

Lösung

You can't call the "servlet" instance from the JSP like you do. What you actually do with:

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

is to create a "new" instance of web.processItem which is not container managed at all - hence it does not have your dependencies.

The servlet only call does work, because the container did create and manage the instance of processItem and you got your dependencies injected.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top