Thursday, October 20, 2011

Sharing HTTP session across web-apps in Oracle Weblogic 10.3



If you’re an experienced J2EE programmer, you probably already heard of the chance to use session sharing between two or more webapps. This feature is not part of the J2EE spec but has been implemented in several application servers like WebLogic, WebSphere or JBoss for example.
Although sharing session data is a Servlet 2.4 spec violation, and a potential security issue under some rare circumstances, there are some understable business cases to use it :
  • You may want to broke up a large application into multiple web-apps, so that new functionality can be pushed out in a more modularized way.
  • If you buy software to a company and that you want to integrate it seamlessly to your web application and make the two act as if you are facing a single application, this is the fastest & easiest way. It saves you from the pain of writing complex code to synchronize the sessions between the web-apps or even to exchange data between these web-apps.
Remember that the following will not apply directly to other application servers but may guide you towards the solution for these servers too.
Here’s how i made it work under WebLogic 10.3 :
My first guess was that i simply had to declare the web-app to use the shared session ability and that only the web-apps using this declaration would participate in this shared memory area. But this is not exactly how it works.
In fact, you need to build an EAR (Enterprise ARchive) out of your two web-apps, and declare in an additionnal configuration file that session sharing is enabled inside this entreprise application.
Let’s say that you have two web-apps which context paths are /poc1 and /poc2. First thing you have to do is reorganize your project to fit to the model of EAR’s. Here’s the final structure of our new EAR :
ear_schema
As you can see, there is a new meta-inf/application.xml file which purpose is to describe the application structure :
01.xml version="1.0" encoding="ISO-8859-1"?>
02.<application xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
06.id="Application_ID" version="5">
07. 
08.<display-name>Test ear appdisplay-name>
09. 
10.<module>
11.<web>
12.<web-uri>poc1web-uri>
13.<context-root>poc1context-root>
14.web>
15.module>
16.<module>
17.<web>
18.<web-uri>poc2web-uri>
19.<context-root>poc2context-root>
20.web>
21.module>
22.application>
You may already have noticed the weblogic-application.xml, this is the Weblogic specific file that contains the sharing session directive.
01.xml version="1.0" encoding="ISO-8859-1"?>
02.<wls:weblogic-applicationxmlns:wls="http://www.bea.com/ns/weblogic/weblogic-application"
05. 
06.<wls:session-descriptor>
07.<wls:persistent-store-type>memorywls:persistent-store-type>
08.<wls:sharing-enabled>truewls:sharing-enabled>
09.wls:session-descriptor>
10.wls:weblogic-application>
Now deploy this application using weblogic console. You can test that everything is running ok by using the read.jsp and set.jsp which allow to set a variable in the session in one web-app and read it from the other web-app just by accessing these in your browser. A typical test would be to open the following urls in two different browser’s windows :
Don’t forget to replace [serverhost:port] by your server name and port which should typically be localhost:7001
http://[serverhost:port]/poc1/set.jsp
http://[serverhost:port]/poc2/read.jsp
The second url should display the following page content :
Session attribute “myvar” is set to : toto1
Everything is working as intented, just don’t forget that you are using shared sessions. This could result in looooooooooooong debugging sessions because of variable names collisions between the web-apps which can be simply solved by prefixing the key of these session attributes with web-app related prefixes.
You can download an archive of the exploded demo EAR, if you want to test it by yourself.

2 comments:

  1. Hello, Can you please share another location of EAR.. its not visible.

    ReplyDelete