• 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

Problem with jsp:setAttribute tag

 
Ranch Hand
Posts: 205
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello sir, madam

I have written the jsp page as follows

<%@ page import="businessData.visitorAddresses.*"%>

<%pageContext.setAttribute("abc","Hello"); %>

<jsp:useBean id="abc" class="businessData.visitorAddresses.AddressBean" scope="page">
<jsp:setProperty name="abc" property="name" value="Ram"/>
<jsp:setProperty name="abc" property="city" value="Pune"/>
<jsp:setProperty name="abc" property="state" value="MH"/>
</jsp:useBean>

<%=pageContext.getAttribute("abc")%>

I was shocked to see that I got a ClassCastException
Bean classes are well defined and are accessible without any problems. What I noticed is that pageContext.findAttribute() is being used in jsp:setProperty and thats why my string attribute named "abc" got a ClassCastException.

I guess this is a tomcat bug if I am not wrong

Waiting for your suggestions.
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I guess this is a tomcat bug if I am not wrong



You are wrong. It is your bug.

You declare 'abc' as a page-scoped variable of type String. Then you use a useBean action that specifies a different class. This is wrong. The useBean action will create a new variable only if a scoped variable with the specifid id does not already exist. When a scoped variable already exists, the useBean action binds to the existing variable.

And since the existing variable is of a different type than the action declares, you get a ClassCastException. Plain and simple.
 
Rohit Bhagwat
Ranch Hand
Posts: 205
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Bear

I am sorry again for using the word "Tomcat Bug"

Yes you are perfect and I have fully understood your explaination.
Thanks for your reply.

Hoping for your reply for my post in future too

Regards
Rohit.
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Yes you are perfect



Hardly! But I'm glad that you've found my reply helpful.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic