Action Tags in JSP
The Action tags are specific to a JSP page. When a JSP container encounters an action tag while translating a JSP page into a servlet, it generates a Java code corresponding to the task to be performed by the action tag.
The following are some important action tags available in JSP page:
The <jsp:include> action tag facilitates Java programmers in including a static or dynamic resource, such as an HTML or JSP page in the current JSP page.
If the resource to be included is static then its content directly included.
If the resource is dynamic then first it is processed and the result included on the page.
<jsp:include page=”test.jsp” />
The <jsp:forward> tag forwards a JSP request to another resource which can be either static or dynamic.
If the request is forwarded to a dynamic resource, a <jsp: param> tag can be used to pass a name and value of a parameter to a resource.
<jsp:forward page=”/header.html” />
Also, The <jsp: param> tag allows Java programmers to pass a name and value of a parameter to a dynamic resource while including it in a JSP page or forwarding a request to another JSP page.
Example for Action tags
<jsp:param name=”uname” value=”PM”/>
To separate the business logic from the presentation logic, it is often a good idea to encapsulate the business logic in Java object and then instantiate and use this Java object within a JSP page, <jsp:useBean> tag helps in such task.
Also, The <jsp:useBean> action tag has certain attributes that add extra characteristics to it, some attributes specific to <jsp:useBean> are:
Id: represents the name assigned to a JavaBean, which later used as a variable to access the JavaBean
Moreover, Class: takes the qualified class name to create a JavaBean instance if the JavaBean instance not found in the given scope.
beanName Action tags
takes the qualified class name or expression of a JavaBean to create a JavaBean.
Moreover, Class & beanName cannot be used together.
Also, We can not use expressions to create the instance of JavaBean from class attribute. For that we have to use beanName attribute.
The scope represents the scope in which a JavaBean instance has to created.
Page scope indicates that a JavaBean can used where <jsp:useBean> action tag is used.
Request scope indicates that a JavaBean can use from any JSP page. Which processes the same request until a response sent to a client by the JSP page.
Session scope indicates that a JavaBean can use. From any JSP page invoked in the same session as the JSP page that created the JavaBean.
Application scope indicates that a JavaBean can use from any JSP page in the same application. As the JSP page that created the JavaBean.
<jsp:useBean id=”myBean” class=”MyBeanClass” scope=”session”>
The <jsp:setProperty> action tag sets the value of a property by using the setter method of a JavaBean.
Before using the <jsp:setProperty> action tag, the JavaBean must be instantiated.
<jsp:setProperty name=”beanName” property=”uName” value=”PM” />
The <jsp:setProperty> action tag retrives the value of a property by using the getter method of a JavaBean and writes the value of the current JspWriter.
<jsp:getProperty name=”myBean” property=”uName” />