I am new to the JSP and server side programming. Till now I am working with Servlets and java classes. I am segregating my application (as per MVC model) with the help of java classes. I would like to know difference between java beans and java classes. And in which scenario I can use a java bean instead of a java class. Any helpful explanation or helpful links?
2 Answers
A Java bean is just a class which conforms to some conventions:
- properties that can be accessed by getters (and setters if those properties are not read-only)
- no-arg public constructor
- serializable
The JSP EL and tags are designed around those conventions. Most of them don't need all these conventions to be respected. properties available by getters is the most important of these conventions. For example, the expression
${foo.bar.name}
displays the name of the bar of the foo bean. foo is a bean that must be in the page, request, session or application context. And this expression will call getBar()
on this bean, and then getName()
on the object returned by getBar()
.

- 678,734
- 91
- 1,224
- 1,255
-
Is necessary implement serializable to be considered a bean? – PlainOldProgrammer Dec 17 '14 at 22:33
-
2@Wronski in the strictest definition, yes: https://docs.oracle.com/javase/tutorial/javabeans/advanced/persistence.html. But as I said, many frameworks rely on JavaBean conventions to be respected, but that doesn't mean they must respect all of them. For example, accessing an object property using the JSP EL won't fail if the object is not serializable. – JB Nizet Dec 18 '14 at 19:35
The JavaBeans specification defines the type JavaBeans components as "reusable software components". A component is a simple Java Bean Class Java respects certain conventions about method naming, construction and behavior. Adherence to these conventions makes it possible to use, reuse, replace and connect Java Beans for development tools. The beans must be "Serializable
"In order to save and restore instances of this class.

- 4,190
- 10
- 49
- 75