You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by Leon Palermo <le...@zedak.com> on 2001/05/25 15:01:51 UTC

(EXPERTS ONLY) Bean Creation in Dispatch Servlet

Re: problems installing tomcat on linuxHello All,

Let me preface this email by saying that I only put 'EXPERTS ONLY' so you hot shot programmers would actually read this email.  If you are reading this, it worked!

I have an odd problem that I was hoping someone could help with.

I have a servlet that all jsps in the system are dispatched from.  I create a bean in this servlet and add it to the request object like so...

    com.blah.blah.MyBean abean = (com.blah.blah.MyBean) Beans.instantiate(this.getClass().getClassLoader(),"com.blah.blah.MyBean");
    ...
    request.setAttribute("thename", abean);
                
I have also tried this to create the bean....

    com.blah.blah.MyBean abean = new com.blah.blah.MyBean();


and also tried to place the object in the request like so....

    pageContext.setAttribute("thename", abean, PageContext.REQUEST_SCOPE);

Anywho, I then have the following in my jsp page...

    <jsp:useBean id="thename" scope="request" class="com.blah.blah.MyBean" />

I get a java.lang.ClassCastException from the jsp.  So, I decided to do a little error hunting in a jsp using the following code...

<%try{
    System.out.println(request.getAttribute("thename") == null);
    System.out.println(request.getAttribute("thename") instanceof com.blah.blah.MyBean);
    System.out.println(request.getAttribute("thename").getClass().getName());
    System.out.println(zedak.docworx.jspsupport.beans.BrandBean)request.getAttribute("thename"));
}catch (ClassCastException e){
    System.out.println("CLASS CAST EXCEPTION!");
}
%>

The results of the code is as follows:

    false
    false
    com.blah.blah.MyBean
    CLASS CAST EXCEPTION!

So, the attribute is present in the request object, it is not an instance of 'com.blah.blah.MyBean'; but the object's class name is 'com.blah.blah.MyBean'.  Does anyone have an idea what is going on?  How can the object's class name be 'com.blah.blah.MyBean' but not be able to cast to 'com.blah.blah.MyBean'?

Thanks in advance!

Leon Palermo

Re: (EXPERTS ONLY) Bean Creation in Dispatch Servlet

Posted by Anne Dirkse <al...@uswest.net>.
Hi Leon --

I had something similar happen, and it occours when I recompile the 
bean and then try to access it without restarting Tomcat.
My *guess* is that classes that are reloaded (when they have been 
loaded once then recompiled) have a different classloader.
Anyhow, restarting Tomcat has always resolved that problem for me.

Hope that helps.
Anne

> Leon Palermo wrote:
> 
> Hello All,
> 
> Let me preface this email by saying that I only put 'EXPERTS ONLY' so
> you hot shot programmers would actually read this email.  If you are
> reading this, it worked!
> 
> I have an odd problem that I was hoping someone could help with.
> 
> I have a servlet that all jsps in the system are dispatched from.  I
> create a bean in this servlet and add it to the request object like
> so...
> 
>     com.blah.blah.MyBean abean = (com.blah.blah.MyBean)
> Beans.instantiate(this.getClass().getClassLoader(),"com.blah.blah.MyBean");
>     ...
>     request.setAttribute("thename", abean);
> 
> I have also tried this to create the bean....
> 
>     com.blah.blah.MyBean abean = new com.blah.blah.MyBean();
> 
> and also tried to place the object in the request like so....
> 
>     pageContext.setAttribute("thename", abean,
> PageContext.REQUEST_SCOPE);
> 
> Anywho, I then have the following in my jsp page...
> 
>     <jsp:useBean id="thename" scope="request"
> class="com.blah.blah.MyBean" />
> 
> I get a java.lang.ClassCastException from the jsp.  So, I decided to
> do a little error hunting in a jsp using the following code...
> 
> <%try{
>     System.out.println(request.getAttribute("thename") == null);
>     System.out.println(request.getAttribute("thename") instanceof
> com.blah.blah.MyBean);
> 
> System.out.println(request.getAttribute("thename").getClass().getName());
> 
> System.out.println(zedak.docworx.jspsupport.beans.BrandBean)request.getAttribute("thename"));
> }catch (ClassCastException e){
>     System.out.println("CLASS CAST EXCEPTION!");
> }
> %>
> 
> The results of the code is as follows:
> 
>     false
>     false
>     com.blah.blah.MyBean
>     CLASS CAST EXCEPTION!
> 
> So, the attribute is present in the request object, it is not an
> instance of 'com.blah.blah.MyBean'; but the object's class name is
> 'com.blah.blah.MyBean'.  Does anyone have an idea what is going on?
> How can the object's class name be 'com.blah.blah.MyBean' but not be
> able to cast to 'com.blah.blah.MyBean'?
> 
> Thanks in advance!
> 
> Leon Palermo

Re: (EXPERTS ONLY) Bean Creation in Dispatch Servlet

Posted by Guido Medina <gu...@ns2.iiac.net>.
Re: problems installing tomcat on linuxExcuse me, you have this: 

  System.out.println(zedak.docworx.jspsupport.beans.BrandBean)request.getAttribute("thename"));

before the catch statement, one question: Did you check very well what you wrote in that line ?

