• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Problem geeting value in JSP after type casting Gurus Please help me

 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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

//super Class
public class Vehicle {

private String engine;
private Object mObj;

public void setMObj(Object mObj){
this.mObj = mObj;
}
public Object getMObj(){
return this.mObj;
}

public void setEngine(String Engine) {
this.engine = engine;
}
public String getEngine() {
return this.engine;
}
}
//child class
public class Car extends Vehicle {
public void setMake(String make) {
this.make = make;
}
public String getMake() {
return this.make;
}
public void setModel(String model) {
this.model = model;
}
public String getModel() {
return this.model;
}
}
//servlet
public class TestServlet extends HttpServlet {
ArrayList list =new ArrayList();

while (rs.next()) {

//creating an instance of sub class

Car car = new Car();

car.setMake(rs.getString("Make"));
car.setEngine(rs.getString("Engine"));
car.setModel(rs.getString("Model");

//creating an instance of super class

Vehicle veh =new Vehicle();


veh.setMObj(car);

list.add(veh);

}//while
session.setAttribute("LIST", list);
RequestDispatcher rd = getServletContext().getRequestDispatcher("/jsp/test.jsp");
rd.forward(req, res);

}
<%
Vehicle veh = new Vehicle();


ArrayList list = (ArrayList) session.getAttribute("LIST");



if (list != null )
{
for (int i = 0; i < list.size(); i++)
{


Car car = ((Car)veh.getMObj()) list.get(i);


%>
 
<table border="0" width="64%">
<tr>
<td width="12%">
<p align="left"><font color="#000080"><b>Cycle :</b></font></td>
<td width="6%"><b><%=car.getMake()%></b></td>


Thank you so much for your help I really appreciate it !!!
Maria
 
Ranch Hand
Posts: 1873
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Maria Peter
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 405
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
bacon. tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic