It is necessary to control the servlet engine's behavior, which can be controlled dynamically by inserting the file by reusing the JavaBeans components or redirecting or forwarding the user to another page., i.e., by forwarding the request to another resource, which will possibly be a JSP, HTML, or other resources. In this chapter, you will learn about the concept of Action and Action tags that make this forwarding possible for processing.



Actions Tags

The JSP specification provides a standard tag called Action tag used within JSP code and is used to remove or eliminate Scriptlet code from your JSP code as JSP Scriptlets are obsolete and are not considered nowadays. There are many JSP action tags or elements, and each of them has its own uses and characteristics. Each JSP action tag is implemented to perform some precise tasks.

The action tag is also implemented to streamline flow between pages and to employ a Java Bean. As it coincides with the XML standard, the syntax for the action element is:

Syntax:

<jsp:action_name attribute = "attribute_value" />

Here is the list of JSP Actions:

  1. jsp:forward: is used for forwarding the request and response to other resources.
  2. jsp:include: is used for including another resource.
  3. jsp:body: is used for defining dynamically-defined body of XML element.
  4. jsp:useBean: is used for creating or locating bean objects.
  5. jsp:setProperty: is used for setting the value of property in the bean object.
  6. jsp:getProperty: is used for printing the value of the property of the bean.
  7. jsp:element: is used for defining XML elements dynamically.
  8. jsp:plugin: is used for embedding other components (applets).
  9. jsp:param: is used for setting the parameter for (forward or include) value.
  10. jsp:text: is used for writing template text in JSP pages and documents.
  11. jsp:fallback: is used for printing the message if plugins are working.
  12. jsp:attribute: is used for defining attributes of dynamically-defined XML element.

Basic Attributes of Action Tags

Two basic attributes are commonly used for all action tags. These are:

  1. id: The id attribute defines unique action elements and allows actions to be referenced within the JSP page. When the action creates an object's instance, the id attribute is used to refer to it.
  2. Scope: The scope attribute is used for identifying an action's life cycle. It correlates with the id attribute because the scope attribute is used to establish that particular object's lifespan associated with the ID.

Here the syntax and implementation of some actions are described below:

jsp:include Action

This "include action" allows you to include another resource in the page being generated.

Syntax:

<jsp:include page = "Page URL"  flush = "Boolean Value" />

Example:

<!DOCTYPE html>
<html>
    <head>
        <title>JSP include Action</title>
    </head>
    <body>
        <h2>JSP page: with include</h2>
        <jsp:include page="header.jsp" flush="false" />
    </body>
</html>

jsp:forward Action

This "forward action" terminates the current page and allows you to forward the request to other resources.

Syntax:

<jsp:forward page = "URL of another static, JSP, OR Servlet page" />

Example:

<!DOCTYPE html>
<html>
    <head>
        <title>Example for Forward Action</title>
    </head>
    <body>
        <h2>JSP page: Demo forward</h2>
        <jsp:forward page="forward_eg.jsp" />
    </body>
</html>

It is to be noted that this JSP file and forward_eg.jsp file should have to reside in the same directory to make it work.

jsp:param Action

This "param action" is used for setting the parameter for (forward or include) value.

Syntax:

<jsp: param name = "param_name" value = "value_of_parameter" />

Example:

<!DOCTYPE html>
<html>
    <head>
        <title>Example of param Action</title>
    </head>
    <body>
        <h2>JSP page: Param with forward</h2>
        <jsp:forward page="forward_eg.jsp">
            <jsp:param name="date" value="06-09-2019" />
            <jsp:param name="time" value="10:30PM" />
            <jsp:param name="data" value="GKR" />
        </jsp:forward>
    </body>
</html>

jsp:useBean Action

This "useBean action": is used for creating or locating bean objects.

Syntax:

<jsp: useBean id="unique_name_to_identify_bean" class="package_name.class_name" />

Example:

<!DOCTYPE html>
<html>
    <head>
        <title>JSP Page for showing use of useBean action</title>
    </head>
    <body>
        <h2>UseBean Action</h2>
        <jsp:useBean id="teacher" class="packageName.Teacher" />
        <jsp:setProperty name="teacher" property="*" />
        <h2>
            name:<jsp:getProperty name="teacher" property="t_name" /> <br />
            empno:<jsp:getProperty name="teacher" property="dept" /> <br />
        </h2>
    </body>
</html>
Example: Java Class:
package samplePackageName;

public class Teacher {
    
    public Teacher() {}
    
    private String t_name;
    private int deptno;
    
    public void setName(String t_name) {
        this.t_name = t_name;
    }
    
    public String getName() {
        return t_name;
    }
    
    public void setDept(int deptno) {
        this.deptno = deptno;
    }
    
    public int getDept() {
        return deptno;
    }
}


Found This Page Useful? Share It!
Get the Latest Tutorials and Updates
Join us on Telegram