Saturday, May 2, 2009

Object Passing between JSP's and Normal Java Classes

Recently I was assigned to work with Java Web Component stuff and I was encountered a problem when I was developing new Web Services Component to WSO2 registry. Let me explain my problem....
I have developed a web Service and generated the code for the client side. If you have a look on my JSP code it's calling the method getProfile by passing arguments path,data,session. My requirement in this method is calling the web service client and get the output of the Web service in to the variable data. When I debug the class GetProfileUtil I notice the expected behaviour of variable data but when the JSP runs it throws a Null Pointer Exception when call the get method of the data(Map>). So the modifications happen inside the method getProfile to the variable data is not visible to the JSP.

String path =request.getParameter("path");
Map defaultprofile = null;
Map> data = null;

try {
GetProfileUtil.getProfile(path,data,config,session);
defaultprofile = data.get(UserCoreConstants.DEFAULT_PROFILE);
} catch (Exception e) {
CarbonUIMessage uiMsg = new CarbonUIMessage(CarbonUIMessage.ERROR, e.getMessage(), e);
session.setAttribute(CarbonUIMessage.ID, uiMsg);
}

But when I modify my method like this to create the object inside the method and return it, it worked fine. I simply avoid passing the variabl in to the method getProfile but rather create a data variable and return it.

String path =request.getParameter("path");
Map defaultprofile = null;
Map> data = null;

try {
data =GetProfileUtil.getProfile(path,config,session);
defaultprofile = data.get(UserCoreConstants.DEFAULT_PROFILE);
} catch (Exception e) {
CarbonUIMessage uiMsg = new CarbonUIMessage(CarbonUIMessage.ERROR, e.getMessage(), e);
session.setAttribute(CarbonUIMessage.ID, uiMsg);
}

when I access the bean variable I got all the modifications happen inside the method.
Is this the typical behaviour of JSP's and it's totally different from the way Java hadle object passing between classes and methods.
As far as I understand only wrong thing I have done in my first case is pointing to a null value for data variable....Otherwise it should work...I don't know that approach might be a worst thing to do...

No comments :