0

we have to use same session in 2 different web module using websphere extension in one ear project .how to get session from one web module in other web module. we are using IBM RAD 7.5, WebSphere 7.0, JSP Frame work.

till now we have tried this approch

  1. Our project structure

common project with common session EAR project web project 1 web project 2

  1. we have applied websphere extension for our project
  2. our web project 1 contain first.jsp from which we are sending the session. code - getFacesContext().getExternalContext().getSessionMap().put("key",common session bean); 4.our web project 2 contain sec.jsp in which we are receiving the session. code - getFacesContext().getExternalContext().getSessionMap().get("key"); the above code returns an object, but we need common session so we have typecaste the returned object to common session type
  3. now when we run the first.jsp on server we get error

java.lang.ClassCastException: common.session.Sess incompatible with common.session.Sess

pnuts
  • 58,317
  • 11
  • 87
  • 139
  • You obviously have different versions of Sess in these two WARs. Please check your classpath. If you want to avoid classpath problem, you might serialize the whole object into JSON. – Magic Wand Jul 25 '14 at 07:58
  • we have same class Sess in both the modules. just we have created session bean for both the modules using the same class. – user3454718 Jul 25 '14 at 08:10
  • What about EAR classloader policy? Do you use classloader policy MODULE od APPLICATION. Try to change it into APPLICATION. – Magic Wand Jul 25 '14 at 08:17
  • what will be the solution in our scenario if we want to share the session managed bean created in one module into other web module for example one login session bean for different web module. – user3454718 Jul 25 '14 at 08:18
  • If you have classloader policy set to APPLICATION, then all application modules have the same classloader (and by that they could share the same objects). Then you can use Singleton pattern (really not advisable in Java EE, but you can use it), or use something like DistributedMap provided by WebSphere (http://www-01.ibm.com/support/knowledgecenter/SSEQTP_7.0.0/com.ibm.websphere.javadoc.doc/web/apidocs/com/ibm/websphere/cache/DistributedMap.html?lang=en). – Magic Wand Jul 25 '14 at 08:23

2 Answers2

0

Try changing EAR classloader policy into APPLICATION and use Singleton pattern, or you can use DistributedMap (http://www-01.ibm.com/support/knowledgecenter/SSEQTP_7.0.0/com.ibm.websphere.javadoc.doc/web/apidocs/com/ibm/websphere/cache/DistributedMap.html?lang=en), which is specific for WebSphere but can provide a very powerful Map-like interface for object storing and retrieval.

Magic Wand
  • 1,572
  • 10
  • 9
  • Distributed Map also return value of Object type but i need the value in common.session.Sess type, as i want complete session managed bean in 2nd web project, for example let login session in 1st web project, i need this session in 2nd web project . therefore i need to type cast the object value returned by Distributed Map to type of common.session.Sess???? – user3454718 Jul 25 '14 at 08:45
  • In DistributedMap you can add any object, as far as it implements java.io.Serializable, or you can convert object into some other serializable form, such as JSON or XML. – Magic Wand Jul 25 '14 at 08:49
  • In other words, you have to cast Object into Sess when doing get() from DistributedMap. – Magic Wand Jul 25 '14 at 08:50
  • Can you show as an example to cast Oject into Sess when doing get() from DistributedMap because when we have tried to cast the Object into Sess doing get() from SessionMap we get the following error java.lang.ClassCastException: common.session.Sess incompatible with common.session.Sess – user3454718 Jul 25 '14 at 09:07
  • I guess your code is OK, you still have problem with classloader. Did you check your EAR classloader policy? – Magic Wand Jul 25 '14 at 09:09
  • Try to change "Classloader Mode" into "PARENT_LAST" and "WAR Classloader policy" into "APPLICATION". If you don't know how to change classloader policy, check this link: http://pic.dhe.ibm.com/infocenter/rsahelp/v8/index.jsp?topic=%2Fcom.ibm.servertools.doc%2Ftopics%2Ftapplicationsv6.html – Magic Wand Jul 25 '14 at 09:12
  • okay we have changed the settings of the classloader. now how can we get session bean into 2nd web module. i.e. the code we are already using is sufficient i.e. using sessionMap().put("key","value") to set and sessionMap().get("key") to retrieve the session bean. NOTE - "value" is common session bean or we have to use some other methods and functionality? – user3454718 Jul 25 '14 at 09:57
  • Depends on what common session bean contains. If you have a need for other information, you can also add it into Map or you can add these properties into session bean. – Magic Wand Jul 25 '14 at 10:05
  • I guess you meant this: sessionMap.put(userName, sessionBean) to set and sessionMap.get(userName) to get sessionBean from sessionMap – Magic Wand Jul 25 '14 at 11:01
  • sessionBean(common.session.Sess) is a complete class which is of session scope,used in web project 1 to store values of the session and contains many components like List,Variables, etc. Can u suggest a way i can get this sessionBean in web project 2 and typecasted as common.session.Sess, as my code in web project 2 needs values stored in common.session.Sess – user3454718 Jul 26 '14 at 04:11
  • after changing WAR classloader policy to APPLICATION and than running the jsf page on server gives following error Application was not properly initialized at startup, could not find Factory: javax.faces.render.RenderKitFactory – user3454718 Jul 26 '14 at 06:16
  • There is obviously a conflict between JSF JARs. Please check JSF versions you provide with both WARs. Which version of JSF are you using: the one embedded into WebSphere, or an external one? Do you package JSF JARs into WEB-INF/lib of both WARs? If you have common JARs it would be better if you can add it on EAR level and create "skinny" WARs (in other words, WAR-s with empty WEB-INF/lib if possible). – Magic Wand Jul 26 '14 at 20:55
  • We are using same version of jsf in both WARs and also we are using the JSF which is embedded into WebSphere, also we have packaged JSF JARs into WEB-INF/lib of both WARs, than also we are facing the same problem Application was not properly initialized at startup, could not find Factory: javax.faces.render.RenderKitFactory – user3454718 Aug 14 '14 at 08:03
  • error:- Application was not properly initialized at startup, could not find Factory: javax.faces.context.FacesContextFactory – user3454718 Aug 14 '14 at 08:19
  • You couls try to add listener com.sun.faces.config.ConfigureListener into web.xml as explained here: http://stackoverflow.com/questions/7886035/could-not-find-factory-javax-faces-context-facescontextfactory – Magic Wand Aug 18 '14 at 07:48
0

Take a look at this page - Assembling so that session data can be shared , it describes all the steps and configurations that needs to be done to allow session sharing between applications. In general you need to:

  • Create isolated shared library with all objects placed in session
  • In application retrieve the session object

    HttpSession session = request.getSession();

  • Cast this object to an IBMSession object and call the getIBMApplicationSession method.

    IBMApplicationSession appSession = ((IBMSession)session).getIBMApplicationSession();

  • Use the appSession like a normal session object.

  • Configure application to use shared session extension - create ibm-application-ext.xml with following attribute

<?xml version="1.0" encoding="UTF-8"?>

<application-ext xmlns="http://websphere.ibm.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://websphere.ibm.com/xml/ns/javaee http://websphere.ibm.com/xml/ns/javaee/ibm-application-ext_1_1.xsd" version="1.1">
   <shared-session-context value="true"/>
</application-ext>
  • Map shared library to your application during installation
Gas
  • 17,601
  • 4
  • 46
  • 93
  • In our case we are not using Http Servelet. We have session managed bean which store all the session values for example login session store all the login details. now we need to use this sesseion bean i.e login session bean into another web module. so for that we have used sessionMap() and from it we where able to tranfer values i.e. String values, but when we try to send the common session bean and then retrive that session bean in another web module it give error java.lang.ClassCastException: common.session.Sess incompatible with common.session.Sess – user3454718 Jul 25 '14 at 09:50
  • Try to add that common.session.Sess class to the library and put it in at the ear level, not in each web application. Did you try as Miljenko suggested to change class loader policy to Application, not module? – Gas Jul 25 '14 at 10:12