aspose file tools*
The moose likes Struts and the fly likes ClassCastException Error Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "ClassCastException Error " Watch "ClassCastException Error " New topic
Author

ClassCastException Error

Ashish Tahasildar
Greenhorn

Joined: Dec 10, 2006
Posts: 12
Hi ,

I am getting ClassCastException Error, followed by the javax.servlet.jsp.JspException No getter method for property: "nameDisplayFlag" of bean: "User"

Where nameDisplayFlag is instance of Class LoginBean ie Short
I am getting ClassCastException on the following code

HttpSession session= request.getSession(true);
LoginBean bean=(LoginBean) session.getAttribute("User");

Some where session is set as session.setAttribute("User",loginBean); (LoginBean)
Also on JSP page I am included a file UserHeader.jsp having following code
as:

<logic:present name="User">
<bean:define id="displayid" name="User" property="nameDisplayFlag"/>
<logic:equal name="displayid" value="1">
<strong><bean:write name="User" property="lastNameKanji"/> <bean:write name="User" property="firstNameKanji"/></strong>
</logic:equal>
<logic:equal name="displayid" value="2">
<strong><bean:write name="User" property="lastNameInAlphabet"/> <bean:write name="User" property="firstNameinAlphabet"/></strong>
</logic:equal>
</logic:present>

I am getting error of nameDisplayFlag getter property.

As Action is executed first then JSP page is called which is my MessageAction and calls message_01.jsp

Following is the whole Error which i got:

2007-02-19 19:21:22,790 INFO [STDOUT] java.lang.ClassCastException: java.lang.String
2007-02-19 19:21:22,790 INFO [STDOUT] at terra.web.message.MessageAction.messageInInbox(MessageAction.java:299)
2007-02-19 19:21:22,790 INFO [STDOUT] at sun.reflect.GeneratedMethodAccessor1412.invoke(Unknown Source)
2007-02-19 19:21:22,790 INFO [STDOUT] at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
2007-02-19 19:21:22,791 INFO [STDOUT] at java.lang.reflect.Method.invoke(Method.java:585)
2007-02-19 19:21:22,791 INFO [STDOUT] at org.apache.struts.actions.DispatchAction.dispatchMethod(DispatchAction.java:274)
2007-02-19 19:21:22,791 INFO [STDOUT] at org.apache.struts.actions.DispatchAction.execute(DispatchAction.java:194)
2007-02-19 19:21:22,791 INFO [STDOUT] at org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:419)
2007-02-19 19:21:22,791 INFO [STDOUT] at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:224)
2007-02-19 19:21:22,791 INFO [STDOUT] at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1196)
2007-02-19 19:21:22,791 INFO [STDOUT] at org.apache.struts.action.ActionServlet.doGet(ActionServlet.java:414)
2007-02-19 19:21:22,791 INFO [STDOUT] at javax.servlet.http.HttpServlet.service(HttpServlet.java:697)
2007-02-19 19:21:22,791 INFO [STDOUT] at javax.servlet.http.HttpServlet.service(HttpServlet.java:810)
2007-02-19 19:21:22,791 INFO [STDOUT] at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:252)
2007-02-19 19:21:22,791 INFO [STDOUT] at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
2007-02-19 19:21:22,791 INFO [STDOUT] at org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:81)
2007-02-19 19:21:22,791 INFO [STDOUT] at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202)
2007-02-19 19:21:22,791 INFO [STDOUT] at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
2007-02-19 19:21:22,791 INFO [STDOUT] at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)
2007-02-19 19:21:22,791 INFO [STDOUT] at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:178)
2007-02-19 19:21:22,791 INFO [STDOUT] at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:126)
2007-02-19 19:21:22,792 INFO [STDOUT] at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:105)
2007-02-19 19:21:22,792 INFO [STDOUT] at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:526)
2007-02-19 19:21:22,792 INFO [STDOUT] at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:107)
2007-02-19 19:21:22,792 INFO [STDOUT] at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:148)
2007-02-19 19:21:22,792 INFO [STDOUT] at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:856)
2007-02-19 19:21:22,792 INFO [STDOUT] at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection(Http11Protocol.java:744)
2007-02-19 19:21:22,792 INFO [STDOUT] at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:527)
2007-02-19 19:21:22,792 INFO [STDOUT] at org.apache.tomcat.util.net.MasterSlaveWorkerThread.run(MasterSlaveWorkerThread.java:112)
2007-02-19 19:21:22,792 INFO [STDOUT] at java.lang.Thread.run(Thread.java:595)
2007-02-19 19:21:22,814 ERROR [org.apache.catalina.core.ContainerBase.[jboss.web].[localhost].[/terra].[jsp]] Servlet.service() for servlet jsp threw exception
javax.servlet.jsp.JspException: No getter method for property: "nameDisplayFlag" of bean: "User"
at org.apache.struts.taglib.TagUtils.lookup(TagUtils.java:986)
at org.apache.struts.taglib.bean.DefineTag.doEndTag(DefineTag.java:230)
at org.apache.jsp.include.UserHeader_jsp._jspService(org.apache.jsp.include.UserHeader_jsp:228)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:97)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:810)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:322)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:314)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:264)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:810)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:252)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:672)
at org.apache.catalina.core.ApplicationDispatcher.doInclude(ApplicationDispatcher.java:574)
at org.apache.catalina.core.ApplicationDispatcher.include(ApplicationDispatcher.java:499)
at org.apache.jasper.runtime.JspRuntimeLibrary.include(JspRuntimeLibrary.java:966)
at org.apache.jsp.message.message_005f01_jsp._jspService(org.apache.jsp.message.message_005f01_jsp:340)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:97)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:810)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:322)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:314)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:264)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:810)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:252)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:672)
at org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:463)
at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:398)
at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:301)
at org.apache.struts.action.RequestProcessor.doForward(RequestProcessor.java:1062)
at org.apache.struts.action.RequestProcessor.processForwardConfig(RequestProcessor.java:386)
at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:229)
at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1196)
at org.apache.struts.action.ActionServlet.doGet(ActionServlet.java:414)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:697)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:810)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:252)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
at org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:81)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:178)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:126)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:105)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:526)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:107)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:148)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:856)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection(Http11Protocol.java:744)
at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:527)
at org.apache.tomcat.util.net.MasterSlaveWorkerThread.run(MasterSlaveWorkerThread.java:112)
at java.lang.Thread.run(Thread.java:595)


