title: Understanding EJB Architecture and Implementation
body: <p>I am a beginner in the Jakarta EE world.
  I am going through a Jakarta EE module at university and I am struggling to find information that properly answers my question.
  I feel like it is one of those things that is so simple, straight forward &amp; expected to be understood that I am just not getting it.</p>
  <p>My tutor has not been very helpful, telling me not to worry about anything else and just to accept that &quot;its just encapsulated business logic&quot; this is meaningless and useless, because if that is all it was why cant I just create my own class.</p>
  <p>Now I have gone through this article <a href="https://docs.oracle.com/cd/E16439_01/doc.1013/e13981/undejbs002.htm" rel="nofollow noreferrer">https://docs.oracle.com/cd/E16439_01/doc.1013/e13981/undejbs002.htm</a> and I get the basic overview.</p>
  <p>I am aware of the following:</p>
  <ol>
  <li>EJB runs inside an EJB Container which runs inside an Application server,</li>
  <li>Two types of EJB: Session Bean, Message Driven Bean</li>
  <li>Three types of Session Bean: Stateless, Stateful, Singleton</li>
  <li>@Stateless does not maintain &quot;conversational state&quot; between session/method invocation? whereas Stateful beans do?</li>
  <li>The container has a pool of these EJB (Java classes) basically that are instantiated and randomly assigned to a client  in the case of @stateless</li>
  <li>@Stateful beans are kept in a pool and the same client is assigned the same bean?</li>
  </ol>
  <p>Please feel free to correct any of the above.
  Based on what I know so far the overall architecture still seems to confuse me,</p>
  <p>I have the following 6 questions (all are linked) I am sorry if I have not organised them properly, I will try edit/improve the answer:</p>
  <ol>
  <li>How do non Java (generic clients) access EJB? Is this even possible?</li>
  </ol>
  <p>As far as I am aware most clients are one of the following:
  a) Web Browser in laptop/tablet/mobile,
  b) Native Mobile App,
  c) Some IOT device or another kind of device (for example delivery driver    saving something to a unique handset) etc...</p>
  <p>Now! Say the clients mentioned above (mostly a &amp; b) as they are the most common need to access the same &quot;business logic&quot;,
  How would they go about this?
  I assume native mobile apps also use the HTTP/s protocol?
  Would this mean that it would only be through a Servlet? Which is sort of like an MVC Controller?</p>
  <p>Based on this diagram from the official Java EE documentation it would appear as though Web Browsers cant access EJB direct:</p>
  <p><a href="https://i.stack.imgur.com/PeUUD.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/PeUUD.png" alt="enter image description here" /></a></p>
  <ol start="2">
  <li>If the answer to (1) is no, that clients such as web/mobile apps cant directly access an EJB, is there any use for a @Stateful session bean?
  Say for example in a web app, HTTP protocol is stateless, state can be maintained with the help of session/cookie,
  Which would mean the state is maintained by the Web Container? Whats the point of having a stateful session bean?</li>
  </ol>
  <p>I have read other questions on stack overflow which suggest its not a good idea to have @stateful session bean with Servlet.</p>
  <p>I get the whole shopping cart example, but this does not explain how state is maintained, if I was accessing my shopping cart through a browser, the cookies would be responsible for state right? Does EJB have access to browser cookies?</p>
  <ol start="3">
  <li>If an EJB can only be directly accessed by Java Clients:</li>
  </ol>
  <p>Example of Java Clients as far as I know:
  Servlet,
  Another EJB running local or remote,
  Another Java Class?,
  Standalone Java app?</p>
  <p>I assume these clients would access the EJB by RMI/JNDI/CDI/Dependency Injection (in case of servlet)?
  Would it be fair to say that an EJB is similar to a SOAP Web Service in the Java EE world?</p>
  <ol start="4">
  <li><p>Based on all of the above is there much of a use case for EJB anymore?
  Especially as most apps are either through the web or mobile using HTTP/Rest end points, many use JWT's to manage state client side, and I cant see many examples of standalone Java apps except for maybe an Android app etc..</p>
  </li>
  <li><p>Is it better to use MVC/Spring/Spring Boot which seems to have a more understandable architechture and create REST/SOAP Web Services</p>
  </li>
  <li><p>Is RMI also using HTTP? Does EJB understand HTTP?</p>
  </li>
  </ol>
  <p>I think most of these questions surround the same point. I cant get a clear answer, I think It should be obvious but I just dont get it.
  Any advice would be greatly appreciated</p>