• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

ClassCastException in forwarded JSP

 
christopher dash
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • 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
 
Deepak Bala
Bartender
Posts: 6663
5
Firefox Browser Linux MyEclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you binding the 2 dimension array to the request attribute by mistake ?
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • 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
  • 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
Firefox Browser Linux MyEclipse IDE
  • Mark post as helpful
  • send pies
  • 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
  • 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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic