Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
Hi,
I'm trying to pass information from a database to a jsp page through a Vector.
************************* Servlet code*********************
...
ResultSet rs = stmt.executeQuery(sql);
//Create a Vector object to store the results
Vector vResults = new Vector();
memoryBean beany = new memoryBean();
//Add the data to the Vector
while(rs.next()) {
beany = new memoryBean();
beany.setPart_ID(rs.getString("Comments"));
vResults.addElement(beany);
}//Change the message if no records
//were returned.
if (vResults.isEmpty()) {
msg = "No Records Returned";
} else {
//Store the Vector and message in the
//HttpSession object so the jsp can
//display them.
msg = "Records Found:";
session.putValue("results",vResults);
}
session.putValue("message",msg);...
**********************************************************
(I am aware that putValue is deprecated but when I try to run with setAttribute(), I get ajava.lang.NoSuchMethodError: javax.servlet.http.HttpSession: method setAttribute(Ljava/lang/String;Ljava/lang/Object;)V not found
in my error log if I use it but putValue() seems to work fine.)
**********************memoryBean code********************
package beans;
public class memoryBean {private String Part_ID = "";
public memoryBean(String Part_ID) {
this.Part_ID = Part_ID;
}public memoryBean() {
this.Part_ID = "";
}public void setPart_ID(String Part_ID) {
this.Part_ID = Part_ID;
}public String getPart_ID() {
return Part_ID;
}
}**********************************************************
*************************JSP Code*************************
<%@page contentType="text/html"%>
<HTML>
<HEAD>
<TITLE> JDBC Servlet/JSP Example </TITLE>
</HEAD>
<BODY>
<%@ page import="beans.memoryBean" %>
<%@ page import="java.util.Vector" %>
<%-- < jsp:useBean id="beanInstanceName" scope="session"
class="package.class" / > --%>
<%-- < jsp:getProperty name="beanInstanceName"
property="propertyName" / > --%>
<H1> JDBC Servlet/JSP Example </H1>
<H2> <%= session.getValue("message") %>
</H2>
<%Vector vData = (Vector)session.getValue("results");
memoryBean mb;%>
<UL>
<%String n = "";
for (int i = 0; i < vData.size(); i++) {
mb = (beans.memoryBean)(vData.get(i));
%>
<LI>
<%= (String) mb.getPart_ID() %>
<% } // end for
%>
</UL>
</BODY>
</HTML>**********************************************************
**************************Page Returned*******************
Exception:
java.lang.ClassCastException: beans.memoryBean
at _memory._search._jspService(Compiled Code)
at oracle.jsp.runtime.HttpJsp.service(Compiled Code)
at oracle.jsp.app.JspApplication.dispatchRequest(Compiled Code)
at oracle.jsp.JspServlet.doDispatch(Compiled Code)
at oracle.jsp.JspServlet.internalService(Compiled Code)
at oracle.jsp.JspServlet.service(Compiled Code)
at javax.servlet.http.HttpServlet.service(Compiled Code)
at org.apache.jserv.JServConnection.processRequest(Compiled Code)
at org.apache.jserv.JServConnection.run(Compiled Code)
at java.lang.Thread.run(Compiled Code)**********************************************************
Funny thing is that when I just print out the Object in the Vector as strings:
**************************JSP Code**************************
<%@page contentType="text/html"%>
<HTML>
<HEAD>
<TITLE> JDBC Servlet/JSP Example </TITLE>
</HEAD>
<BODY>
<%@ page import="beans.memoryBean" %>
<%@ page import="java.util.Vector" %>
<%-- < jsp:useBean id="beanInstanceName" scope="session"
class="package.class" / > --%>
<%-- < jsp:getProperty name="beanInstanceName"
property="propertyName" / > --%>
<H1> JDBC Servlet/JSP Example </H1>
<H2> <%= session.getValue("message") %>
</H2>
<%Vector vData = (Vector)session.getValue("results");
%>
<UL>
<%String n = "";
for (int i = 0; i < vData.size(); i++) {
%>
<LI>
<%= (vData.get(i)).toString() %>
<% } // end for
%>
</UL>
</BODY>
</HTML>**********************************************************
I am returned a page that looks like this:
*************************Page Returned********************
JDBC Servlet/JSP Example
Records Found:* beans.memoryBean@70f941
* beans.memoryBean@fc3132
* beans.memoryBean@101e0b0**********************************************************
Which is encouraging because the message ("Records Found:") is passed up and it looks like there are three beans.memoryBean objects in the Vector which is what I am expecting.
Anyone have any idea why I am getting a java.lang.ClassCastException?
Thanks.
- Linus

Noticed a few things..
1) Do you really need to use the session? why not just use the Request object? Also, no matter which one you use, you should use setAttribute to bind the object to the Request or Session and getAttribute to retrieve it.
2) You could simplify your jsp to this..
< jsp:useBean name="beanVect" id="requestBean" scope="session"
class="java.util.Vector" />//should probably not use request as the beans id.
Enumeration enum=beanVect.elements();
while(enum.hasMoreElements()){
out.print((beans.memoryBean)enum.nextElement().getPart_ID);
}This should work.. you may need to mess with it, I almost always screw up code the first time around(that, and my java's really rusty right now)! Anyways, I think this is what is messing up your page...
vData.get(i)).toString()
Looks like you're trying to convert a bean instance straight to a string... when you already have a method that will return the string.
Hope this helps,
Greg

Thanks for the reply.
I am using putValue() because I keep getting the error:
java.lang.NoSuchMethodError: javax.servlet.http.HttpServletRequest: method setAttribute(Ljava/lang/String;Ljava/lang/Object;)V not found
at java.lang.Throwable.fillInStackTrace(Native Method)
at java.lang.Throwable.fillInStackTrace(Compiled Code)
at org.apache.jserv.JServConnection.processRequest(Compiled Code)
at org.apache.jserv.JServConnection.run(Compiled Code)
at java.lang.Thread.run(Compiled Code)in the Apache error log. It's strange because when I put in setAttribute() I can compile the code just fine but when I open my jsp page, the error shows up.
I am using Oracle 9i w/
Oracle HTTP Server Powered by Apache 1.3.12 (Unix)
ApacheJServ 1.1
mod_perl 1.24Not really sure what's going on here.
Any suggestions would be much appreciated.
Thanks!

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |