1
  <jsp:getProperty name="user" property="email" />

I have an user class with 3 instance variables: fname, lname, email. But the code above won't work unless I change my instance variables to exactly "firstName", "lastName", and "emailAddress" and changing the bean tag of course. All these fails when I tried (error example: no variable "fname" exist in the bean): fname, fName, firName, emailAddr, etc..

is there an exact requirement to the naming of variables?

user3758745
  • 777
  • 1
  • 6
  • 19

2 Answers2

0

There is no requirement. Make sure the fields on your bean are set to the names you are expecting in your JSP page. If you have getters and setters, you'll want to make sure those are updated too.

JQ-
  • 390
  • 1
  • 6
0

I dont know what you did but as you have a bean with 3 instance variables fname, lname, email. you must have the getter and setters for them.

Then in jsp you must first set the value for the properties.

<jsp:useBean id="user" class="packageName.User" scope="request"/>
<jsp:setProperty property="fname" value="<%=firstName %>" name="user"/>
<jsp:setProperty property="lname" value="<%=lastName %>" name="user"/>
<jsp:setProperty property="email" value="<%=emailId%>" name="user"/>

Where the firstName, lastName,emailId in this case i'm getting from some Form in scriptlet

Then get the properties like this.

<jsp:getProperty property="fname" name="user"/>
<jsp:getProperty property="lname" name="user"/>
<jsp:getProperty property="email" name="user"/>
SparkOn
  • 8,806
  • 4
  • 29
  • 34
  • ya i have setters and getters and all the requirements except jsp:setProperty in jsp. How come if i use "property="firstName"", then I don't need to setProperty? is it because my setters and getters are called getFirstName, getLastName, getEmailAddress that it assume my instances are firstName, lastName, emailAddress? – user3758745 Jul 16 '14 at 15:14
  • I dint understand fully what you are trying to say but if you change the names also if you dont set or initialiaze the properties with value how can you get the values – SparkOn Jul 16 '14 at 15:46
  • properties was initialized/added in the servlet. my jsp is just calling it from the session. What is mean is if I change my User instance variable to firstName and do then it works but if I change my instance variable to fname and do it says no variable "fname" for the bean. – user3758745 Jul 16 '14 at 22:13