0

I have created simple EJB and i am trying to use in my sample servlet, i am using TomEE server and eclips as IDE

following is my code

HelloBeanInterface.java

package com.ccc.demo;

public interface HelloBeanInterface {
    public String from();
}

HelloBean.java

package com.ccc.demo;

import javax.ejb.LocalBean;
import javax.ejb.Stateless;

 /**
 * Session Bean implementation class HelloBean
 */
@Stateless
@LocalBean
public class HelloBean implements HelloBeanInterface{

    private String message = "I am from EJB";

    public String from() {
        
        return message;

    }

}

HelloServlet.java

package com.ccc.demo;

import java.io.IOException;
import java.io.Writer;

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;

/**
 * Servlet implementation class HelloServlet
 */
@WebServlet("/HelloServlet")
public class HelloServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    @EJB
    private HelloBeanInterface bean;

   
     /**
     * @see HttpServlet#HttpServlet()
     */
    public HelloServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws 
    ServletException, IOException {

        response.setContentType("text/html");
    
        Writer out = response.getWriter();
    
        out.append("<html>");
        out.append("<body>");
        out.append("<h1>Hello Servlet</h1>");
        out.append("<h1>"+bean+"</h1>");
        out.append("</body>");
        out.append("</html>");
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
        protected void doPost(HttpServletRequest request, HttpServletResponse response) throws 
        ServletException, IOException {
                // TODO Auto-generated method stub
                doGet(request, response);
      }

  }

my problem is here out.append("<h1>"+bean+"</h1>"); value for bean is printing NULL which i have declared here

@EJB
private HelloBeanInterface bean;

it is my result after run

enter image description here

Jignesh Ansodariya
  • 12,583
  • 24
  • 81
  • 113
  • Your code looks pretty much correct. I suspect you may need to annotate the `HelloBeanInterface` with `@Local` (`javax.ejb.Local`). If it still does not work, it may have to do with configuration (TomEE specific?) – Nikos Paraskevopoulos May 10 '21 at 14:30
  • Thanks @NikosParaskevopoulos i tried but no luck – Jignesh Ansodariya May 12 '21 at 09:52
  • Strange... I am not familiar with TomEE, so cannot tell for sure. First thing I would do is decide whether the problem is that the server is not deploying the EJB at all (so nothing is available for injection), or just that injection doesn't work. Is there any hint (debugging? server logs?) that the EJB is indeed deployed? If it is, can you inject it to another EJB using `@EJB`? – Nikos Paraskevopoulos May 12 '21 at 10:05
  • Can you provide the code as a self containing example via GitHub? I can have a look. – rzo1 May 13 '21 at 08:58

1 Answers1

-1

I'll just hazard a guess here that the servlet is only in the Tomcat http server system, and although you are using ejb classes you should also have configured an ejb application and ejb container configuration file and have an ejb bean class with the bean rules syntax.

Samuel Marchant
  • 331
  • 2
  • 6