O'Reilly logo

Head First Servlets and JSP, 2nd Edition by Bert Bates, Bryan Basham, Kathy Sierra

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required