Note that there are some explanatory texts on larger screens.

plurals
  1. POJSF request scoped bean keeps recreating new Stateful session beans on every request?
    primarykey
    data
    text
    <p>I'm building my first Java EE application using JSF, PrimeFaces, Glassfish and Netbeans. Because I'm new, it's possible I'm approaching the core problem wrong. </p> <p>Core problem: I want to maintain user's information securely. There seems to be conflicting ideas on whether it should be maintained in a JSF session bean or a stateful session EJB. I'm trying to use a stateful session EJB because it is more secure that way.</p> <p>The problem is that my application seems to be creating multiple instances of that bean when I expect it to create one and re-use it. If I refresh the page it runs the <code>@PostConstruct</code> and <code>@PostActivate</code> 3 times, all of them with a different instances. Then they all get destroyed when I re-deploy the application. </p> <p>Did I misunderstand how it should work or is something wrongly configured?</p> <p>I'll try to show a trimmed down code sample:</p> <p><code>basic.xhtml</code>:</p> <pre><code>&lt;?xml version='1.0' encoding='UTF-8' ?&gt; &lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt; &lt;html xmlns="http://www.w3.org/1999/xhtml" xmlns:h="http://java.sun.com/jsf/html" xmlns:c="http://java.sun.com/jsp/jstl/core"&gt; &lt;h:head&gt; &lt;title&gt;Facelet Title&lt;/title&gt; &lt;/h:head&gt; &lt;h:body&gt; Hello from Facelets &lt;c:if test="#{loginController.authenticated}"&gt; Authenticated &lt;/c:if&gt; &lt;c:if test="#{loginController.authenticated}"&gt; Authenticated &lt;/c:if&gt; &lt;c:if test="#{loginController.authenticated}"&gt; Authenticated &lt;/c:if&gt; &lt;/h:body&gt; &lt;/html&gt; </code></pre> <p><code>LoginController</code>:</p> <pre><code>@Named(value = "loginController") @RequestScoped public class LoginController implements Serializable { @EJB private UserBeanLocal userBean; public boolean isAuthenticated() { return userBean.isAuthenticated(); } } </code></pre> <p><code>UserBean</code> (excluding <code>UserBeanLocal</code> interface)</p> <pre><code>@Stateful public class UserBean implements UserBeanLocal, Serializable { boolean authenticated = false; @PostConstruct @PostActivate public void setup(){ System.out.println("##### Create user Bean: "+this.toString()); } @Override public boolean isAuthenticated() { System.out.println("########## Authentication test is automatically passing."); authenticated = true;//hard coded for simplicity. return authenticated; } @PrePassivate @PreDestroy public void cleanup(){ System.out.println("##### Destroy user Bean"); } } </code></pre> <p>Finally, here is the Glassfish output after refreshing three times:</p> <pre class="lang-none prettyprint-override"><code>INFO: ##### Create user Bean: boundary._UserBean_Serializable@2e644784 INFO: ########## Authentication test is automatically passing. INFO: ########## Authentication test is automatically passing. INFO: ########## Authentication test is automatically passing. INFO: ##### Create user Bean: boundary._UserBean_Serializable@691ae9e7 INFO: ########## Authentication test is automatically passing. INFO: ########## Authentication test is automatically passing. INFO: ########## Authentication test is automatically passing. INFO: ##### Create user Bean: boundary._UserBean_Serializable@391115ac INFO: ########## Authentication test is automatically passing. INFO: ########## Authentication test is automatically passing. INFO: ########## Authentication test is automatically passing. </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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