cancel
Showing results for 
Search instead for 
Did you mean: 

JSP Instantiate Session Bean After Load

Former Member
0 Kudos

Hi Everyone,

I'm having a problem using a session bean in my jsp.

Sometimes, after I upload a par file, the first time I run the jsp, the jsp instantiates the session bean instead of using the one stored in the session.

I know the bean was correctly instantiated in my DynPage code because I wrote some of its information back to the jsp using IPortalComponentResponse object and I can see those.

I'm wondering if I need to change a property on my portalapp.xml file or on the portal itself.

Here is the component definition in my portalapp.xml:

<component name="NewRequest">

<component-config>

<property name="ClassName" value="com.nexeninc.tots.user.newrequest.NewRequest"/>

<property name="ComponentType" value="none"/>

<property name="JSP" value="pagelet/NewRequest.jsp"/>

</component-config>

<component-profile>

<property name="tagLib" value="/SERVICE/htmlb/taglib/htmlb.tld"/>

</component-profile>

</component>

One more thing, the problem is not recurring which is giving me grief to identify why and when exactly it is happening.

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

Hi Alaa,

Just comment off this section

<property name="ComponentType" value="none"/>

<property name="JSP" value="pagelet/NewRequest.jsp"/>

in the portalapp.xml file.It will work, you will get the values of the bean instance.

Thanks

Manab

Portal Consultant

IBM Global Services India.

+91339830603327

Former Member
0 Kudos

Hi Manab,

I tried your suggestion but it didn't work.

FYI, I can read the information from my jsp when I refresh the page. It's just the first time I upload my par file that I get this strange behaviour from the jsp.

What makes it even more dazzling is that it does not happen consistently. It happens every two or three uploads. Any idea why?

Answers (3)

Answers (3)

Former Member
0 Kudos

Thanks Darrell, Francisco for your answers.

Darrell, I agree with you, putting a bean in the IPortalComponentSession should be fine. However the behaviour that I'm getting this time is strange.

In my doInitialization() i'm creating a new bean object and store it in the session. After that the code simply call doProcessBeforeOutput() and point to the correct jsp. Nothing special in there.

Here is my code to give you an idea of what I'm talking about:

public void doInitialization(){

componentSession = ((IPortalComponentRequest)getRequest()).getComponentSession();

componentSession.removeValue("newRequestBean");

newRequestBean = new NewRequestBean((IPortalComponentRequest)getRequest());

componentSession.putValue("newRequestBean",newRequestBean);

newRequestBean.appendDebugMessage("initialized page");

}

public void doProcessBeforeOutput() throws PageException {

if (newRequestBean.getState() == REQUEST) {

this.setJspName("NewRequest.jsp");

} else {

this.setJspName("RequestorSelection.jsp");

}

newRequestBean.appendDebugMessage("finished doProcessBeforeOutput");

}

}

The bean I initiated in the doInitialization() is loaded with data that will populate my jsp. When the jsp calls the default constructor to instantiate my bean, I lose all that data.

However, when I click to refresh the page, everything seems to work fine. The bean gets created just fine in the doInitialization(), loaded to the session and the jsp correctly reads that bean from the session instead of instantiating its own.

darrell_merryweather
Active Contributor
0 Kudos

Whats the code in your JSP page

D

Former Member
0 Kudos

The jsp code is too long so I'm posting the first part that include the declaration of the bean. Tell me if you want me to post the whole jsp.

<%@ taglib uri="tagLib" prefix="hbj" %>

<%@ page import="com.nexeninc.tots.user.newrequest.bean.*" %>

<%@ page import="com.sapportals.htmlb.enum.EventTrigger" %>

<%@ page import="java.util.Vector" %>

<jsp:useBean id="newRequestBean" scope="session" class="com.nexeninc.tots.user.newrequest.bean.NewRequestBean" />

<%! String newReqID; %>

<%! String newCalculatedDayOff; %>

<%! String startDateField; %>

<hbj:content id="myContext" >

<hbj:page title="PageTitle">

<hbj:form id="frm" >

<% if(newRequestBean.isDefaultConstructor()){

Vector errors = newRequestBean.getValidationErrors();

errors.add("The information couldn't be loaded, please refresh screen. "+

"If problem persists, contact your system administrator.");

newRequestBean.setValidationErrors(errors);

newRequestBean.setDefaultConstructor(false);

}

%>

Former Member
0 Kudos

Correct me if i'm wrong - but I think it is normal behaviour that the bean gets a new instance when you upload a new par file.

darrell_merryweather
Active Contributor
0 Kudos

Not strictly. When you upload a new par file, the object that is written into the IPortalComponentSession is invalidated. It still exists as an object which is not null, however, if you try and cast the object into your type variable, then you will get a ClassCastException, which really needs to be caught.

A new instance will then need to be created, and can only be created when the code is run. However, you can't predict when a garbage collection is run and this object is removed from the IPortalComponentProfile or IPortalComponentRequest. I have never had any problems with putting the object into the IPortalComponentSession though.

I hope this helps

Darrell

darrell_merryweather
Active Contributor
0 Kudos

This may because of the place that you are storing the Bean object. You should be ok to store the bean in the IPortalComponentSession, however, I have seen instances that when you store the bean in the IPortalComponentProfile or IPortalComponentRequest that the Bean object is garbage collected, which can lead to very strange results

I hope this helps

D