Using scriptlet to Initialize JavaBean


How do I use a scriptlet to initialize a newly instantiated bean?



A jsp:useBean action may optionally have a body. If the body is specified, its contents will be automatically invoked when the specified bean is instantiated. Typically, the body will contain scriptlets or jsp:setProperty tags to initialize the newly instantiated bean, although you are not restricted to using those alone.

The following example shows the "today" property of the Foo bean initialized to the current date when it is instantiated. Note that here, we make use of a JSP expression within the jsp:setProperty action.

<jsp:useBean id="foo" class="com.Bar.Foo" >
<jsp:setProperty name="foo" property="today"
   (new java.util.Date())%>"/>
<%-- scriptlets calling bean setter methods go here --%>
</jsp:useBean >

2007-04-03, 6771👍, 0💬