This week's book giveaway is in the Agile and Other Processes forum.
We're giving away four copies of The Little Book of Impediments (e-book only) and have Tom Perry on-line!
See this thread for details.
Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Error on basic jsp...

 
Carmen Brianick
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I'm getting a "cannot resolve symbol getEmpID()" error when I click submit on my html page. I have listed my short html page, jsp, and bean code below. Thanks so much for your great help! -Carmen
***html code***
<html><body><form action="TestBean.jsp">
name: <input type="text" name="name">
ID#: <input type="text" name="empID">
<input type="submit">
</form></body></html>

***jsp***
<html><body><jsp:useBean id="person" type="foo.Person" class="foo.Employee">
<jsp:setProperty name="person" property="*"/>
</jsp:useBean>
User's name is <%=person.getName()%><br>
ID is <%=person.getEmpID()%>
</body><html>

*** Person bean ***
package foo;
public abstract class Person{
private String name;
public void setName(String name){this.name = name;}
public String getName(){return name;}
}

*** Employee bean ***
package foo;
public class Employee extends Person{
private int empID;
public void setEmpID(int empID){this.empID=empID;}
public int getEmpID(){return empID;}
}
 
Carmen Brianick
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, how come if I use '<jsp:getProperty name="person" property="empID"/>' it would work? Why won't <%=person.getEmpID()%> work?

Thanks so much,
Carmen
 
Chandra Sagi
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your reference type is Person which doesn't know about empID. empID is defined in your Employee class. Change your type to foo.Employee and it should work

Thanks
Chandu
 
Carmen Brianick
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Chandra, thanks for your answer! I still have 2 questions I hope you can help on:

1. Why does this work, even though type is still 'foo.Person'? :
<jsp:useBean id="person" type="foo.Person" class="foo.Employee">
<jsp:setProperty name="person" property="*"/></jsp:useBean>
empID is: <jsp:getProperty name="person" property="empID"/>

2. In Person p = new Employee();// p is a Person and does not have any features or know about Employee, am I right?

Thanks so much,
Carmen
 
srini E
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'd really like to know the explanation for that....

Reason: HFJS Page 414, last but one bullet reads as
-- if you specify a "type" attribute in the <jsp:useBean>, you can set properties in <jsp:setProperty> ONLY on properties of the "type", but NOT on properties that exist only in the actual "class" type (In other words, polymorphism and normal java type rules apply).

Going by above i'm wordering how is that your get value is giving empId.??

will appreciate your response..
 
Carmen Brianick
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi srinivas, I believe I may have answered my own question #1 b/c for:
<jsp:getProperty name="person" property="empID"/>

the generated servlet code is:

out.write(org.apache.jasper.runtime.JspRuntimeLibrary.toString((((foo.Employee)_jspx_page_context.findAttribute("person")).getEmpID())));

So I see (foo.Employee) here doing some casting on the object, so maybe this is it.

This is as far as I know, maybe someone else can comment more?

Anyway, srinivas, I hope this helps, btw, do you have any comment about my question #2?

Thanks,
Carmen
 
Chandra Sagi
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Carmen,

You are right about the second question too.

Thanks
Chandu
 
Carmen Brianick
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for your confirmation of my answers Chandu.

Regards,
Carmen
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic