JSP Elements Part 3 – Standard Actions

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:

  1. standard

  2. 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.

 

Standard actions

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:

  1. jsp:useBean

    • declares a JavaBean instance and associates this with a variable name.

  2. jsp:getProperty

    • read properties on bean set up using the <jsp:useBean> standard action

  3. jsp:setProperty

    • write properties on bean set up using the <jsp:useBean> standard action

  4. jsp:include

    • can be used to include the response from another file within your JSP page output

  5. jsp:forward

    • forward processing to another resource within the web application

  6. jsp:params

    • add additional parameters to the request and include it in the body of a <jsp:include> or <jsp:forward>

  7. 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.

  8. jsp:body

  9. jsp:element

  10. jsp:fallback

  11. jsp:output

  12. jsp:param

  13. jsp:plugin

 

We will discuss about some important standard actions.

 

<jsp:useBean>

  1. Declares a JavaBean instance and associates this with a variable name.

  2. Attributes are

    1. id

    2. class

    3. scope

      • values: page, request, session, application

      • default is page.

    4. type

    5. beanName

  3. 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.

  4. Eg. <jsp:useBean id="theDog" class="animals.Dog" />

    1. The attribute class must specify the fully qualified name of a class.

    2. animals.Dog must obey JavaBean conventions.

    3. 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.

  5. All ids for beans on a page must be unique.

  6. The jave code in the _jspService() method can reference the object created by <jsp:useBean> in two ways:

    1. As a local variable in the method, whose name comes from value of the id attribute.

    2. 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.

  7. Eg. <jsp:useBean id="theDog" class="animals.Dog" />

    1. theDog becomes a local variable.

    2. theDog is also the name of an attribute.

  8. We can specify scope of the attribute created by <jsp:useBean>.

    1. The valid values for scope are page, request, session, and application.

    2. 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.

    3. We can specify the scope as below:

      • <jsp:useBean id="theDog" class="animals.Dog" scope="session" />

    4. If we don’t specify the scope, the scope of the attribute created by <jsp:useBean> will be page scope.

    5. 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>

  1. <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.

  2. Attributes of <jsp:setProperty> standard action are

    1. name

      1. name of the bean

    2. property

      1. property for which we need to assign the value

    3. value.

      1. value attribute supplies the data for the property through the setter method of the property object.

    4. param

      1. can use param attribute instead of value to set the value from a request parameter

  3. E.g. 1: <jsp:setProperty name="theDog" property="weight" value="6.4" />

  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.

  5. E.g. 2: <jsp:setProperty name="theDog" property="weight" param="dogWeight" />

    1. 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:

  6. E.g. 3: <jsp:setProperty name="theDog" property="weight" value="<%= request.getParameter("dogWeight") %>" />

    1. Because the property here is “weight,” then the underlying code will assume the existence of a setWeight() method on the theDog bean.

  7. Instead of supplying a literal value, you can even substitute an expression. But this feature is not available to any other attributes.

    1. <% float w = 6.4f; %>

    2. <jsp:setProperty name="theDog" property="weight" value="<%=w%>" />

  8. 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.

  9. E.g. <jsp:setProperty name="theDog" property="weight" /> ?

  10. 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.”

  11. We can also use wild cards within property element.

  12. E.g. <jsp:setProperty name="theDog" property="*" />

  13. Any property whose name matches a request parameter name will have its value preloaded from that request parameter.

 

<jsp:getProperty>

  1. You use it to output the value of a bean’s property to the response.

  2. There are two attributes to supply:

    1. name

      1. the name of the bean

    2. property

      1. the name of the property.

  3. They’re both mandatory.

  4. E.g. <jsp:getProperty name="theDog" property="weight" />

 

Important notes on <jsp:useBean>, <jsp:setProperty> and <jsp:getProperty>

  1. We can use <jsp:setProperty> and <jsp:getProperty> without a previous <jsp:useBean> if an attribute of the right name exists in the PageContext.

  2. 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.

  3. 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>

  1. The <jsp:include> can be used to include the response from another file within your JSP page output.

  2. <jsp:include> Standard Action has two attributes

    1. page

      1. Page to include

    2. flush

      1. commit the response so far before anything is done about including the target page.

  3. 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.

  4. We cannot include an external file outside the web application using the <jsp:include> standard action.

  5. We can include non-jsp files using the <jsp:include> standard action.

  6. You can specify the value of the page attribute from a parameter as <jsp:include> runs at request time.

  7. E.g. <jsp:include page='<%= request.getParameter("thePage") %>' />

 

<jsp:forward>

  1. The purpose of this standard action is to forward processing to another resource within the web application.

  2. There is only one mandatory attribute, which is page="URL."

  3. The JSP specification says that a “<jsp:forward> effectively terminates the current page,”. Therefore any line after “<jsp:forward> won’t be executed.

  4. 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:

    1. There is no buffer, and even one character has been written to the response.

    2. The buffer has been explicitly flushed (response.flushBuffer()).

    3. The buffer has been automatically flushed on filling up (in a JSP, this will happen by default).

 

<jsp:params>

  1. 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>.

  2. 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.

  3. They don’t replace existing parameters of the same name—they merely augment the list of values.

 

<jsp:attribute>

  1. 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.

  2. E.g. <jsp:attribute name="attributeName" [ trim= "true | false" ] />

  3. 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.

  4. 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.

Tags: 

Search the Web

Custom Search

Searches whole web. Use the search in the right sidebar to search only within javajee.com!!!