Follow by Email

Sunday, May 8, 2011

Applet/Swing/Flex/J2ME to Servlet Communication Why I am losing JSESSIONID (on weblogic 11) and what's the solution.

We have an application which was built using Applet, Servlet and JSP technologies.
The Application has been on production since last 2-3 years and was working perfectly fine on WebLogic 10G. Recently we were asked to deploy our applications on WebLogic (10.3.3) 11g , and we found that the same application (Which is working fine on WebLogic 10.3.0.0) was crashing on WebLogic 10.3.3.
After doing some research and debugging code we found that the problem was actually with Applet Servlet communication as Applet was not passing JSESSIONID to servlet as a result the server was unable to find the Session associated to the client and application was crashing.
The confusing point was that the same code was working perfectly fine on older version i.e. WebLogic 10.3.0.0.
After digging the code we found that we were using java.net.URLConnection from Applet to connect to servlet and were assuming that this will take care of JSESSIONID while connecting the server, this assumption was right for WebLogic10.3.0.0 but not for WebLogic 10.3.3.
So the solution is to store the JSESSIONID at client end (APPLET) and always send the JSESSIONID from applet to servlet.
For reference you can see the following forum post
http://www.jguru.com/faq/view.jsp?EID=33978
And especially following post
I just did some tests, and it appears that using URLConnection() the applet is automatically returning the sessionId that the servlet sets (also automatically). That is, in both IE5 and NN4, without explicitly adding anything to the header the applet was sending back, the servlet was able to recognize the sessionId as the same across repeated calls from the same applet. So my guess is that if you run your applet from within one of these browsers, they automatically add in the cookies to all your URLConnections. Any naysayers?
And in response following post
And did you do tests for more versions of java (1.1.4, 1.3, 1.4.2)? Is this behavior described in Applet specification or is it an accident? If it is not a standard then it could stop work in future versions of Java
Note: If we send JSESSIONID with all the requests to servlets we can get rid of issue of finding no session on servlet end.

Summary:
When you communicate with a servlet from Applet/Swing or any other application and you face the problem of not finding the appropriate session on server you should first check the communication of JSESSIONID between your application and server and if your application is not properly sending JSESSION ID than you can do it yourself by rewriting JSESSIONID in URL. And it can also be a best practice if you take care of JSESSIONID communication you don’t have to relay/assume anything about the APIs you are using for your application to servlet connection and this solution will always work.

13 comments:

  1. I was doing some editing in my recent blog today and by mistake pressed publish button instead of save button and surprisingly I lost my blog and all the valuable comments on it. Luckily I was mentaining a word document for this blog so I am posting it again with a better subject (I Think :)).

    ReplyDelete
  2. First comment posted by Asem (http://www.blogger.com/profile/02010091141795542983)

    Asem why i can't see anything when I click on this link or you haven't written a blog yet?

    I'm questioning here again: why applets?

    ReplyDelete
  3. My reply

    Let me copy paste my answer to the same question on facebook Walaikum salam and good question :) Applets were used in an application which was developed in 2006 and we are still supporting it.I think flex or GWT could hv been better options for this project but don't know de reason why they didn't use those technologies. By the way Applet are yet not dead.

    ReplyDelete
  4. Asem all the technologies have their advantages and disadvantages , it's the role of the Architect who should clearly understand the requirements and decide best technologies to be used to a particular solution. In this project as far as I understand they wanted to develop a browser based application and since the requirement was to build a Gant chart like GUI plus different complex forms. Plus they had time limitations as well. It was clear that server will be J2EE based.
    Now there could be a lot of reasons to choose Applet e.g.
    1) They already had expertise in this domain and they felt it very easy to develop it as applet servlet solution.
    2) They may have thought about other options , but with all other non java options you will have to write your own communication layer (mostly xml based) between non java client and J2EE server , however if you are using Applet you can just write your objects on Servlet Input Stream and Servlet can read it as the same object so notice you don't have to waste your time writing communication layer and you get more time to concentrate of business problem.
    3) They have used JFreeChart in this project to draw Gant Charts which could be another reason of using Applets as JFreeChart was easy to use with Applets or modifying it was easy or they were comfortable with this type of development they may already have the experience of doing that. 4) We are talking about 2006 here , Flex was not that mature as it is these days and may be they didn't have expertise in Flex or flex was not good enough again.
    There could be a lot of other reasons. Hope you got my point or else we can discuss it more if you want

    ReplyDelete
  5. Asem's reply

    I thought we had a license of FusionCharts. BTW, I used java webstart, which was downloaded from a servlet, using the JFreeChart objects to draw the charts. However, the users had so many problems when upgrading from vista to windows 7, java upgrades, even weblogic changes. The users didn't like that, so we had to provide an alternative, which was FusionCharts. Since they have now a canvas library, if I were a good at JavaScript, I would pick that over Flex.

    ReplyDelete
  6. By Ibrahim Al Shehri

    It looks like SDS, i think they used Applet because they were challenged to come up with UI similar to C++ desktop app. they found JFreeChart to provide excellent support on this in addition to the good amount of customization that made the app really similar to the legacy app and support dynamic user interaction like copy/paste activity , drag n drop , etc .. , i don't think there were other good charting lib to use. BTW, they were doing the R&D on that even before 2006

    ReplyDelete
  7. This comment has been removed by the author.

    ReplyDelete
  8. I got the same problem while loading image using a servlet (image in database) with struts img tag as well.
    The solution was to write the following scriplet, so as to pass two parameter to img tag.

    <%
    HoleDetailsActionForm form= (HoleDetailsActionForm)request.getAttribute("holeDetailActionForm");
    HashMap params = new HashMap();
    params.put("reportId", form.getReportId()); // parameter 1 is the report Id, to get the image of the associated report
    params.put("sessionId", session.getId()); // parameter 2 session id
    pageContext.setAttribute("params", params);
    %>

    <html:img page="/getWellImageAction.do" name="params" width="400" height="550" style="border:none" />

    ReplyDelete
  9. params.put("sessionId", session.getId());

    I think JSESSIONID should be used instead of sessionid here , one strange thing I also noticed when I was implementing the solution I initially tried to send session id as jsessionId (as it was mentioned in the forum) but it didn’t work for weblogic 11g than I had to use JSESSIONID and it worked .

    ReplyDelete
  10. Couple of days ago I had a similar problem while migrating a php web application from Apche 1.3 to Apache 2.2 and Php 4.x to Php 5.x.
    Everything was working fine on older version but application crashed on new version of platform.
    After some research it was found that Apache was not setting PHPSESSID in cookie rather we have to pass the PHPSESSID in the url to work on new platform.
    So one solution was to pass PHPSESSID in url and the other more appropriate was to change a setting in php.ini file (session.use_only_cookies=On). This solved the problem without changing code.
    There may be similar configuration option in web-logic for cookies.

    ReplyDelete
  11. Yeah there is an option to set HTTP only session to true in weblogic , but the problem is if you need to migrate your application again to another webserver there you will may not get the same solution , One of my colleagues also suggested me to use that approach which will work provided the browser running your application has the HTTP only implementation.
    Therefore I implemented the most reliable solution which doesn't work on assumptions and which will always work.

    ReplyDelete
  12. I was facing the same problem when migrating from weblogic 9.2 to 10.3.5(11g) when I pass jsessionid in url when opening urlConnection it is wokring fine.
    good informative section .

    ReplyDelete