0

I am trying to set a property to a bean ModelBean in a Filter and access this property in a JSF controller IndexController.

The ModelBean is annotated @SessionScoped and it is used in the filter and controller with @Inject. The problem is that two separate instances are created and I cannot access the property that I set in filter.

What would be the best way to keep the bean alive throughout the session? Or maybe there is a better method to pass data from the filter?

@SessionScoped
public class ModelBean{
    private String deviceId;

    public ModelBean() {
        super();
    }

    public String getDeviceId() {
        return deviceId;
    }

    public void setDeviceId(String deviceId) {
        this.deviceId = deviceId;
    }
}
@Provider
public class AuthRequestFilter implements Filter {

    @Inject
    ModelBean model;


    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws
            IOException, ServletException {

        // the device id is set just fine
        model.setDeviceId(deviceId);
        filterChain.doFilter(servletRequest, servletResponse);
        return;
    }
}
@Named(value = "indexController")
public class IndexController {

    @Inject
    ModelBean model;

    // the method **is* called from the xhtml
    public String justAnExample() {
        // this is the problem, the deviceId is null=>
        return model.getDeviceId();
    }
}
BanzaiTokyo
  • 1,376
  • 4
  • 17
  • 33
  • If two instances are created most likely **you** made and error. But simce there is no [mcve], it is **impossible** to help – Kukeltje Jun 17 '19 at 06:17
  • @Kukeltje thanks for the recommendation. I have Added the code, hope this makes the question more understandable. Let me know if there is anything else that seems unclear to you. – BanzaiTokyo Jun 17 '19 at 06:40
  • How do ypu determine you have two different imstances? And what is the package of `@SessionScoped`? And wjatbis ypur JSF bersion and implementation? – Kukeltje Jun 17 '19 at 07:21
  • @Kukeltje the package is `javax.faces.bean.SessionScoped`. And I determine that those are different objects when I see they have different ids in debug. – BanzaiTokyo Jun 17 '19 at 07:25
  • sorry for the typo's btw... quick typing on my phone ;-) – Kukeltje Jun 17 '19 at 09:33
  • Duplicate of https://stackoverflow.com/questions/9470440/sessionscoped-bean-looses-scope-and-gets-recreated-all-the-time-fields-become. – Kukeltje Jun 17 '19 at 09:35

1 Answers1

0

Thanks to @Kukeltje for the suggestion to look into the packages. I still Don't know why the package javax.faces.bean.SessionScoped that I was using did not keep the bean alive, but replacing it with javax.enterprise.context.SessionScoped did the trick. The bean is now alive and the data can be passed.

BanzaiTokyo
  • 1,376
  • 4
  • 17
  • 33
  • No need to answer... There is a duplicate for this: https://stackoverflow.com/questions/9470440/sessionscoped-bean-looses-scope-and-gets-recreated-all-the-time-fields-become, but in your case it is the 'reverse' – Kukeltje Jun 17 '19 at 09:35