*
The moose likes JSP and the fly likes unable to set the value of Internal attribute using <c:set> Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "unable to set the value of Internal attribute using <c:set>" Watch "unable to set the value of Internal attribute using <c:set>" New topic
Author

unable to set the value of Internal attribute using <c:set>

Anil Deshpande
Ranch Hand

Joined: Jan 13, 2008
Posts: 119
Before explaining the problem let me just give the code

I have two classes Person and Dog. The code is as follows:




The I have written two JSP file index.jsp showfirst.jsp

The code is As follows:

index.jsp


showfirst.jsp:





The problem is in showfirst.jsp file. at line 7. I am unable to change the value of the Dog's name

It is giving me an exception




What to do if i want to change the dog's name in the person.

Please help me.


Anil Deshpande
SCJP 1.5, SCWCD 1.5
Himanshu Kansal
Ranch Hand

Joined: Jul 05, 2009
Posts: 257
The "person" you are getting in "showFirst.jsp" is in "request" object. You cannot change it there directly. You need to dereference it from there first.


Experience and talent are independent of age
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61092
    
  66

Himanshu Kansal wrote:The "person" you are getting in "showFirst.jsp" is in "request" object. You cannot change it there directly. You need to dereference it from there first.

I have no idea what you are talking about.

The dog instance must be referenced as the target:


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Himanshu Kansal
Ranch Hand

Joined: Jul 05, 2009
Posts: 257
Hello,

I said that the "person" being referenced in showFirst.jsp is the one in request scope. An effort is being made to change the dog's name in that object itself. First reference to that object needs to be obtained(dereferenced) and then chnge the name.

target={person.dog} and property="name" would help in accessing that property, but wont change it anyhow.

Moreover I did not get any exception upon running the code.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61092
    
  66

Himanshu Kansal wrote:... would help in accessing that property, but wont change it anyhow.

You are not correct. My example will work just fine and is the proper way to make the change to the property.
Himanshu Kansal
Ranch Hand

Joined: Jul 05, 2009
Posts: 257
1. That won't work because the person is contained in the request object.
2. I have tried it, name does not change.
3. I have no intentions of an argument. I tried this example without any change of the code, as it is and all results were as expected. This was done because you had different opinion, so I decided to try.

Your example would work perfect in case of <jsp:include...> here it's a forward.

Regards
Tim McGuire
Ranch Hand

Joined: Apr 30, 2003
Posts: 820

The Bear example works in my test.

The "person" on showfirst.jsp is the person from the request and can be changed with the jstl c set tag.

This is from http://www.ibm.com/developerworks/java/library/j-jstl0211.html :

the EL provides built-in support for retrieving these objects as scoped variables. In particular, any identifier appearing in an EL expression that does not correspond to one of the EL's implicit objects is automatically assumed to reference an object stored in one of the four JSP scopes


perhaps Himanshu Kansal refers to the jsp setProperty tag
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61092
    
  66

There's no argument. My code works. Any assertions to the contrary are incorrect. No opinion is involved; just simple facts.

A forward transfers the same request to the next resource. All scoped vairables placed on the request before the forward are still available in the target of the forward.
Anil Deshpande
Ranch Hand

Joined: Jan 13, 2008
Posts: 119
Well I think tempers have been flying high, just cool down everybody.

I want to change the dog's value to "whatever I want" in showfirst.jsp.

I did some of the changes mentioned in the shoefirst.jsp and these are my observations. (I have executed the code after doing the changes)

If I write just line number 8 in showfirst.jsp without any previous code in line 7 (where I am doing <c:set target="${person.dog.name}" property="name" value="Nimmy"/>), i.e

It displyes "Jimmy". That's expected and that's what I am getting.

But As soon as I introduce line 7 i.e




Things start going heywire.

It throws me an exception from the index.jsp page


I did even changes in line 13 to <jsp:forward page="/showfirst.jsp"/>.

Still I am getting the same exception. What shall I do. I want to change the name of the Dog . Can I do it without throwing the exception.

please Explain with the code.

I perosnally feel both of you are right somewhere.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61092
    
  66

<c:set target="${person.dog.name}" property="name" value="Nimmy"/>

This does not match what I wrote.

It should be:


