JSP standard actions
Without standard actions (using scripting)
<html>
<body>
<% foo.Person p = (foo.Person) request.getAttribute(“person”); %>
Person is: <%= p.getName() %>
</body>
</html>
With standard actions (no scripting)
<html>
<body>
<jsp:useBean id=”person” class=”foo.Person” scope=”request” />
Person created by servlet: <jsp:getProperty name=”person” property=”name” />
</body>
</html>
jsp:useBean
# Identifies the standard action tag
id="person"
# declares the identifiers for the bean object
class="foo.person"
# Declares the fully qualified class name for the bean object
scope = "request"
# Identifies the attribute scope for this bean object
jsp:getProperty
# Identifies the standard action tag
name=”person”
# Identifies the actual bean object. This will match the “id” value from the <jsp:useBean> tag.
property=”name”
# Identifies the property name in the bean class. The property with the setter and getter methods.
<jsp:setProperty name=”person” property=”name” value=”Fred” />
It will set value into the name property of a person object
jsp:setProperty
This action tag is used to set the value into the property of an action object.
---------------------------------------------------------------------------------------------------------
<jsp:useBean id=”person” class=”foo.Person” scope=”page”>
<jsp:setProperty name=”person” property=”name” value=”Fred” />
</jsp:useBean >
Any code inside the body of <jsp:useBean > is CONDITIONAL. It runs ONLY if the bean
isnt found and a new one is created
---------------------------------------------------------------------------------------------------------
We need to make the reference variable type Person, and the object an instance
of class Employee. Adding a type attribute to the tag lets us do that.
<jsp:useBean id=”person” type=”foo.Person” class=”foo.Employee”
scope=”page”>
----------------------------------------------------------------------------------------------------------
The param attribute lets you set the value of a bean property to the value of a request parameter. JUST by naming the request parameter!
<jsp:useBean id=”person” type=”foo.Person” class=”foo.Employee”>
<jsp:setProperty name=”person” property=”name” param=”userName” />
</jsp:useBean>
Here the name property of a person bean will receive the value from the request parameter named "userName"
If the request parameter name matches the bean property name, you don’t need to specify a value in the <jsp:setProperty> tag for that property.
--------------------------------------------------------------------------------------------------------
If you make ALL the request parameter names match the bean property names.
<jsp:useBean id=”person” type=”foo.Person” class=”foo.Employee”>
<jsp:setProperty name=”person” property=”*” />
</jsp:useBean>
No need to specify the name of the property if all the property names are matching with the request parameter names. All parameter values will be set using the property="*" attribute.
---------------------------------------------------------------------------------------------------------
Notes
If type is used without class, the bean must already exist.
If class is used (with or without type) the class must NOT
be abstract, and must have a public no-arg constructor.
If type is used without class and if the bean object not already exist then it will throw
java.lang.InstantiationException exception
The scope attribute defaults to “page”
If you use the <jsp:setProperty> standard action tag with the property wildcard, OR just a property name without a value or param attribute (which means the property name matches the request parameter name), OR you use a param attribute to indicate the request parameter whose value should be assigned to the bean’s property, OR you type a literal value, the automatic conversion from String to int works. Each of these examples converts automatically:
These all work
<jsp:setProperty name=”person” property=”*” />
<jsp:setProperty name=”person” property=”empID” />
<jsp:setProperty name=”person” property=”empID” value=”343” />
<jsp:setProperty name=”person” property=”empID” param=”empID” />
BUT... if you use scripting, the automatic conversion does NOT work: !
This does NOT work!
<jsp:setProperty name=”person” property=”empID” value=”<%= request.getParameter(“empID”)%>”/>