Places Where Javabeans Are Used

Places where JavaBeans are used?

They often just represents real world data. Here's a simple example of a Javabean:

public class User implements java.io.Serializable {

// Properties.
private Long id;
private String name;
private Date birthdate;

// Getters.
public Long getId() { return id; }
public String getName() { return name; }
public Date getBirthdate() { return birthdate; }

// Setters.
public void setId(Long id) { this.id = id; }
public void setName(String name) { this.name = name; }
public void setBirthdate(Date birthdate) { this.birthdate = birthdate; }

// Important java.lang.Object overrides.
public boolean equals(Object other) {
return (other instanceof User) && (id != null) ? id.equals(((User) other).id) : (other == this);
}
public int hashCode() {
return (id != null) ? (getClass().hashCode() + id.hashCode()) : super.hashCode();
}
public String toString() {
return String.format("User[id=%d,name=%s,birthdate=%d]", id, name, birthdate);
}
}

Implementing Serializable is not per se mandatory, but very useful if you'd like to be able to persist or transfer Javabeans outside Java's memory, e.g. in harddisk or over network.

In for example a DAO class you can use it to create a list of users wherein you store the data of the user table in the database:

List<User> users = new ArrayList<User>();
while (resultSet.next()) {
User user = new User();
user.setId(resultSet.getLong("id"));
user.setName(resultSet.getString("name"));
user.setBirthdate(resultSet.getDate("birthdate"));
users.add(user);
}
return users;

In for example a Servlet class you can use it to transfer data from the database to the UI:

protected void doGet(HttpServletRequest request, HttpServletResponse response) {
List<User> users = userDAO.list();
request.setAttribute("users", users);
request.getRequestDispatcher("users.jsp").forward(request, response);
}

In for example a JSP page you can access it by EL, which follows the Javabean conventions, to display the data:

<table>
<tr>
<th>ID</th>
<th>Name</th>
<th>Birthdate</th>
</tr>
<c:forEach items="${users}" var="user">
<tr>
<td>${user.id}</td>
<td><c:out value="${user.name}" /></td>
<td><fmt:formatDate value="${user.birthdate}" pattern="yyyy-MM-dd" /></td>
</tr>
</c:forEach>
</table>

Does it make sense? You see, it's kind of a convention which you can use everywhere to store, transfer and access data.

See also:

  • JavaBeans specification

What is a JavaBean exactly?

A JavaBean is just a standard. It is a regular Java class, except it follows certain conventions:

  1. All properties are private (use getters/setters)
  2. A public no-argument constructor
  3. Implements Serializable.

That's it. It's just a convention. Lots of libraries depend on it though.

With respect to Serializable, from the API documentation:

Serializability of a class is enabled by the class implementing the
java.io.Serializable interface. Classes that do not implement this
interface will not have any of their state serialized or deserialized.
All subtypes of a serializable class are themselves serializable. The
serialization interface has no methods or fields and serves only to
identify the semantics of being serializable.

In other words, serializable objects can be written to streams, and hence files, object databases, anything really.

Also, there is no syntactic difference between a JavaBean and another class -- a class is a JavaBean if it follows the standards.

There is a term for it, because the standard allows libraries to programmatically do things with class instances you define in a predefined way. For example, if a library wants to stream any object you pass into it, it knows it can because your object is serializable (assuming the library requires your objects be proper JavaBeans).

What are JavaBeans in plain English?

A javabean is a standard. All Javabeans have the following 3 qualities:

1) The class implements Serializable
2) All fields have public setters and getters to control access.

3) A public no-argument constructor.

JavaBeans being used with complex types

If you're restricted to jsp:useBean/jsp:setProperty, then you need to create the both beans yourself beforehand and then set the child bean as property of the parent bean yourself. The last line in the following snippet does basically a game.setGameStatistics(gameStatistics).

<jsp:useBean id="game" class="com.example.Game" scope="session" />
<jsp:setProperty name="game" property="*" />
<jsp:useBean id="gameStatistics" class="com.example.GameStatistics" scope="request" />
<jsp:setProperty name="gameStatistics" property="*" />
<jsp:setProperty name="game" property="gameStatistics" value="${gameStatistics}" />

Also ensure that the input field names match the exact bean property names. Thus don't use

<input name="gameStats.amountOfNumbersToMemorise">

but just

<input name="amountOfNumbersToMemorise">

and ensure that the both beans doesn't share the same property names.


I'd however warmly recommend to have a look at a Java based MVC framework such as JSF or Spring MVC which does this all transparently without the need to fiddle with legacy jsp:useBean and jsp:setProperty tags.

JavaBeans place

Basically JavaBeans are the entities which are

  • made for re-usability "write once, run anywhere"
  • passing one object instead of multiple
  • are serializable, zero argument constructor, accessible through getter & setter

So I think there is no need to think a lot of where do i keep it ?considering you are following coding standard.
I will suggest WEB-INF\classes\com\myproject\beans



Related Topics



Leave a reply



Submit