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
> {
> public void doGet( HttpServletRequest req, HttpServletResponse res )
> throws ServletException, IOException
> {
> // Get the current session object, create one if necessary
> HttpSession session = req.getSession(true);
>
> String action = req.getParameter("action");
> LocationModel model = (LocationModel)
> session.getAttribute("location_model"); /* at
>LocationController.doGet(LocationController.java:62) */
> if ( model == null ) {
> // If the session does not have a model then create
> // a new one and add it to the session
> model = new LocationModel();
> session.setAttribute("location_model", model );
> }
>
> // ....
>
> RequestDispatcher rd =
>getServletContext().getRequestDispatcher("locationview.jsp");
> rd.forward( req, res );
> }
>
> I have also put the model in the session inside the JSP (locationview.jsp) and
> add a form with action that calls "/servlet/LocationController"
>
> <%
> LocationModel model = (LocationModel)
> session.getAttribute("location_model");
> if ( model == null ) {
> model = new LocationModel();
> session.setAttribute("location_model", model );
> }
> %.
>
> Why do I get a class cast exception?
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.
> Peter Pilgrim
>
Craig McClanahan
===========================================================================
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