I am trying to get the data in a jsp through session object. Class Car extends Vehicle and Vehicle class has get and set method to set and get an Object In TestServlet i am retrieving the vales from the database and storing it in a array list and forwarding request to a jsp using Request dispatcher. And in my JSP i am trying to access the value of both my super class(Vehicle) and sub class(Car) by type Casting but i get an error message as org.apache.jasper.JasperException at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper .java:254) at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:2 95) at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:241) at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
I pretty new to this So Gurus Please help me
I have my code sample below and please let me know is there any easier way to achieve same result
Hi Maria Well, it seems that you are putting "Vehicle" object and trying to cast to "Car" object when you get data back in JSP. You seem confused about inheritance here. Here, in your case, you are creating Car object that inherits Vehicle object but in the code you say, Vehical veh = new Vehicle(); veh.set(); // whatever that set object method is Instead you should say, Vehicle veh = new Car(); // and then whatever methods are This way you should have the code working. Here, even if Car extends Vehicle you don't really use that inheritance relationship as you create "Vehicle" object to put into the Vector instead of Car. Hope this throws some light for you. Regards Maulin
Thank you so much for your help I am not very familiar with this object oriented concept. I am just trying to get the values in my jsp from the super class I tried this in my servlet and it works fine Vehicle veh = new Vehicle()' Car car = (Car)veh.getMObj(); System.out.println(car.getVehicle()); System.out.println(car.getMake()); System.out.println(car.getModel()); I am confused when it comes to JSP because i am using an arraylist to store the values. and I am not able to get data by doing this Car car = ((Car)veh.getMObj()) list.get(i); Before i tried couple of JSP without inheritance by type casting array list to car class
for (int i = 0; i < list.size(); i++) { Car car = (Car) list.get(i);
%> and it perfectly works fine. But i am not able to understand how to get it working with inheritance. Thanks Maria
I will try to give you the short version and the long version of what I think is the answer. The short version: This should solve your problem ...
The long version: I believe you have some misunderstandings as far as OO principles are concerned i.e. inheritance and polymorphism. This is something new to those learning an OO language such as Java. It took me awhile to learn and I am still learning. There are several books on the market some free and some not, that will give you a nice definition of what inheritance and polymorphism mean, but if you are like me, just pretend I am from Missouri, and just "SHOW ME". I had this code example still around and I believe it may help you in understanding OO principles or at least give you another way at looking at your example. I believe it came from Thinking In Java from Bruce Eckel and there are other good books out there. It is along the same lines as the Shape and Circle examples alot of the books use as an example of OO programming. If you need more help in understanding OO principles, I believe the Java beginner or the Advanced forum may also be a great place to learn.
I hope this helps. Craig.
Maria Peter
Greenhorn
Joined: Oct 27, 2003
Posts: 29
posted
0
Craig, Thank you so much for your help. Its working now. As i mentioned earlier i am very new to this concept and i am trying to learn and implement things. And some time i face these kind of problems Once again thank you so much for your help I really appreciate it Maria