This error I am getting on different PC but not on my local server, JBoss4.0 is the web app used
Please Help me why I am receiving this error

thanks
Ashish
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
What the message is telling you is that when the attribute "User" was retrieved from the session, it wasn't of type "LoginBean", but rather of type String, and that's what caused the ClassCastException. Find out why it was a String instead of a LoginBean.


Merrill
Consultant, Sima Solutions
Ashish Tahasildar
Greenhorn

Joined: Dec 10, 2006
Posts: 12
Hi Merrill ,

Thanks for the reply and your perfect solution . The reason was the same as someone was setting the String value in "User", Thanks a lot.
One thing I did not get is that We got this error on Production Server but not on my local server Why??

Thank You,
Ashish
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
It's not uncommon for problems like this to surface once an application is put on a production server. When you're testing on your own server, your application only has to deal with the actions of one user and one application. When running on a production server, the app has to deal with may users interacting with the server simultaneously and many applications interacting.

The problem you experienced is a classic example. The problem was that another application or process was using the namespace "user" in the session context to store a different object than the one you were storing in the same namespace. That type of problem would only surface in a multi-user, multi-application environment.
Brent Sterling
Ranch Hand

Joined: Feb 08, 2006
Posts: 948
To follow up on Merrill's post...based in my experience I would say it is bad practice to use such generic names like "user" when adding items to the session. First off you want to use names that would be specific to your application. You could use a full domain name type convention (com.mycompany.myapp.user) but just something like "myapp.user" would be fine. This at least prevents conflicts with any other tool or framework that might stick something on the session using the value "user".

Then it is also a good idea to keep a global list of the names you are using and the types. This will prevent multiple instances in your application where you use the name "myapp.user" (which it sounds like was the problem here). The convention that I use is to define constants for all session variable names in a common source file. As long as all the developers stick to this convention you can prevent these types of issues.

- Brent
Ashish Tahasildar
Greenhorn

Joined: Dec 10, 2006
Posts: 12
Thanks Merrill and Brent . I will follow the good convention for namespace setting in session.

Thanks,
Ashish
 
Consider Paul's rocket mass heater.
 
subject: ClassCastException Error