Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Problem with jsp:setAttribute tag

 
Rohit Bhagwat
Ranch Hand
Posts: 205
  • Mark post as helpful
  • send pies
  • 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.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65227
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • 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
  • 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
Author and ninkuma
Marshal
Pie
Posts: 65227
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes you are perfect


Hardly! But I'm glad that you've found my reply helpful.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic