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