Servlets & JSP Series 8 - Script-free pages
?
- Standard action is related to JavaBean; Standard actions contains- 1.jsp:useBean; 2.jsp:getProperty; 3.jsp:setProperty; 4.jsp:include; 5.jsp:forward.
- Declare and initialize a bean attribute with <jsp:useBean>; Get a bean attribute’s property value with <jsp:getProperty>.
- <jsp:useBean> can also create a bean: if the <jsp:useBean> cannot find an attribute object, it can make one.
- With a <jsp:useBean> body, you can have code that runs conditionally, only if the bean attrivute cannot be found and a new bean is created, the Property values will be set only if a new bean is created, if an existing bean with that scope and id are found, the body of the tag will never run, so the property will not be reset from your JSP code, any code inside the body of <jsp:useBean> is conditional, it runs only if the bean is not found and a new one is created.
- When you write a <jsp:useBean>, the class attribute determines the class of the new object, it also determines the type of the reference variable used in the generated servlet, but if you want the reference type to be different from the actual object, you can change the Person class to make it abstract, and make a concrete subclass Employee, and in JSP, we you need to make the reference variable type Person, and the object an instance of class Employee.
- If type is used without class, the beam must already exists in “page” scope; If class is used (with or without type) the class must not be abstract, and must have a public no-arg constructor.
- The scope attribute defaults to “page”, if you do not specify a scope in either the <jsp:useBean> or <jsp:getProperty> tags, the Container uses the default of “page”.
- Type == reference type; class== object type; type is what you declare (can be abstract); class is what you instantiate (must be concrete); type x = new class().
- If going straight from the request to the JSP without going through a servlet, 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.
- If the request parameter name matched the bean property name, you do not need to specify a value in the <jsp:setProperty> tag for that property.
- The <jsp:setProperty> action takes the String request parameter, converts it to an int and passes that int to the bean’s setter method for that property.
- Automatic String-to-primitive conversion does not work if you use scripting, it fails even if an expression is inside the <jsp:setProperty> tag, if you use scripting, the automatic conversion does not work.
- The bean standard action tags are more natural to a non-programmer.