• 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
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

ClassCastException pulling list from session

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
I'm trying to create a very simple app to learn jsps and servlets. I have 2 jsps and 1 servlet, along with various java classes.

The first jsp UserEntryJsp asks the user to input basic info like name, age for search criteria. The FORM ACTION from this jsp goes to the UserEntryServlet.

The UserEntryServlet instantiates a user, calls a getUserList method which returns an ArrayList from a MySQL database.


The UserExitJsp is supposed to display the List of users matching the search criteria. I've added tons of System.out.printlns to the servlets, jsps, etc. so I can use the console to see what is happening and not happening. My problem is when I try to retrieve the ArrayList from session and cast it to a List.




I'm using Eclipse, and the message I get is
org.apache.jasper.JasperException: Exception in JSP: /jsps/UserExitJsp.jsp:9
It indicates the line above as the problem. It lists a root cause of:
java.lang.ClassCastException

I've searched on this and other sites and found the frequent answer that the code should be something like this:

[code]
<%List<String> l = new ArrayList<String>((ArrayList<String> session.getAttribute("userList")); %>
[\code]

I've tried this and get an error ArrayList cannot be resolved to a type.

Thanks in advance for any help!
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Wendy,

Welcome to JavaRanch!

Let's get the easy stuff out of the way first: in the first code snippets above, the List is in a variable "l" when you create it, and "u" when you store it in the session. Is that right? Is it just a typo in copying the code, or perhaps you're not actually storing what you think you are?

If it's not that, then try something like this:

<% Object o = request.getSession().getAttribute("userList");
out.println(o.getClass().getName());
%>

This will tell you what kind of object is actually stored in the session.
 
Sheriff
Posts: 22850
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ArrayList has to be imported, therefore the ArrayList error.

Are you importing java.awt by any chance? That also has a List class. What happens if you specify the full type: java.util.List list = (java.util.List)...?
 
Wendy Lee
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ernest & Rob -- Thank you so much!!

As you suspected, turns out that I wasn't putting the list in session as I thought. I was putting the user. The code snippet to see what kind of object was stored in session was very helpful (I'll keep if for future use.) Since that was my problem, I went back to using List instead of ArrayList and had already imported "java.util.List."
 
reply
    Bookmark Topic Watch Topic
  • New Topic