title: how to access a @Stateless @LocalBean remotely
body: <p>I'm following an <a href="https://www.packtpub.com/networking-and-servers/ejb-31-cookbook" rel="nofollow">EJB cookbook</a>, from packt, with the following code:</p>
  
  <pre><code>package packt;
  
  import javax.ejb.Stateless;
  import javax.ejb.LocalBean;
  
  //@Stateless
  @LocalBean
  @Stateless(mappedName="salutationBean")
  public class Salutation {
      public String getFormalSalutation(String name) {
          return "Dear " + name;
      }
      public String getInformalSalutation(String name) {
          return "Hi " + name;
      }
  }
  </code></pre>
  
  <p>How do i access this bean from a class which doesn't extend <code>HttpServlet</code>?  The servlet itself works fine:</p>
  
  <pre><code>package servlet;
  
  import java.io.IOException;
  import java.io.PrintWriter;
  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 packt.Salutation;
  
  @WebServlet(urlPatterns = {"/SalutationServlet"})
  public class SalutationServlet extends HttpServlet {
  
      @EJB
      private Salutation salutation;
  
      protected void processRequest(HttpServletRequest request,
              HttpServletResponse response)
              throws ServletException, IOException {
          response.setContentType("text/html;charset=UTF-8");
          PrintWriter out = response.getWriter();
          try {
              out.println("&lt;html&gt;");
              out.println("&lt;head&gt;");
              out.println("&lt;title&gt;Servlet SalutationServlet&lt;/title&gt;");
              out.println("&lt;/head&gt;");
              out.println("&lt;body&gt;");
              out.println("&lt;h1&gt;"
                      + salutation.getFormalSalutation("Sherlock Holmes")
                      + "&lt;/h1&gt;");
              out.println("&lt;/body&gt;");
              out.println("&lt;/html&gt;");
          } finally {
              out.flush();
              out.close();
          }
      }
  
      @Override
      protected void doGet(HttpServletRequest request,
              HttpServletResponse response)
              throws ServletException, IOException {
          processRequest(request, response);
      }
  
      @Override
      protected void doPost(HttpServletRequest request,
              HttpServletResponse response)
              throws ServletException, IOException {
          processRequest(request, response);
      }
  }
  </code></pre>
  
  <p>and is viewable from the designated URL:</p>
  
  <pre><code>thufir@dur:~$ 
  thufir@dur:~$ lynx http://localhost:8080/SalutationApplication-war/SalutationServlet -dump
                                Dear Sherlock Holmes
  
  
  thufir@dur:~$ 
  </code></pre>
  
  <p>The current structure:</p>
  
  <pre><code>thufir@dur:~/NetBeansProjects$ 
  thufir@dur:~/NetBeansProjects$ tree SalutationApplication/
  SalutationApplication/
  ├── build.xml
  ├── nbproject
  │   ├── ant-deploy.xml
  │   ├── build-impl.xml
  │   ├── genfiles.properties
  │   ├── private
  │   │   └── private.properties
  │   ├── project.properties
  │   └── project.xml
  ├── SalutationApplication-ejb
  │   ├── build.xml
  │   ├── nbproject
  │   │   ├── ant-deploy.xml
  │   │   ├── build-impl.xml
  │   │   ├── genfiles.properties
  │   │   ├── private
  │   │   │   └── private.properties
  │   │   ├── project.properties
  │   │   └── project.xml
  │   └── src
  │       ├── conf
  │       │   └── MANIFEST.MF
  │       └── java
  │           └── packt
  │               └── Salutation.java
  ├── SalutationApplication-war
  │   ├── build.xml
  │   ├── nbproject
  │   │   ├── ant-deploy.xml
  │   │   ├── build-impl.xml
  │   │   ├── genfiles.properties
  │   │   ├── private
  │   │   │   └── private.properties
  │   │   ├── project.properties
  │   │   └── project.xml
  │   ├── src
  │   │   ├── conf
  │   │   │   └── MANIFEST.MF
  │   │   └── java
  │   │       └── servlet
  │   │           └── SalutationServlet.java
  │   └── web
  │       ├── index.html
  │       └── WEB-INF
  └── src
      └── conf
          └── MANIFEST.MF
  
  20 directories, 27 files
  thufir@dur:~/NetBeansProjects$ 
  </code></pre>
  
  <p>Right now it's fine that it's only a local bean, but ultimately I would like the Salutation.java bean to be accessible remotely.  I have a passing familiarity with EJB from <a href="http://www.headfirstlabs.com/books/hfejb/" rel="nofollow">Head First EJB</a>, but that's quite old.</p>