7jsp Actions
7jsp Actions
JSP actions use constructs in XML syntax to control the behavior of the servlet eng ine. You can dynamically
insert a file, reuse JavaBeans components, forward the user to another pag e, or g enerate HT ML for the Java
plug in.
T here is only one syntax for the Action element, as it conforms to the XML standard:
Action elements are basically predefined functions and there are following JSP actions available:
Syntax Purpose
Common Attributes:
T here are two attributes that are common to all Action elements: the id attribute and the sc ope attribute.
Id attribute: T he id attribute uniquely identifies the Action element, and allows the action to be
referenced inside the JSP pag e. If the Action creates an instance of an object the id value can be used to
reference it throug h the implicit object Pag eContext
Sc ope attribute: T his attribute identifies the lifecycle of the Action element. T he id attribute and the
scope attribute are directly related, as the scope attribute determines the lifespan of the object associated
with the id. T he scope attribute has four possible values: (a) pag e, (b)request, (c)session, and (d)
application.
Unlike the inc lude directive, which inserts the file at the time the JSP pag e is translated into a servlet, this action
inserts the file at the time the pag e is requested.
flush T he boolean attribute determines whether the included resource has its
buffer flushed before it is included.
Example:
Let us define following two files (a)date.jps and (b) main.jsp as follows:
<p>
Today's date: <%= (new java.util.Date()).toLocaleString()%>
</p>
<html>
<head>
<title>The include Action Example</title>
</head>
<body>
<center>
<h2>The include action Example</h2>
<jsp:include page="date.jsp" flush="true" />
</center>
</body>
</html>
Now let us keep all these files in root directory and try to access main.jsp. T his would display result something
like this:
<jsp:useBean />
Once a bean class is loaded, you can use jsp:setProperty and jsp:g etProperty actions to modify and
retrieve bean properties.
type Specifies the type of the variable that will refer to the object.
beanName Gives the name of the bean as specified by the instantiate () method of the
java.beans.Beans class.
Let us discuss about jsp:setProperty and jsp:g etProperty actions before g iving a valid example related
to these actions.
You can use jsp:setProperty after, but outside of, a jsp:useBean element, as below:
In this case, the jsp:setProperty is executed reg ardless of whether a new bean was instantiated or an existing
bean was found.
A second context in which jsp:setProperty can appear is inside the body of a jsp:useBean element, as below:
Here, the jsp:setProperty is executed only if a new object was instantiated, not if an existing one was found.
name Desig nates the bean whose property will be set. T he Bean must have been
previously defined.
property Indicates the property you want 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 T he value that is to be assig ned to the g iven property. T he the parameter's
value is null, or the parameter does not exist, the setProperty action is
ig nored.
param T he param attribute is the name of the request parameter whose value the
property is to receive. You can't use both value and param, but it is
permissible to use neither.
T he g etProperty action has only two attributes, both of which are required ans simple syntax is as follows:
name T he name of the Bean that has a property to be retrieved. T he Bean must
have been previously defined.
Example:
Let us define a test bean which we will use in our example:
/* File: TestBean.java */
package action;
Compile above code to g enerated T estBean.class file and make sure that you copied T estBean.class in
C:\apache-tomcat-7.0.2\webapps\WEB-INF\classes\action folder and CLASSPAT H variable should also be
set to this folder:
Now use the following code in main.jsp file which loads the bean and sets/g ets a simple String parameter:
<html>
<head>
<title>Using JavaBeans in JSP</title>
</head>
<body>
<center>
<h2>Using JavaBeans in JSP</h2>
<jsp:useBean />
<jsp:setProperty name="test"
property="message"
value="Hello JSP..." />
<p>Got message....</p>
</center>
</body>
</html>
pag e Should consist of a relative URL of another resource such as a static pag e,
another JSP pag e, or a Java Servlet.
Example:
Let us reuse following two files (a) date.jps and (b) main.jsp as follows:
<p>
Today's date: <%= (new java.util.Date()).toLocaleString()%>
</p>
<html>
<head>
<title>The include Action Example</title>
</head>
<body>
<center>
<h2>The include action Example</h2>
<jsp:forward page="date.jsp" />
</center>
</body>
</html>
Now let us keep all these files in root directory and try to access main.jsp. T his would display result something
like as below. Here it discarded content from main pag e and displayed content from forwarded pag e only.
If the needed plug in is not present, it downloads the plug in and then executes the Java component. T he Java
component can be either an Applet or a JavaBean.
T he plug in action has several attributes that correspond to common HT ML tag s used to format Java
components. T he <param> element can also be used to send parameters to the Applet or Bean.
<jsp:fallback>
Unable to initialize Java Plugin
</jsp:fallback>
</jsp:plugin>
You can try this action using some applet if you are interested. A new element, the <fallback> element, can be
used to specify an error string to be sent to the user in case the component fails.
<html xmlns="http://www.w3c.org/1999/xhtml"
xmlns:jsp="http://java.sun.com/JSP/Page">
<jsp:text>Template data</jsp:text>
T he body fo the template cannot contain other elements; it can only contain text and EL expressions ( Note: EL
expressions are explained in subsequent chapter). Note that in XML files, you cannot use expressions such as
${whatever > 0}, because the g reater than sig ns are illeg al. Instead, use the g t form, such as ${whatever g t 0}
or an alternative is to embed the value in a CDAT A section.
<jsp:text><![CDATA[<br>]]></jsp:text>
If you need to include a DOCT YPE declaration, for instance for XHT ML, you must also use the <jsp:text>
element as follows:
<jsp:text><![CDATA[<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"DTD/xhtml1-strict.dtd">]]>
</jsp:text>
<head><title>jsp:text action</title></head>
<body>
<books><book><jsp:text>
Welcome to JSP Programming
</jsp:text></book></books>
</body>
</html>