Search code examples
jspjakarta-eecdi

Understanding CDI Session Scoped Bean


MyCart.java

This MyCart Bean, is CDI Named bean Instantiated per HTTP Session.

package cdv.project.bean;
import cdv.project.api.*;
 
@Named("cart")
@SessionScoped
public class MyCart extends MyCartAbstract {
       private String value;
 @Override
 public String getValue() {
        System.out.println("Getting Value Value to : "+ this.value);
        return "FromBeanClass: "+this.value;
  }
 
 @Override
 public void setValue(String a) {
     System.out.println("Setting Value to : "+ a);
        this.value = a;
         System.out.println("Setting Value to ddd: "+ this.value);
  }
}

Abstract Class

package cdv.project.api;
public abstract class MyCartAbstract {
public abstract String getValue();
public abstract void setValue(String a);
} 

Problem Description

For just Understanding the Session Scoped CDI. I have two JSP File as Follows

SetCDIBeanValue.jsp In which, I would get the Instance of the Named Session CDI Bean ( MyCart ) Set the Value of It String Property to String value "FROM_FIRST_JSP"

Since this Session scoped there should be only one Session Instantiated I guess.

GetCDIBeanValue.jsp In Which, I would retrieve the Bean Property Value that was set from First JSP, and show it in Paragraph. However I would get the Bean Instance using different name (For more Info see below )

getFromSecondJsp.getValue() always return NULL

SetCDIBeanValue.jsp

<%@ page import="cdv.project.api.*" %>
<%@taglib prefix="f" uri="http://java.sun.com/jsf/core"%>
<%@taglib prefix="h" uri="http://java.sun.com/jsf/html"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 
<%
MyCartAbstract SetFromFirstJsp = (MyCartAbstract) UIBeanLocator.getBeanByName("cart");
# Getting the Session Instantiated Bean by its Name
SetFromFirstJsp.setValue("FROM_FIRST_JSP");
%>

GetCDIBeanValue.jsp

<%@ page import="cdv.project.api.*" %>
<%@taglib prefix="f" uri="http://java.sun.com/jsf/core"%>
<%@taglib prefix="h" uri="http://java.sun.com/jsf/html"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 <%@include file="SetCDIBeanValue.jsp"%>
<%
MyCartAbstract getFromSecondJsp = (MyCartAbstract) UIBeanLocator.getBeanByName("cart");
%>
<html>
    <head></head>
    <body>
<p><%out.print("Get Bean Property Value From FirstJSP : " + SetFromFirstJsp.getValue());%></p>
<p><%out.print("Get Bean Property Value From SecondJSP: " + getFromSecondJsp.getValue());%></p>
    </body>
</html>

Now Deploy the Project in a Glassfish Server, and browse the GetCDIBeanValue.jsp

And it always show the following in the Browser

Get Bean Property Value From FirstJSP : FromBeanClass: FROM_FIRST_JSP

Get Bean Property Value From SecondJSP: FromBeanClass: null

Code to Get the BeanInstance

public static Object getBeanByName(String name) {
        BeanManager bm = getBeanManager();
        Set<Bean<?>> beans  = bm.getBeans(name);
        Bean bean = beans.iterator().next();
        CreationalContext ctx = bm.createCreationalContext(bean);
        Object o = bm.getReference(bean, bean.getBeanClass(), ctx);
        return o;
    }

Question Am I not getting the Same Instance of the MyCart Bean Class from both the JSP. Means the Object SetFromFirstJsp getFromSecondJsp

Does it not access the same Bean Instance, or Each JSP Instantiates different Instance of MyCart CDI Bean Class. ? I know, there was some mistake and I have not have understood the concept well. But will it be possible that someone let me know where I am making a Mistake. Thanks.


NOTE

The Same works Perfectly, if change the following from

    @Named("cart")
    @SessionScoped
    public class MyCart extends MyCartAbstract {

to

    @Named("cart")
    @ApplicationScoped
    public class MyCart extends MyCartAbstract {

Solution

  • My Bad, I was wrong and found out the issue i was using JSF Managed Bean import instead of CDI Bean... After Changing the Import to CDI Bean, it is working as Expected....... Means my Annotation import was worng I was Loading JSF instead of CDI.