Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6535497
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T10:22:50+00:00 2026-05-25T10:22:50+00:00

I am trying to inject an EJB with the @EJB annotation : when I

  • 0

I am trying to inject an EJB with the @EJB annotation :

  • when I inject an EJB into another EJB it works fine.
  • when I inject the same EJB into a servlet I got a null pointer exception (my EJB is null).

myapp.ear contains the following :

  • myapp.war (where the servlet is located)
  • myapp.jar (where the EJBs are)

EJB Interface :

package com.mycompany.myapp.ejb.hello;

@Local
public interface HelloEjb {
    public final static String NAME = "HelloEjb";   
    public String sayHello();
}

EJB Impl :

package com.mycompany.myapp.ejb.hello;

@Stateless(name = HelloEjb.NAME)
public class HelloEjbImpl implements HelloEjb {

    @Override
    public String sayHello() {
        return "Hello";
    }
}

My Servlet :

public class HelloServlet extends HttpServlet {

    @EJB
    private HelloEjb helloEjb;

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        resp.setContentType("text/html");
        PrintWriter out = resp.getWriter();
        out.println("<html>");
        out.println("This is my stateless session-EJB: " + helloEjb.sayHello());
        out.println("</html>");
        out.close();
    }
}

The web.xml in the WAR has nothing spetacular, only the servlet defintion, no ejb tags :

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="MyApp" version="2.5"
    xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

    <!-- General -->
    <display-name>My App</display-name>

    <!-- Standard Action Servlet Configuration -->
    <servlet>
        <servlet-name>helloServlet</servlet-name>
        <servlet-class>com.mycompany.myapp.web.servlet.HelloServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <!-- Standard Action Servlet Mapping -->
    <servlet-mapping>
        <servlet-name>helloServlet</servlet-name>
        <url-pattern>*.do</url-pattern>
    </servlet-mapping>

    <!-- The Usual Welcome File List -->
    <welcome-file-list>
        <welcome-file>/index.jsp</welcome-file>
    </welcome-file-list>

</web-app>

If in the servlet I use lookup, it works I am able to get the EJB :

helloEjb = ctx.lookup("ejblocal:com.mycompany.myapp.ejb.hello.HelloEjb");

I am using Websphere 7 & EJB 3.0

Any help would be much appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T10:22:50+00:00Added an answer on May 25, 2026 at 10:22 am

    My first shot is, if you specifiy a ‘name’ for your stateless EJB you should inject it with ‘mappedName’ attribute eg. ‘@EJB(mappedName=”foo.FooRemote”)’

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got an app that is trying to use @EJB annotation to inject remote
I am trying to inject an EJB with the @EJB annotation: when I inject
I am trying to Inject a seam component into another, auto creating it. But
Currently I am trying to inject a stateless EJB into a CDI managed controller
I'm trying to inject Spring beans into an EJB using @Interceptors(SpringBeanAutowiringInterceptor.class) but I cannot
I'm trying to inject entire JSF managed bean into another managed bean by means
Hi i am trying to inject custom css into a page my manifest file
I'm trying to inject some Scala code into my existing Java app. (So, being
I am trying to inject an IList into a constructor and want to know
I am trying to inject json into my backbone.js app. My json has &quot;

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.