Wrox Home  
Search P2P Archive for: Go

  Return to Index  

pro_jsp thread: Problems using Java Beans with JSP


Message #1 by "Lichty, Kent" <Kent.Lichty@d...> on Mon, 1 Apr 2002 10:34:55 -0600
Hi

Does your bean implements   Serializable ....


Julio Oliveira
Buenos Aires - Argentina
------------------------
Oliveira_Julio@y...
ICQ  65689456




-----Mensaje original-----
De: Lichty, Kent [mailto:Kent.Lichty@d...]
Enviado el: Lunes, 01 de Abril de 2002 01:35 p.m.
Para: Pro_JavaServer_Pages
Asunto: [pro_jsp] Problems using Java Beans with JSP




Is it just me, or have others of you out there discovered that Tomcat
sometimes just cant handle some situations where a JSP is using a Java Bean,
particularly where multiple JSP pages are attempting to use the same Java
Bean.  I am confident that all of my syntax is correct and that my bean is
located in the correct directory; sometimes Tomcat just cant seem to handle
it and it's very frustrating.  Here is just ONE specific example:

JSP A and JSP B both use a common java bean X.  If I access JSP A FIRST, and
then call JSP B, then everything works fine.  However, if I call JSP B
FIRST, WITHOUT calling JSP A, then I get a runtime error like this:

Apache Tomcat/4.0.1 - HTTP Status 500 - Internal Server Error

type Exception report
message Internal Server Error
description The server encountered an internal error (Internal Server Error)
that prevented it from fulfilling this request.
exception
javax.servlet.ServletException: class com.dsionline.apps.rain.web.facadebean
: java.lang.NullPointerException
	at
org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImp
l.java:463)
	at org.apache.jsp.WebSearch$jsp._jspService(WebSearch$jsp.java:270)
	at
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:107)


This refers to the following section of code in JSP B:


       } catch (Throwable t) {
            if (out != null && out.getBufferSize() != 0)
                out.clearBuffer();
       LINE 270:     if (pageContext != null)
pageContext.handlePageException(t);
        } finally {
            if (_jspxFactory != null)
_jspxFactory.releasePageContext(pageContext);
        }
    }
}

This is some error handling stuff in my JSP B which I have no control over!

root cause
java.lang.InstantiationException: class
com.dsionline.apps.rain.web.facadebean : java.lang.NullPointerException
	at org.apache.jsp.WebSearch$jsp._jspService(WebSearch$jsp.java:85)
	at
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:107)


I have found that this general problem is ALWAYS where it tries to
instantiate the JavaBean!  It just cant seem to handle it sometimes, while
other times it can!  Line 85 is identified. Why cant it easily find the bean
when I use JSP A, but not for the JSP B?  My bean is defined the same way in
BOTH JSPs, as a simple session bean?


boolean _jspx_specialFacadebean  = false;
                 synchronized (session) {
                    Facadebean= (com.dsionline.apps.rain.web.facadebean)

pageContext.getAttribute("Facadebean",PageContext.SESSION_SCOPE);
                    if ( Facadebean == null ) {
                        _jspx_specialFacadebean = true;
                        try {
                            Facadebean 
(com.dsionline.apps.rain.web.facadebean)
java.beans.Beans.instantiate(this.getClass().getClassLoader(),
"com.dsionline.apps.rain.web.facadebean");
                        } catch (ClassNotFoundException exc) {
                            LINE 85 throw new
InstantiationException(exc.getMessage());
                        } catch (Exception exc) {
                             throw new ServletException (" Cannot create
bean of class "+"com.dsionline.apps.rain.web.facadebean", exc);
                        }
                        pageContext.setAttribute("Facadebean", Facadebean,
PageContext.SESSION_SCOPE);
                    }
                 }

My Java Bean is perfectly well defined and it exists in the correct
directory.  It is defined like this in BOTH JSPs:

<jsp:useBean id="Facadebean" scope="session"
class="com.dsionline.apps.rain.web.facadebean">
</jsp:useBean>

This problem is very frustrating because it simply has no rhyme or reason:
sometimes the container is able to locate and instantiate the JavaBean and
sometimes it simply cannot! I can also cause similar behavior when I have
two JSPs that are contained in two separate frames that load together.  If I
reference the bean in both JSP pages, the page that loads second also
encounters this error.  Maybe it is because the code above is synchronized.

Well, I was just wondering if any of you others have experienced similar
problems using JSPs and JavaBeans.  I am just about ready to give up on JSPs
and just start using servlets, at least I can control the object that is
created and not rely on the container!! Any help would be really
appreciated! Thanks!





----------  Internet E-mail Confidentiality Disclaimer  ----------

PRIVILEGED / CONFIDENTIAL INFORMATION may be contained in this message.  If
you are not the addressee indicated in this message or the employee or agent
responsible for delivering it to the addressee, you are hereby on notice
that you are in possession of confidential and privileged information.  Any
dissemination, distribution, or copying of this e-mail is strictly
prohibited.  In such case, you should destroy this message and kindly notify
the sender by reply e-mail.  Please advise immediately if you or your
employer do not consent to Internet email for messages of this kind.

Opinions, conclusions, and other information in this message that do not
relate to the official business of my firm shall be understood as neither
given nor endorsed by it.





  Return to Index