Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You get a <code>NullPointerException</code> because you are instantiating your <code>BookService</code> with a <code>new()</code> - which is basically wrong - and nothing gets injected in the EJB. EJB are component that are managed by the container and should be obtained either via injection or with a lookup. </p> <p>Here, while the JSP spec allows any code to be run in a scriplet, calling an EJB from a JSP is actually not really encouraged and JSPs don't support injection. In other words, you'll have to use a lookup:</p> <pre><code>&lt;%@ page import="beans.BookService" %&gt; &lt;%@ page import="beans.Book" %&gt; &lt;html&gt; &lt;body&gt; &lt;% BookService bs = (BookService) new InitialContext().lookup("java:module/BookServiceBean") Book b = new Book("Superman"); bs.createOrUpdate(b); %&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>But you should call your EJB from a Servlet or a JSF Managed Bean (and your EJB could be injected in such components).</p> <p>If you need some samples, have a look at the <a href="http://java.sun.com/javaee/reference/code/" rel="noreferrer">Java EE Code Samples &amp; Apps</a>.</p> <p><strong>Update:</strong> See <a href="https://glassfish.dev.java.net/javaee5/ejb/EJB_FAQ.html#POJOLocalEJB" rel="noreferrer">How do I access a Local EJB component from a POJO?</a> in the EJB FAQ for more details on JNDI (especially the new <a href="https://glassfish.dev.java.net/javaee5/ejb/EJB_FAQ.html#What_is_the_syntax_for_portable_global_" rel="noreferrer">portable global JNDI names defined by the EJB 3.1 specification</a>).</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload