But what if the attribute is not a String, but an instance of Person?

And not just a Person, but a Person with a “name” property. We’re using the term “property” in the non-enterprise JavaBean[7] way—the Person class has a getName() and setName() method pair, which in the JavaBean spec means Person has a property called “name”. Don’t forget that the “name” property means a change in case for the first letter, “n”. In other words, the name of the property is what you get when you strip off the prefix “get” and “set”, and make the first character after that lower case. So, getName/setName becomes name.

image with no caption

Note

We can tell from the getter/setter pair that Person has a property called “name” (note the lowercase “n”).

Servlet code

public void doPost(HttpServletRequest request, HttpServletResponse response)
                                        throws IOException, ServletException {

   foo.Person p = new foo.Person();
   p.setName("Evan");
   request.setAttribute("person", p);

   RequestDispatcher view = request.getRequestDispatcher("result.jsp");
   view.forward(request, response);
}

JSP code

image with no caption

[7] We’ll talk about JavaBeans in a few pages, but for now, just know that it’s a plain old Java class that has getters and setters that follow a naming convention.

Get Head First Servlets and JSP, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.