Engineering Full Stack Apps with Java and JavaScript
Actions are XML elements that encapsulate functionality on a JSP page. Programmers can create them and they can be used by the nonprogrammer to introduce dynamic behavior to a JSP page without having any knowledge of Java programming language. For those, non-programmers these will like a new html or xml tag element.
There are two forms of action elements:
standard
custom.
Standard actions are used for the same purpose as Java language-based scripting. Most things that we can achieve with scripting elements are achievable with other standard actions also. Difference between standard and custom actions are that you can rely on any J2EE-compliant JSP container to provide support for all the standard actions defined in the JSP spec. Custom actions will have to be imported manually.
Syntax
The general syntax of actions (standard and custom) is:
<prefix:tagname firstAttribute="value" secondAttribute="value" …> ...
</prefix:tagname>
Each standard action element consists of a start tag, <prefix:tagname>, and an end tag of the same name, </prefix:tagname>.
The start tag may contain named attributes, separated from their corresponding value by equal signs. The value is typically surrounded by double quotes or by single quotes.
A standard action may have a body, but it often has no body at all.
You can rely on any J2EE-compliant JSP container to provide support for all the standard actions defined in the JSP spec.
The prefix for all standard actions is jsp.
Standard actions can work only if the java object on which is applied obey at least some minimal conventions. The java objects that need to be used by standard actions need to follow JavaBeans specification. The idea of JavaBeans is that you can have Java components (typically classes) that can be interrogated by interested software, using the reflection techniques. By interrogating the methods available on a JavaBean, a standard action can obtain information about the properties that the bean supports—in other words, the data that it stores.
Standard actions available in JSP are:
jsp:useBean
declares a JavaBean instance and associates this with a variable name.
jsp:getProperty
read properties on bean set up using the <jsp:useBean> standard action
jsp:setProperty
write properties on bean set up using the <jsp:useBean> standard action
jsp:include
can be used to include the response from another file within your JSP page output
jsp:forward
forward processing to another resource within the web application
jsp:params
add additional parameters to the request and include it in the body of a <jsp:include> or <jsp:forward>
jsp:attribute
Allows you to define the value of a tag attribute in the body of an XML element instead of in the value of an XML attribute.
jsp:body
jsp:element
jsp:fallback
jsp:output
jsp:param
jsp:plugin
We will discuss about some important standard actions.
<jsp:useBean>
Declares a JavaBean instance and associates this with a variable name.
Attributes are
id
class
scope
values: page, request, session, application
default is page.
type
beanName
The instance is then available for use elsewhere in your JSP page: either in Expression Language, other standard actions, or even in Java language scripting.
Eg. <jsp:useBean id="theDog" class="animals.Dog" />
The attribute class must specify the fully qualified name of a class.
animals.Dog must obey JavaBean conventions.
animals.Dog must be visible somewhere in the web application—mostly this means it will exist as a class in WEB-INF/classes or within a JAR file in WEB-INF/ lib.
All ids for beans on a page must be unique.
The jave code in the _jspService() method can reference the object created by <jsp:useBean> in two ways:
As a local variable in the method, whose name comes from value of the id attribute.
As an attribute in some scope or other—page, request, session, or application. If we don’t specify the scope, it will be page scope.
Eg. <jsp:useBean id="theDog" class="animals.Dog" />
theDog becomes a local variable.
theDog is also the name of an attribute.
We can specify scope of the attribute created by <jsp:useBean>.
The valid values for scope are page, request, session, and application.
Specifying the scope attribute creates or uses an attribute in the specified scope and also makes it available as a local variable in the page scope with that name.
We can specify the scope as below:
<jsp:useBean id="theDog" class="animals.Dog" scope="session" />
If we don’t specify the scope, the scope of the attribute created by <jsp:useBean> will be page scope.
When we use <jsp:useBean> to create an attribute in one scope and another attribute with same name is already available in that scope, the jsp:useBean> recycles the existing bean; it doesn’t create a new one.
<jsp:setProperty>
<jsp:setProperty> is to set the values of one or more properties on a bean previously declared with <jsp:useBean>. The property attribute specifies a property on the bean.
Attributes of <jsp:setProperty> standard action are
name
name of the bean
property
property for which we need to assign the value
value.
value attribute supplies the data for the property through the setter method of the property object.
param
can use param attribute instead of value to set the value from a request parameter
E.g. 1: <jsp:setProperty name="theDog" property="weight" value="6.4" />
If we need to set the value from a request parameter, then we can use param attribute instead of value and give a valid request parameter as the value of the param attribute.
E.g. 2: <jsp:setProperty name="theDog" property="weight" param="dogWeight" />
This is shorthand for saying take the request parameter called “dogWeight,” and use the value for this to set the property called “weight” on the bean called “theDog.” This is equivalent to:
E.g. 3: <jsp:setProperty name="theDog" property="weight" value="<%= request.getParameter("dogWeight") %>" />
Because the property here is “weight,” then the underlying code will assume the existence of a setWeight() method on the theDog bean.
Instead of supplying a literal value, you can even substitute an expression. But this feature is not available to any other attributes.
<% float w = 6.4f; %>
<jsp:setProperty name="theDog" property="weight" value="<%=w%>" />
Container will also do some guessing if we don’t provide a param or value attribute. It will look for a parameter (from the ServletRequest or HttpServletRequest) with same name as that of property.
E.g. <jsp:setProperty name="theDog" property="weight" /> ?
The underlying code will look for a parameter (from the ServletRequest or HttpServletRequest) called “weight” and use this to set the property value for “weight” on “theDog.”
We can also use wild cards within property element.
E.g. <jsp:setProperty name="theDog" property="*" />
Any property whose name matches a request parameter name will have its value preloaded from that request parameter.
<jsp:getProperty>
You use it to output the value of a bean’s property to the response.
There are two attributes to supply:
name
the name of the bean
property
the name of the property.
They’re both mandatory.
E.g. <jsp:getProperty name="theDog" property="weight" />
Important notes on <jsp:useBean>, <jsp:setProperty> and <jsp:getProperty>
We can use <jsp:setProperty> and <jsp:getProperty> without a previous <jsp:useBean> if an attribute of the right name exists in the PageContext.
It’s good practice to include <jsp:useBean> before these actions in the same JSP page. The <jsp:useBean> won’t replace beans of the same name that you have set up by other means, and it will create beans of the right name that don’t exist already.
If your <jsp:setProperty> and <jsp: getProperty> standard actions try to access an attribute that doesn’t exist, they will fail with HTTP 500 errors returned to the requester.
<jsp:include>
The <jsp:include> can be used to include the response from another file within your JSP page output.
<jsp:include> Standard Action has two attributes
page
Page to include
flush
commit the response so far before anything is done about including the target page.
You might use <jsp:include> to include files that don’t exist at the point where you deploy your including pages. There’s no check on the existence of the page specified in <jsp:include> during the translation phase. However you’ll get a run-time error if you let your users access JSPs that try to include a page that doesn’t exist. So we should introduce controls that prevent access to the including JSP until the files needed for inclusion are actually present in your web application directory structure.
We cannot include an external file outside the web application using the <jsp:include> standard action.
We can include non-jsp files using the <jsp:include> standard action.
You can specify the value of the page attribute from a parameter as <jsp:include> runs at request time.
E.g. <jsp:include page='<%= request.getParameter("thePage") %>' />
<jsp:forward>
The purpose of this standard action is to forward processing to another resource within the web application.
There is only one mandatory attribute, which is page="URL."
The JSP specification says that a “<jsp:forward> effectively terminates the current page,”. Therefore any line after “<jsp:forward> won’t be executed.
The IllegalStateException will be thrown if any part of the response has already been committed and we try to use <jsp:forward>. Below are few cases:
There is no buffer, and even one character has been written to the response.
The buffer has been explicitly flushed (response.flushBuffer()).
The buffer has been automatically flushed on filling up (in a JSP, this will happen by default).
<jsp:params>
We can use the <jsp:param> standard action to add additional parameters to the request and include it in the body of a <jsp:include> or <jsp:forward>.
Added additional parameters to the request along with < jsp:include> or <jsp:forward> only last for the duration of the include or forward. Once you’re back in the including or forwarding JSP page, the parameters disappear.
They don’t replace existing parameters of the same name—they merely augment the list of values.
<jsp:attribute>
Allows you to define the value of a tag attribute in the body of an XML element instead of in the value of an XML attribute.
E.g. <jsp:attribute name="attributeName" [ trim= "true | false" ] />
All JSP standard actions and custom actions can contain a jsp:attribute standard element as a substitute for any of its attributes. One use case in which jsp:attribute is particularly helpful is where the value of an attribute is the result of a multi-line expression, which would not fit in the value of an attribute in the start tag of the action.
If an action contains any jsp:attribute elements and the action also has a body, it must use the jsp:body tag to represent the body.