Link to home
Get AccessLog in
Avatar of dyma82
dyma82

asked on

<jsp:useBean> Not working after Migration

I migrated a J2EE application from Websphere v4 to Websphere v5.1. This involves migrating from J2EE 1.2 to J2EE 1.3.

My application uses Struts, so it has a FrontController servlet wich intercept every request. Then, it prepares everything the requested JSP will need to render itself.
As part of this process the FrontController servlet (Wich will be loaded at startup) loads a bean into the ServletContext. Later on, all JSPs will use this bean to acquire some of the text they need to render.

This is the code I use:

Snipped where the FrontController loads the bean:

public static void loadSharedResources(ServletContext sc) throws NamingException, RemoteException, FinderException
{
      TEXT_RESOURCES = new TextResources(SERVER_PROPERTIES);
      (sc).setAttribute("textResources",TEXT_RESOURCES);
      servletContext = sc;
}

Snipped where my JSP tries to use the loaded bean:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML>
<HEAD>    
<%@ page            
language="java"
contentType="text/html; charset=ISO-8859-1"  
import="gov.mdt.utils.GlobalPointers,
            gov.mdt.exceptions.SystemLockException,
            org.apache.struts.action.ActionErrors,
            gov.mdt.utils.ViewResources,
            java.util.Enumeration"
errorPage="ErrorPage.jsp"
%>
<jsp:useBean id="textResources" class="gov.mdt.utils.TextResources"
      scope="application"></jsp:useBean>

Before the migration the <jsp:useBean id="textResources"…> was working fine. After the migration I am getting the following error:

An error occurred at line: 19 in the jsp file: /StoreFloor.jsp

Generated servlet error:
C:\projects\Java\MDC2-5.1\.metadata\.plugins\com.ibm.etools.server.core\tmp1\cache\localhost\server1\MDCT\MDCTWeb.war\_StoreFloor.java:156: cannot resolve symbol
symbol  : constructor TextResources &#40;&#41;
location: class gov.mdt.utils.TextResources
            textResources =  new gov.mdt.utils.TextResources&#40;&#41;&#59;
                             ^
It seems that for some weird reason the tag <jsp:useBean id="textResources"…> is not finding my bean in the ServletContext, so it is trying to construct a new one by calling the default constructor, which my bean does not have. Of course, this bean is not mean to be created by the JSP page. It is created only once when the application starts and store in the ServletContext. I verified that it was in the ServletContext and that it wasn’t null. Still the <jsp:useBean id="textResources"…> could not find it.

Any help with this problem????
Thanks a lot for your help.
Avatar of damonf
damonf

>It seems that for some weird reason the

your explanation is something that would happen at runtime.  The error is a compile time error.  So something else is going on.  I suggest looking at the source of the generated servlet.  This may give you a clue of what's happening.
Avatar of dyma82

ASKER

Well.....yes and no....This error happens at runtime. When you call a JSP page it is compiled right there. Of course, at this point the application is already running, and in my case the "textResources" instance is already store in the ServletContext. The error says "compile error", but it happen at runtime not a compilation time.

Supposely, as far as I know the  <jsp:useBean id="textResources"…scope="application"> tag will check if the object "textResources" is in the ServletContext. If that were the case a local reference to this existing object would be created for this JSP page. Otherwise, it will try to create the object by calling the default constructor. In my application there is no way for the second action (call the default constructor) to be ever performed since there is a FrontController servlet intersecting every request and placing the necessary objects in the ServletContext.

That's why I think that the <jsp:useBean id="textResources"…scope="application"> tag is not working properly for my app.
 
ASKER CERTIFIED SOLUTION
Avatar of damonf
damonf

Link to home
membership
This content is only available to members.
To access this content, you must be a member of Experts Exchange.
Get Access
Avatar of dyma82

ASKER

It does NOT just checks the classpaths and compiles!

This is my tag definition:
<jsp:useBean id="textResources" class="gov.mdt.utils.TextResources" scope="application"></jsp:useBean>

When the JSP page carrying this tag is compiled on the spot, the compiler will check the ServletContext for an instance of "gov.mdt.utils.TextResources" named “textResources”. If it finds this object it will create a local reference to it named “textResources”, which will be available to this JSP page (servlet). If and only if it does NOT find any such instance in the servlet context, it will try to run the default constructor to create a new instance of this object.

Thus, I am leaning toward your second suggestion. However, I haven’t found any documentation saying that.

I am running the test you suggested today.
I am really appreciating your help!  
Avatar of dyma82

ASKER

My bad.
Things are as you are saying.
All that ServletContext check happens at runtime after the JSP has been compiled, but not during compilation.
Please disregard the initial part of my previous comment. (I did not sleep well yesterday... :) )

Still I think you are right on your second suggestion. I am running that test today.
Thanks
No offense taken.  Let me know what you find.

Avatar of dyma82

ASKER

Yes, definitely in the new Servlet 2.3 a validation is taking place at compilation time making sure the used bean is a real bean.
Thus, I add the default constructor and everything worked.