Thanks Craig.

It works when the two classes are move "learn.jsp" package!

Yep. I have been reading "Web Dev with JSP" book which does explains Java Beans 
properly.

--
Peter Pilgrim
G.O.A.T
                    "The Greatest of All Time"



---------------------------------------- Message History 
----------------------------------------


From: "Craig R. McClanahan" <[EMAIL PROTECTED]>@java.sun.com> on 17/11/2000 
11:04 PST

Please respond to A mailing list about Java Server Pages specification and reference 
<[EMAIL PROTECTED]>

DELEGATED - Sent by:     A mailing list about Java Server Pages specification and 
[EMAIL PROTECTED]>


To:   [EMAIL PROTECTED]
cc:
Subject:  Re: Developing with Tomcat 3.2beta


Hi Peter, see below:

Peter Pilgrim wrote:

> I am trying to write a mini test MVC with JSP as a test with Tomcat 3.2B
> I wrote two classes :
>
>      <WEBAPP>/ROOT/WEB-INF/classes/LocationModel
>      <WEBAPP>/ROOT/WEB-INF/classes/LocationController
>
> I am creating a session object in both the JSP and the Servlet with
>
> public class LocationController extends HttpServlet
__<CUT>--

You get a class cast exception because of the way Java treats classes that are not 
defined in packages.

When you reference an unpackaged class name from another class, Java assumes that the 
other class is in the same package as the one you are compiling.  That works fine for 
your servlet, because it is correct.  However, the generated servlet that is created 
for your JSP pages is *not* in the unnamed package -- and,
what's worse, the package it ends up under is different for each servlet container.

The solution is to use a "package" declaration at the top of your two classes (you'll 
have to adjust where the .class files go accordingly), and then use fully qualified 
class names in the JSP page (or specify the correct import statement).  For example, 
assume that you put these two classes in a package named
"com.mycompany.mypackage".  To make your JSP page work, simply add the following to 
the page:

    <%@ page import="com.mycompany.mypackage" %>

Of course, you are still working too hard :-).  The JSP page will do what your 
scriptlet does if you use this approach:

    <jsp:useBean id="location_model" scope="session"
     class="com.mycompany.mypackage.LocationModel" />

This creates the session scope bean if it's not there already, *and* makes a local 
variable named "location_model" available that references this object.  The only 
"restriction" (if you want to call it that) is that the variable name and the session 
attribute key have to be the same, because they are both calculated
from the "id" attribute.


--<CUT>--


--

This e-mail may contain confidential and/or privileged information. If you are not the 
intended recipient (or have received this e-mail in error) please notify the sender 
immediately and destroy this e-mail. Any unauthorised copying, disclosure or 
distribution of the material in this e-mail is strictly forbidden.

===========================================================================
To unsubscribe: mailto [EMAIL PROTECTED] with body: "signoff JSP-INTEREST".
For digest: mailto [EMAIL PROTECTED] with body: "set JSP-INTEREST DIGEST".
Some relevant FAQs on JSP/Servlets can be found at:

 http://java.sun.com/products/jsp/faq.html
 http://www.esperanto.org.nz/jsp/jspfaq.html
 http://www.jguru.com/jguru/faq/faqpage.jsp?name=JSP
 http://www.jguru.com/jguru/faq/faqpage.jsp?name=Servlets

Reply via email to