aspose file tools*
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes <jsp:setProperty> Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "<jsp:setProperty>" Watch "<jsp:setProperty>" New topic
Author

<jsp:setProperty>

nisha chidella
Ranch Hand

Joined: Jul 24, 2008
Posts: 57




Here Person is superclass with name property and Employee is subclass with empID property.
In the above code which works fine, shouldn't we get jsp compilation error in line 8 because empID is property of Employee not Person.
won't the two lines translate this way somewhere in generated servlet:
person.setName("neesha");//1
person.setEmpID(2);//2

now since empID is Employee property not Person, shouldn't we get an error here in line 2?
how does this work ?


SCJP 5.0(100%)
Preparing for SCWCD
Balu Sadhasivam
Ranch Hand

Joined: Jan 01, 2009
Posts: 874



This piece of code is translated similar to

Person person = new com.example.model.Employee();

Hence by polymorhism
person.setName("neesha");//1
person.setEmpID(2);//2

are possible
nisha chidella
Ranch Hand

Joined: Jul 24, 2008
Posts: 57

Person person = new com.example.model.Employee();

Hence by polymorhism
person.setName("neesha");//1
person.setEmpID(2);//2





that's my question, by polymorphism, line 2 should not work. setEmpID(int emp) is in class Employee not in class Person.

Balu Sadhasivam
Ranch Hand

Joined: Jan 01, 2009
Posts: 874

Oops. sorry I got it completely wrong and no way related to polymorphism.

you are right !! is it compiled and working fine ?
nisha chidella
Ranch Hand

Joined: Jul 24, 2008
Posts: 57


is it compiled and working fine ?




yup. it gets compiled and works fine.
Balu Sadhasivam
Ranch Hand

Joined: Jan 01, 2009
Posts: 874

Nisha ,

Its not compiled for me. i get the error as you mention "empID" is not property of Person.
nisha chidella
Ranch Hand

Joined: Jul 24, 2008
Posts: 57
absolutely no idea how it's not working for you and working for me.

see what's happening in the generated servlet's service method:




there is no call to person.setName() or person.setEmpID..but eventually somewhere these shud be called. don't know how findAttribute() is working.

I definately don't have getEmpID() in Person class. Here are all my other files:

1.Person.java




2.Employee.java



3. S1.java




4. result.jsp


Can you tell me what exactly is the error you are getting ? sorry for posting all this code. but i need to know whether it's supposed to work or not. It works for me.
Balu Sadhasivam
Ranch Hand

Joined: Jan 01, 2009
Posts: 874

Well i had similar jsp and bean classes like that , anyways let me try tomm and will let you know.
nisha chidella
Ranch Hand

Joined: Jul 24, 2008
Posts: 57
ok.thanks.

my guess is internally it must always be doing getXXX() and setXXX() on class not the type. Thats the only way it can work.
Balu Sadhasivam
Ranch Hand

Joined: Jan 01, 2009
Posts: 874


my guess is internally it must always be doing getXXX() and setXXX() on class not the type. Thats the only way it can work.


Nope thats shouldnt be the case. I tried with same code as you shown and got this Parsing error. What JSP container are you using ?



As you know , there are 2 phases translation and compilation and I guess your JSP container is dumb that it allows translation to happen and thats why you get the jspservlet generated , but actually when you run it where the compilation happens it should fail. So the question did you see the output ?

And if you can see the output then are the bean classes are updated ? compiled and jsp is refering to correct bean classes. So we should not calculate anything on the "translated code".


Well i came to such conclusion , because i tried changing abstract Person class to non-abstract and used following jsp code
which tries to instantiates Person class with type as Employee Subclass ( whihc is impossible in Java standards) which get Parsed successully . but failed compilation (incompatible types)




nisha chidella
Ranch Hand

Joined: Jul 24, 2008
Posts: 57
I am getting the output too. so it's getting translated and compiled too. Am using Tomcat6.0. It shouldn't be that dump.

In your jsp, you have type="Employee" and class="Person". so, ofcourse it's supposed to fail. I am trying with type="Person" and class="Employee" and my question is when type is Person, does the translation somewhere happens like this:

person.setName("xxx");
person.setEmpID(1);

