Which servlet engine are you encountering these difficulties under? This is
important because they all implement class loaders differently.
In particular, does the problem occur under JSWDK? It doesn't use custom class
loaders, so any ClassCastException will be due to creating a bean with one class
and trying to access it with another -- like this, for example:
In the servlet:
Integer myInteger = new Integer(12345);
session.putValue("myBean", myInteger);
and in the JSP page:
<jsp:useBean id="myBean" class="java.lang.String" />
This will trigger a class cast exception at runtime (but will compile just fine),
as will any other case where the class of the object you stored and the class (or
type) you name in the useBean are not "assignment compatible".
Craig McClanahan
Pilli Sujata wrote:
> -----Original Message-----
> From: Sujata Pilli
> Sent: Wednesday, September 29, 1999 8:23 PM
> To: 'JSP'
> Subject: Please help! ClassCastException when sharing data across JSPs
> in sessions!
>
> I have MyBean which is in myBeanPkg and a myServlet in myServletPkg.
> myBeanPkg is in the classpath and hence is not dynamically reloadable
> as the servlet in myServletPkg. So, just for clarification, the
> ClassCastException
> is not because of the classpath problem which occurs most commonly.
> So,
>
> In myServlet I do the following:
>
> MyBean myBean = new MyBean(...);
> HttpSession session = request.getSession();
> session.putValue("myBean", myBean);
>
> In myJSP Page I do the following:
>
> <jsp:useBean id="myBean" scope="session" class="MyBean" />
>
> So this works fine.
> So In my first situation:
> I added the following to anotherJSP
> <jsp:useBean id="anotherBean" scope="request" class="AnotherBean" />
> <jsp:useBean id="myBean" scope="session" class="MyBean" />
>
> so I can use myBean.getName()
>
> (Basically I am displaying attributes like name etc on every page
> which is set in myBean)
>
> But I get a ClassCastException!
>
> So i tried the following.
>
> In myJSP Page I added a call to the bean property which returns a
> string as follows.
> <% session.putValue("name",myBean.getName())%>
>
> In anotherJSP page I did..
> <%=(String) session.getValue("property")%>
>
> When I load anotherJSP I still get the classic ClassCastException!
>
> So my question is why? Is it still because the ClassLoaders are different
> and hence it is not the same session?
> Is there any other alternative other than passing the name as a hidden
> variable and the servlet loads it
> into the request And anotherJSP accesses it from the request?
>
> I am using JRun 2.3.3 build 153 w/ Apache Server.
> thanks
> Sujata
>
> -----Original Message-----
> From: Craig R. McClanahan [mailto:[EMAIL PROTECTED]]
> Sent: Wednesday, September 29, 1999 5:13 PM
> To: [EMAIL PROTECTED]
> Subject: Re: Passing Objects between Servlets...
>
> Patrick Deloulay wrote:
>
> > in the more recent servlet specs (2.2?), the use of
> >
> > public Servlet ServletContext.getServlet(String servletName) has been
> > deprecated.
> >
> > I am right now heavily using this API to pass objects around betwen
> > servlets.. I am learning/moving to the JSP API and I was wondering how I
> > could perform the same kind of operations...
> >
> > Any ideas... Thanks for your always helpful input.
> >
>
> Since JSPs are based on servlets, you're going to run into the same
> restrictions there. There is no longer any legal mechanism to get a
> reference
> to an instance of another servlet (for valid security-related reasons), so
> you
> can no longer use instance variables or public methods in servlets to share
> things.
>
> I share things in three different ways based on the "lifetime" and
> "visibility" of the objects that is needed. In all cases, such objects are
> equally visible to servlets and JSP pages, so I illustrate the syntax to
> store
> an object with a servlet, and access it as a bean in a JSP page.
>
> * Lifetime is just this single request, visible only to the servlets
> and JSP pages that need to process this request (typically you
> would be using RequestDispatcher.include or RequestDispatcher.forward
> for servlets, or <jsp:include> or <jsp:forward> in JSP pages):
>
> Servlet:
>
> MyBean myBean = new MyBean(...);
> request.setAttribute("myBean", myBean);
>
> JSP Page:
>
> <jsp:useBean id="myBean" scope="request" class="MyBean" />
>
> * Lifetime is more than one request for the same user, but not
> a globally shared variable:
>
> Servlet:
>
> MyBean myBean = new MyBean(...);
> HttpSession session = request.getSession();
> session.putValue("myBean", myBean);
>
> JSP Page:
>
> <jsp:useBean id="myBean" scope="session" class="MyBean" />
>
> * LIfetime is the entire application, and the variable needs to be
> shared across multiple users (like a database connection pool):
>
> Servlet:
>
> MyBean myBean = new MyBean(...);
> getServletContext().setAttribute("myBean", myBean);
>
> JSP Page:
>
> <jsp:useBean id="myBean" scope="application" class="MyBean" />
>
> > Patrick
> >
>
> Craig
>
> ===========================================================================
> To unsubscribe: mailto [EMAIL PROTECTED] with body: "signoff
> JSP-INTEREST".
> FAQs on JSP can be found at:
> http://java.sun.com/products/jsp/faq.html
> http://www.esperanto.org.nz/jsp/jspfaq.html
>
> ===========================================================================
> To unsubscribe: mailto [EMAIL PROTECTED] with body: "signoff JSP-INTEREST".
> FAQs on JSP can be found at:
> http://java.sun.com/products/jsp/faq.html
> http://www.esperanto.org.nz/jsp/jspfaq.html
===========================================================================
To unsubscribe: mailto [EMAIL PROTECTED] with body: "signoff JSP-INTEREST".
FAQs on JSP can be found at:
http://java.sun.com/products/jsp/faq.html
http://www.esperanto.org.nz/jsp/jspfaq.html