Click here to Skip to main content
15,040,003 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
MyCart.java

This MyCart Bean, is CDI Named bean Instantiated per HTTP Session.
Java
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 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
Java
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

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

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

2) GetCDIBeanValue.jsp
In Which, I would retrieve the Bean Property Value that was set from First JSP, and show it in Paragraph.
How ever getFromSecondJsp.getValue() always return NULL

I know, there is some mistake and not have understood the concept well.
But will it be possible that some one let me know where I am making a Mistake.
Thanks .



SetCDIBeanValue.jsp
XML
<%@ 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

XML
<%@ 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 getFromSecondJsp = (MyCartAbstract) UIBeanLocator.getBeanByName("cart");
# Getting the Session Instantiated Bean by its Name
%>

<html>
    <head>
    <%@include file="SetCDIBeanValue.jsp"%>
    </head>
    <body>
    <p>
    <%
        out.print("Get Bean Property Value: " + getFromSecondJsp.getValue());
    %></p>
    </body>
</html>


Always Show the following in the Browser when Launching the GetCDIBeanValue.jsp
Get Bean Property Value: null


I am getting back the Instance of the Bean as follow


Java
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;
    }


WebServer
GlassFish Server Used


Thanks and Regards
Durai Velan C.
Posted
Updated 8-Apr-15 3:48am
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900