File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JSP and the fly likes Problem with jsp:setAttribute tag Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "Problem with jsp:setAttribute tag" Watch "Problem with jsp:setAttribute tag" New topic
Author

Problem with jsp:setAttribute tag

Rohit Bhagwat
Ranch Hand

Joined: Dec 19, 2004
Posts: 205
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

Joined: Jan 10, 2002
Posts: 60752
    
  65

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.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Rohit Bhagwat
Ranch Hand

Joined: Dec 19, 2004
Posts: 205
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

Joined: Jan 10, 2002
Posts: 60752
    
  65

Yes you are perfect


Hardly! But I'm glad that you've found my reply helpful.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Problem with jsp:setAttribute tag
 
Similar Threads
Q 63 from SCWCD mock exam
bean name
Scope of jsp:useBean
<jsp:useBean>
JSP error with tomcat: can't find write method