Why are my bean properties not recognized by the JSP container?

I have created a simple bean that looks like this:

package com.mycomp;
public class TestBean {
  private String firstName;
  private String lastName;

  public String getFirstName() {
    return firstName;
  }

  public void setFirstName(String v) {
    firstName = v;
  }

  public String getLastName() {
    return lastName;
  }

  public void setLastName(String v) {
    lastName = v;
  }
}

I have compiled it and placed the class file in the WEB-INF/classes/com/mycomp directory for the application, as described in the book.

Still, when I use a <jsp:setProperty> or <jsp:getProperty> action and specify the property names FirstName or LastName, I get a compilation error for the page like this:

Error: 500 Location: /test/jsp/test.jsp 
Internal Servlet Error: org.apache.jasper.JasperException: 
Cannot find any information on property 'FirstName' 
  in a bean of type 'com.mycomp.TestBean'.

What’s going on here?

Answer:
Java is a case-sensitive language, and all bean property names are required to start with a lowercase letter. So the access methods in your bean:

getFirstName()/setFirstName()
getLastName()/setLastName()

correspond to properties named “firstName” and “lastName”. The property name is the method name minus the get/set prefix, with the first character changed from upper to lower case.

That’s why the container can’t find a property named “FirstName”, since it starts with an uppercase “F” instead of a lowercase “f”.

This naming rule is followed in all examples in the book, but it’s not explicitely described anywhere. I’ll put it on my list of things to be improved in the next edition of the book.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>