Home

JSP and Beyond

a pragmatic primer on building web-based solutions with Java technologies


Attributes of a Bean

When accessing beans through a JSP, you need to create an instance of them by setting a series of attributes. The following block of code provides an example of a bean that is called “calendar” and will only live for the current request. It is created from the com.mycompany.Calendar class.

<jsp:useBean id=”calendar” scope=”page” class=”com.mycompany.Calendar” />

id
The name that we will use to refer to the bean after it is created.

class
The class contains the fully qualified name of the class for the bean. This is the fully qualified name for the class such as “com.mycompany.Calendar”.

type
This defines the object type that the bean will return (String, Int or another type or object). Our example above does not return a type.

scope
Each bean lives for a certain amount of time. The value for the scope can reference the page, request, session or application types.

  • Page - The bean is valid only for the current request.
  • Request - The bean is valid only for the current request. This is not a typo, even though it is very similar to the above Page scope. The difference is that the request scope will make the information in the bean available to any other JSPs that are accessed via <jsp:include> or <jsp:forward> actions.
  • Session - The bean will persist over the lifespan of the user’s session.
  • Application - Similar to the session, but applied globally. If we place a bean in the application scope it will be available across all user sessions.