Guido.
  ----- Original Message ----- 
  From: Leon Palermo 
  To: tomcat-user@jakarta.apache.org 
  Sent: Friday, May 25, 2001 9:01 AM
  Subject: (EXPERTS ONLY) Bean Creation in Dispatch Servlet


  Hello All,

  Let me preface this email by saying that I only put 'EXPERTS ONLY' so you hot shot programmers would actually read this email.  If you are reading this, it worked!

  I have an odd problem that I was hoping someone could help with.

  I have a servlet that all jsps in the system are dispatched from.  I create a bean in this servlet and add it to the request object like so...

      com.blah.blah.MyBean abean = (com.blah.blah.MyBean) Beans.instantiate(this.getClass().getClassLoader(),"com.blah.blah.MyBean");
      ...
      request.setAttribute("thename", abean);
                  
  I have also tried this to create the bean....

      com.blah.blah.MyBean abean = new com.blah.blah.MyBean();


  and also tried to place the object in the request like so....

      pageContext.setAttribute("thename", abean, PageContext.REQUEST_SCOPE);

  Anywho, I then have the following in my jsp page...

      <jsp:useBean id="thename" scope="request" class="com.blah.blah.MyBean" />

  I get a java.lang.ClassCastException from the jsp.  So, I decided to do a little error hunting in a jsp using the following code...

  <%try{
      System.out.println(request.getAttribute("thename") == null);
      System.out.println(request.getAttribute("thename") instanceof com.blah.blah.MyBean);
      System.out.println(request.getAttribute("thename").getClass().getName());
      System.out.println(zedak.docworx.jspsupport.beans.BrandBean)request.getAttribute("thename"));
  }catch (ClassCastException e){
      System.out.println("CLASS CAST EXCEPTION!");
  }
  %>

  The results of the code is as follows:

      false
      false
      com.blah.blah.MyBean
      CLASS CAST EXCEPTION!

  So, the attribute is present in the request object, it is not an instance of 'com.blah.blah.MyBean'; but the object's class name is 'com.blah.blah.MyBean'.  Does anyone have an idea what is going on?  How can the object's class name be 'com.blah.blah.MyBean' but not be able to cast to 'com.blah.blah.MyBean'?

  Thanks in advance!

  Leon Palermo

Re: (EXPERTS ONLY) Bean Creation in Dispatch Servlet

Posted by Marco Baringer <ma...@convey.it>.
On Fri, May 25, 2001 at 09:01:51AM -0400, Leon Palermo wrote:
> Re: problems installing tomcat on linuxHello All,
> 
> Let me preface this email by saying that I only put 'EXPERTS ONLY' so you hot shot programmers would actually read this email.  If you are reading this, it worked!
> 
> I have an odd problem that I was hoping someone could help with.
> 
> I have a servlet that all jsps in the system are dispatched from.  I create a bean in this servlet and add it to the request object like so...
> 
>     com.blah.blah.MyBean abean = (com.blah.blah.MyBean) Beans.instantiate(this.getClass().getClassLoader(),"com.blah.blah.MyBean");
>     ...
>     request.setAttribute("thename", abean);
>                 
> I have also tried this to create the bean....
> 
>     com.blah.blah.MyBean abean = new com.blah.blah.MyBean();
> 
> 
> and also tried to place the object in the request like so....
> 
>     pageContext.setAttribute("thename", abean, PageContext.REQUEST_SCOPE);
> 
> Anywho, I then have the following in my jsp page...
> 
>     <jsp:useBean id="thename" scope="request" class="com.blah.blah.MyBean" />
> 
> I get a java.lang.ClassCastException from the jsp.  So, I decided to do a little error hunting in a jsp using the following code...
> 
> <%try{
>     System.out.println(request.getAttribute("thename") == null);
>     System.out.println(request.getAttribute("thename") instanceof com.blah.blah.MyBean);
>     System.out.println(request.getAttribute("thename").getClass().getName());
>     System.out.println(zedak.docworx.jspsupport.beans.BrandBean)request.getAttribute("thename"));
> }catch (ClassCastException e){
>     System.out.println("CLASS CAST EXCEPTION!");
> }
> %>
> 
> The results of the code is as follows:
> 
>     false
>     false
>     com.blah.blah.MyBean
>     CLASS CAST EXCEPTION!
> 
> So, the attribute is present in the request object, it is not an instance of 'com.blah.blah.MyBean'; but the object's class name is 'com.blah.blah.MyBean'.  Does anyone have an idea what is going on?  How can the object's class name be 'com.blah.blah.MyBean' but not be able to cast to 'com.blah.blah.MyBean'?
> 
> Thanks in advance!
> 
> Leon Palermo

I'm pretty sure you've got a precedence problem.

this code:
(com.blah.blah.MyBean)request.getAttribute("whatever");

will try to cast request to com.blah.blah.MyBean and then apply the
getAttribute generic function to that (this had better NOT work :).
try this:

(com.blah.blah.MyBean)(request.getAttribute("whatever"));

http://java.sun.com/docs/books/jls/index.html You want section 15.7,
Evaluation order. 

hope this helps.
-- 
-Marco
Ring the bells that still can ring.
Forget the perfect offering.
There's a crack in everything.
It's how the light gets in.
	-Isonard Cohen