• 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

ClassCastException in forwarded JSP

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a HTML form, a servlet and a JSP page. The form hits the servlet and the servlet forwards to the JSP. I set a request attribute in the servlet and get the attribute in the JSP. Casting the attribute throws a ClassCastException.

Here is the servlet:

Here is the JSP page:

Here is the ArrayWrapper class:

Here is the error message:


org.apache.jasper.JasperException: An exception occurred processing JSP page /result.jsp at line 9

6: <p>
7:
8: <%
9: ArrayWrapper arrayObj = (ArrayWrapper) request.getAttribute("array");

Stacktrace:
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:505)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:416)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:342)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:267)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
com.control.MoveRight.doPost(MoveRight.java:21)
javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)

root cause

java.lang.ClassCastException: [[I cannot be cast to com.model.ArrayWrapper
org.apache.jsp.result_jsp._jspService(result_jsp.java:62)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:374)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:342)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:267)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
com.control.MoveRight.doPost(MoveRight.java:21)
javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)


Please let me know if you need more information.

Thank you in advance,
cd
 
Bartender
Posts: 6663
5
MyEclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Are you binding the 2 dimension array to the request attribute by mistake ?
 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

christopher dash wrote:
java.lang.ClassCastException: [[I cannot be cast to com.model.ArrayWrapper



I think Deepak Bala is right . so christopher , did you verify that?
 
christopher dash
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Deepak Bala wrote:Are you binding the 2 dimension array to the request attribute by mistake ?



I pass a two-dimensional array into the ArrayWrapper constructor:

I then bind the ArrayWrapper instance to the request as an attribute named "array":

Is this okay to do?
 
Deepak Bala
Bartender
Posts: 6663
5
MyEclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes it is ok to do. However what is bound is not the type that you mentioned but a two dimensional array. That is what is causing the class cast. Just before forwarding to the jsp, check the object contents that are bound again
 
christopher dash
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is one solution.

Earlier I passed an instance of ArrayWrapper into the setAttribute method, but the parameter is considered an Object. By casting the parameter as a ArrayWrapper, no ClassCastException is thrown.

I will mark this thread resolved.

Thank-you very much,
cd
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic