Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8995707
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T23:39:20+00:00 2026-06-15T23:39:20+00:00

I create an ArrayList based on ProductData class on my JSP, this list represents

  • 0

I create an ArrayList based on ProductData class on my JSP, this list represents as a shopping cart.

ArrayList<ProductData> cartList = new ArrayList<ProductData>();
ProductData data = new ProductData(productId, productName, productPrice, productAmmount);
cartList.add(data);

After that, I’m storing my ArrayList to JSP session, cause I don’t want my ArrayList lost when I leave this page.

session.setAttribute("cartList", cartList);

On the other section, I get the session and convert it as an ArrayList of ProductData
(I check if the session is null or not for sure)

ArrayList<ProductData> cartList = session.getAttribute("cartList") == null ? new ArrayList<ProductData>() : (ArrayList<ProductData>)session.getAttribute("cartList");

It works well on the first run. But, every time I changes the code on my page (and I’m not modifying my class declaration), the servlet thows an error: java.lang.ClassCastException: org.apache.jsp.MCPatisserie.cart_jsp$1ProductData

My question is: is this a good method to implement a class on java servlet page?
Is this error caused by recompilation of the servlet page and the session object didn’t match with my ArrayList anymore?
Thanks in advance. 🙂

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T23:39:21+00:00Added an answer on June 15, 2026 at 11:39 pm

    Each time you change your JSP, the container recompiles it to a new Java source file, and recompiles this new Java source file to a new class. It seems you have defined a Java class inside the JSP. So this class is also regenerated and recompiled. So the object in the session has a class that is not the same as the one recreated.

    You shouldn’t have Java code in JSPs. And definitely not class definitions. ProductData should be defined in its own .java file, like any other class.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Using this Java code: // create the items list List<Item> items = new ArrayList<Item>();
E.G. to create an ArrayList of Strings we have to do something like List<String>
I need to create an Arraylist in a while loop with a name based
Hi I have Card class... In another class I create an arrayList of Card
I'm running into a problem when trying to create an ArrayList in Java, but
Say I create one object and add it to my ArrayList . If I
CREATE OR REPLACE FUNCTION layer2layerAttribute RETURN VARCHAR2 AS /** * This function properly joins
create table ImagenesUsuario { idImagen int primary key not null IDENTITY } This doesn't
Create a new appdomain, setup the assemblyResolve handler and you always get an exception
This is very similar to another question ( Functional Data Structures in Java )

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.