Search code examples
jspjavabeansgetproperty

Javabean property in JSP


  <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?


Solution

  • 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"/>