If so, it should fail by principle of polymorphism. we were discussing this..right? just change your jsp and let me know. thanks.
nisha chidella
Ranch Hand

Joined: Jul 24, 2008
Posts: 57

i tried changing abstract Person class to non-abstract and used following jsp code
which tries to instantiates Person class with type as Employee Subclass



Ok. sorry didn't read your reply properly earlier.
so, you tried both ways.
Don't know..it's working for me and i can see the output too.
Shailesh Narkhede
Ranch Hand

Joined: Jul 10, 2008
Posts: 368
nisha,
Actally they are taking care of all the things in below method(introspecthelper)

org.apache.jasper.runtime.JspRuntimeLibrary.introspecthelper(_jspx_page_context.findAttribute("person"), "name", "neesha", null, null, false);
which is the static method of JspRuntimeLibrary class.

By using some classes in java.lang.reflect & java.beans package.


Thanks,
Shailesh
Kamal Tripathi
Ranch Hand

Joined: Oct 02, 2008
Posts: 86
or as I can see in your generated servlet code in line nos. 32 and 38

"person" references are searched with

_jspx_page_context.findAttribute("person")

and then cast back to type mentioned in class attribute: com.example.model.Employee, so now you can call setName("...") as well. This I think is may also be one of the reasons


Kamal Tripathi
SCJP 1.4 90%, SCWCD5 94%, Next SCDJWS--> In Naescent stage. Researching abt exam and material itself.
Balu Sadhasivam
Ranch Hand

Joined: Jan 01, 2009
Posts: 874


Shailesh , Kamal ,

I m not sure if you understood Nisha question properly. empId is property of Employee and cannot be accessed through Person object. but she was able to do in her tomcat 6 container.

I tried in weblogic 8.1 and 10.3 and as expected it failed but it dint fail for her.


]] Root cause of ServletException.
weblogic.servlet.jsp.CompilationException: Failed to compile JSP /person.jsp
person.jsp:9:42: This property does not exist.
<jsp:setProperty name="person" property="empID" value="2" />
^-----^
person.jsp:16:41: This property does not exist.
<jsp:getProperty name="person" property="empID"/>

^-----^


Balu Sadhasivam
Ranch Hand

Joined: Jan 01, 2009
Posts: 874

nisha chidella wrote:

i tried changing abstract Person class to non-abstract and used following jsp code
which tries to instantiates Person class with type as Employee Subclass



Ok. sorry didn't read your reply properly earlier.
so, you tried both ways.
Don't know..it's working for me and i can see the output too.


I couldnt believe this yet !!
nisha chidella
Ranch Hand

Joined: Jul 24, 2008
Posts: 57
. i have left banging my head over it for now...Am still doing my first reading of HFSJ...so, i moved on.
we need clarification from another Tomcat user on this. hope my question is clear to others. Balu, you understood my question. and thanks for trying.
Archana Annamaneni
Ranch Hand

Joined: Jan 29, 2003
Posts: 147
Nisha

I ran your code using Tomcat 4.1 , it compiled and prints results.

I understood what you are saying , how it is working if it is not property of Employee , I am not really sure.



Looks like above two lines are the reasin it is working . How does the compiled code in Weblogic looks like ?

Archana
nisha chidella
Ranch Hand

Joined: Jul 24, 2008
Posts: 57
Archana, thanks for that confirmation that it works in Tomcat. and also we see how the Tomcat container is making it work. So, does it mean Tomcat container is dump, because as Balu confirmed it doesn't work in websphere which is what we expect to happen. And what if such question appear in the exam, what should we answer ?
Balu Sadhasivam
Ranch Hand

Joined: Jan 01, 2009
Posts: 874


Balu confirmed it doesn't work in websphere


Please correct it as weblogic.. and sure i will try in websphere soon..


How does the compiled code in Weblogic looks like ?


@ Archana , please go back in this thread to see weblogic generated source.
Archana Annamaneni
Ranch Hand

Joined: Jan 29, 2003
Posts: 147
Balu , sorry I didn't see earlier .

I see Weblogic does little bit more processing, before setting the values.

Archana
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: <jsp:setProperty>