JSP actions controls the behavior of the underneath servlet engine.
With JSP actions we can dynamically insert a file, reuse JavaBeans components, or forward the user to another page.
The following code is the syntax for the Action element:
<jsp:action_name attribute="value" />
We can use the following JSP actions.
Syntax | Purpose |
---|---|
jsp:include | Includes a file |
jsp:useBean | Initialize a JavaBean |
jsp:setProperty | Sets the property of a JavaBean |
jsp:getProperty | Get the property from a JavaBean |
jsp:forward | Forwards the request to another page |
jsp:plugin | Generates an OBJECT or EMBED tag for the Java plugin |
jsp:element | Defines XML elements dynamically. |
jsp:attribute | Defines XML element's attribute. |
jsp:body | Defines XML element's body. |
jsp:text | Write template text in JSP pages and documents. |
There are two common attributes for all Action elements:
the id
attribute and the scope
attribute.
Id attribute uniquely identifies the Action element. We can use the id to reference the action in the JSP page.
Scope attribute identifies the lifecycle of the Action element.
The scope
attribute has four possible values:
jsp:include Action inserts files into the JSP pages. The syntax is listed as follows:
<jsp:include page="relative URL" flush="true" />
The include
directive includes a file at the time the JSP page is
translated into a servlet, while jsp:include action inserts the file at the time the page is requested.
The following table lists the attributes associated with include action.
Attribute | Description |
---|---|
page | The relative URL of the page to be included. |
flush | The boolean attribute determines whether the included resource should have its buffer flushed before including. |
The following code shows how to use include action.
Here is the data.jsp.
<p>
Today's date: <%= (new java.util.Date()).toLocaleString()%>
</p>
Here is the content of main.jsp file:
<html> <body> <jsp:include page="date.jsp" flush="true" /> </body> </html>
The useBean
action
first searches for an existing object using the id
and scope
attributes.
If an object is not found, it then will create the specified object.
The simplest way to load a bean is as follows:
<jsp:useBean id="name" class="java.lang.String" />
<jsp:useBean id="yourName" class="com.yourCom.YourClassName" />
Once a bean is loaded, we can use jsp:setProperty
and
jsp:getProperty
actions to modify and retrieve bean properties.
The following table list attributes associated with useBean action.
Attribute | Description |
---|---|
class | Defines the full package name of the bean. |
type | Specifies the type of the variable. |
beanName | Named the loaded bean. |
The setProperty
action sets the properties of a Bean.
We can use jsp:setProperty
after outside of a jsp:useBean element as follows.
<jsp:useBean id="myName" ... /> <jsp:setProperty name="myName" property="someProperty" .../>
Or we can include jsp:setProperty
inside the body of a jsp:useBean
element as follows.
<jsp:useBean id="myName" ... > ... <jsp:setProperty name="myName" property="someProperty" .../> </jsp:useBean>
In the code above, the jsp:setProperty is executed only if a new object was instantiated, not if an existing one was found.
The following table lists the attributes associated with setProperty
action.
Attribute | Description |
---|---|
name | The name of the bean whose property will be set. |
property | Property to set. A value of "*" means that all request parameters whose names match bean property names will be passed to the appropriate setter methods. |
value | Value to set to the property. |
param | The param attribute is the name of the request parameter. |
The getProperty
action retrieves the value of a given property
and converts it to a string, and then inserts it into the output.
The getProperty action has two attributes listed as follows:
<jsp:useBean id="myName" ... /> ... <jsp:getProperty name="myName" property="someProperty" .../>
Following is the list of required attributes associated with getProperty action.
Attribute | Description |
---|---|
name | The name of the Bean which must have been previously defined. |
property | The name of the Bean property. |
Create a JavaBean as follows.
package action; public class TestBean { private String message = "No message specified"; public String getMessage() { return(message); } public void setMessage(String message) { this.message = message; } }
Compile above code to TestBean.class file and copy TestBean.class to C:\apache-tomcat\webapps\WEB-INF\classes\action folder.
Save the following code to hellow.jsp file.
<html> <body> <jsp:useBean id="test" class="action.TestBean" /> <jsp:setProperty name="test" property="message" value="Hello JSP..." /> <jsp:getProperty name="test" property="message" /> </body> </html>
The forward
action forwards the request to another a static page, or another JSP page, or a Java Servlet.
The simple syntax of this action is as follows:
<jsp:forward page="relativeURL | <%= expression %>" />
The following table lists the required attributes for forward
action.
Attribute | Description |
---|---|
page | Relative URL for another resource. |
The following code shows how to use the forward action.
The date.jsp file:
<p>
Today's date: <%= (new java.util.Date()).toLocaleString()%>
</p>
Here is the main.jsp file:
<html>
<body>
<jsp:forward page="date.jsp" />
</body>
</html>
The plugin
action can insert Java applet,
wrapped in the <object> or <embed> tags, into a JSP page.
The following code lists the typical syntax of using plugin action.
<jsp:plugin type="applet" codebase="dirname" code="MyApplet.class" width="60" height="80"> <jsp:param name="fontcolor" value="red" /> <jsp:param name="background" value="black" /> <jsp:fallback> Unable to initialize Java Plugin </jsp:fallback> </jsp:plugin>
The <jsp:element>, <jsp:attribute> and <jsp:body> actions defines XML elements dynamically.
The following code shows how to define XML elements dynamically.
<%@page language="java" contentType="text/html"%> <html xmlns="http://www.w3c.org/1999/xhtml" xmlns:jsp="http://java.sun.com/JSP/Page"> <body> <jsp:element name="xmlElement"> <jsp:attribute name="xmlElementAttr"> Value </jsp:attribute> <jsp:body> Body </jsp:body> </jsp:element> </body> </html>