The target identifies the bean, not the property.
Himanshu Kansal
Ranch Hand

Joined: Jul 05, 2009
Posts: 257
Bear Bibeault wrote: A forward transfers the same request to the next resource. All scoped vairables placed on the request before the forward are still available in the target of the forward.

Yes, the request object is shared, also are the variable placed on the request. What about the other variables NOT placed on the request? Isn't doing the following in showFirst.jsp:

same as doing:

?

Anil Deshpande wrote:Well I think tempers have been flying high, just cool down everybody.

Not a high temper from my side, but the person I replied above is the one who I have been following since when I started learning Java an year ago. And now I am almost head-on.

Anil Deshpande wrote: But As soon as I introduce line 7 i.e




person.dog.name has no property called "name" but person.dog has one. So target="${person.dog}" should be there.

Here again I emphasize that using person.dog as target, where we change, won't change for that object which we print in the next line, hence the unchanged name only would be printed.

Still I'll watch here if someone points out what I am missing to see which might be so obvious.

Regards

Anil Deshpande
Ranch Hand

Joined: Jan 13, 2008
Posts: 119
Viola.................
javascript:emoticon('');
I got the result. Sean is right.

If I place <c:set target="person.dog" property="name" value="Nimmy"/> It really did give me the updated name of the Dog, "Nimmy"

thanks a lot for the replies. What would I have done without JavaRanch helpjavascript:emoticon('');
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61092
    
  66

Himanshu Kansal wrote:[What about the other variables NOT placed on the request?

Of course those are not transferred, but no one is talking about those. In fact, if using the JSTL and EL, there should be no scripting variables at all. I assumed (and I hope that this is true) that the scriptlets in the example are just there to set up the beans for testing and would not be there in real, production code.
Himanshu Kansal
Ranch Hand

Joined: Jul 05, 2009
Posts: 257
Here is my code:

setParam.jsp


getParam.jsp


Classes Person and Dog are same as in original question.

output is in screenshot. The poor dog's name is unchanged.

Regards





[Thumbnail for output.jpg]

Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61092
    
  66

Running your exact code:




[Thumbnail for a.png]

Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61092
    
  66

Why it's not working for you, I do not know. But it should work, and does work as expected.

We've already hijacked this thread enough with this, so if you want to explore why your code is not working as expected according to the Servlet and JSP Specifications, please open another topic to do so.
Tim McGuire
Ranch Hand

Joined: Apr 30, 2003
Posts: 820

yes, Himanshu Kansal I get your same results when I remove the import from the top of the jsp file:


you have this taglib import or no?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61092
    
  66

Good catch Tim. Yeah, if you don't define the JSTL, it isn't going to work.
Anil Deshpande
Ranch Hand

Joined: Jan 13, 2008
Posts: 119
Good catch Tim. Yeah, if you don't define the JSTL, it isn't going to work.


Tim and Sean are right. Himanshu I think you had not included the taglib directive for including the core JSTL in the JSP.

I too have got the result as Sean explained.

I think We shall just stop this thread here. No mor replies
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61092
    
  66

Anil Deshpande wrote:I think We shall just stop this thread here. No mor replies

Why? You seem to think that there's something "bad" about this thread, but there isn't. Someone made a mistake, and we've found out (at least we think we have found out) what it was.

That's good for everyone to learn from in case they end up with the same problem.
Anil Deshpande
Ranch Hand

Joined: Jan 13, 2008
Posts: 119
You seem to think that there's something "bad" about this thread, but there isn't.


There is nothing wrong about this thread. Personally this has been most informative thread that I have ever been involved in on Javaranch. I meant no offence to any one when I said

"No more Replies".

I just meant to say that the problem has been identified and solved.

Once again thank you one and all
Himanshu Kansal
Ranch Hand

Joined: Jul 05, 2009
Posts: 257
Tim, thanks a lot for that. Yes I missed out the taglib directive.

do I need to start a new thread to ask why it affected the output?

Regards


(Now I believe it's good to speak-up, clears all doubts)
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61092
    
  66

Himanshu Kansal wrote:... ask why it affected the output?

Do a View Source in the browser. What do you see?
 
jQuery in Action, 2nd edition
 
subject: unable to set the value of Internal attribute using <c:set>