[jboss-user] [EJB/JBoss] - Can someone look at this application? help understanding why

rickcr do-not-reply at jboss.com
Wed Oct 1 01:37:52 EDT 2008


I'm trying to use the @EJB annotation on a stateless session bean in a servlet and having troubles getting it to work. I've read some other posts on the issue and supposedly it 'should' work, so I'd love some help trying to figure out what I'm doing wrong. 

I posted the simple source code here: 

http://dl-client.getdropbox.com/u/86998/learntechnology-maven-jee.zip

If you run "mvn install" it will build a user-adminstration.ear in the lt-ear/target directory. When deployed to JBoss .. try to access the URL:
http://localhost:8080/sample/hello

The Stateless session bean never gets set. (You end up with a NPE.)

I'll post some of the relevant code. ..

//lt-ejb-jar:

  | @Local
  | public interface UserService {
  |     public String helloWorld(String s); 
  | }
  | 


  | @Stateless
  | public class UserServiceBean implements UserService {
  | 
  |     public String helloWorld(String s) {
  |             return "Hello World: "+s;
  |     }
  | }
  | 

//lt-web.war...

  | public class SayHelloServlet extends HttpServlet {
  |     private static final long serialVersionUID = 1L;
  | 
  |     //not working: @EJB(mappedName="user-administration/UserServiceBean/local")
  |     //works: private UserService userService = (UserService) ServiceLocator.getEJB("user-administration/UserServiceBean/local"); 
  | 
  |     //not working:
  |     @EJB
  |     private UserService userService;
  | 
  |     public void init() throws ServletException {
  |         super.init();
  |     }
  | 
  |     protected void doGet(HttpServletRequest req, HttpServletResponse resp)
  |         throws ServletException, IOException {
  |         doPost(req, resp);
  |     }
  | 
  |     protected void doPost(HttpServletRequest req, HttpServletResponse response)
  |         throws ServletException, IOException {
  |         System.out.println("userService = " + userService);
  |         response.setContentType("text/plain");
  |         response.getOutputStream().println("Servlet Test: ");
  |         response.getOutputStream().println(userService.helloWorld("My test"));
  |     }
  | }
  | 

web.xml

  | <?xml version="1.0" encoding="UTF-8"?>
  | 
  | <web-app xmlns="http://java.sun.com/xml/ns/j2ee"
  |          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  |          xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
  |          http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
  |          version="2.4">
  |     
  |     <servlet>
  |         <servlet-name>SayHelloServlet</servlet-name>
  |         <servlet-class>
  |             net.learntechnology.web.SayHelloServlet
  |         </servlet-class>
  |         <load-on-startup>0</load-on-startup>
  |     </servlet>
  |      
  |     <servlet-mapping>
  |         <servlet-name>SayHelloServlet</servlet-name>
  |         <url-pattern>/hello</url-pattern>
  |     </servlet-mapping>
  |     
  | </web-app>
  | 

Thanks for any help with this.

View the original post : http://www.jboss.com/index.html?module=bb&op=viewtopic&p=4179690#4179690

Reply to the post : http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&p=4179690



More information about the jboss-user mailing list