JSP - Actions

JSP actions use constructs in XML syntax to control the behavior of the servlet engine. You can dynamically insert a file, reuse JavaBeans components, forward the user to another page, or generate HTML for the Java plugin.

There is only one syntax for the Action element, as it conforms to the XML standard:

<jsp:action_name attribute="value" />

Action elements are basically predefined functions and there are following JSP actions available:

SyntaxPurpose
jsp:includeIncludes a file at the time the page is requested
jsp:useBeanFinds or instantiates a JavaBean
jsp:setPropertySets the property of a JavaBean
jsp:getPropertyInserts the property of a JavaBean into the output
jsp:forwardForwards the requester to a new page
jsp:plugin Generates browser-specific code that makes an OBJECT or EMBED tag for the Java plugin
jsp:elementDefines XML elements dynamically.
jsp:attributeDefines dynamically defined XML element’s attribute.
jsp:bodyDefines dynamically defined XML element’s body.
jsp:textUse to write template text in JSP pages and documents.

Common Attributes:

There are two attributes that are common to all Action elements: the id attribute and the scope attribute.

  • Id attribute: The id attribute uniquely identifies the Action element, and allows the action to be referenced inside the JSP page. If the Action creates an instance of an object the id value can be used to reference it through the implicit object PageContext
  • Scope attribute: This attribute identifies the lifecycle of the Action element. The id attribute and the scope attribute are directly related, as the scope attribute determines the lifespan of the object associated with the id. The scope attribute has four possible values: (a) page, (b)request, (c)session, and (d) application.

The <jsp:include> Action

This action lets you insert files into the page being generated. The syntax looks like this:

<jsp:include page="relative URL" flush="true" />

Unlike the include directive, which inserts the file at the time the JSP page is translated into a servlet, this action inserts the file at the time the page is requested.

Following is the list of attributes associated with include action:

AttributeDescription
pageThe relative URL of the page to be included.
flushThe 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:

Following is the content of date.jsp file:

<p>
   Today's date: <%= (new java.util.Date()).toLocaleString()%>
</p>
Here is the content of `main.jsp` file:
<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`. This would display result something like this:

The include action Example

   Today's date: 12-Sep-2010 14:54:22

The <jsp:useBean> Action

The useBean action is quite versatile. It first searches for an existing object utilizing the id and scope variables. If an object is not found, it then tries to create the specified object.

The simplest way to load a bean is as follows:

<jsp:useBean id="name" class="package.class" />

Once a bean class is loaded, you can use jsp:setProperty and jsp:getProperty actions to modify and retrieve bean properties.

Following is the list of attributes associated with useBean action:

AttributeDescription
classDesignates the full package name of the bean.
typeSpecifies the type of the variable that will refer to the object.
beanNameGives 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:getProperty actions before giving a valid example related to these actions.

The <jsp:setProperty> Action

The setProperty action sets the properties of a Bean. The Bean must have been previously defined before this action. There are two basic ways to use the setProperty action:

You can use jsp:setProperty after, but outside of, a jsp:useBean element, as below:

<jsp:useBean id="myName" ... />
...
<jsp:setProperty name="myName" property="someProperty" .../>
In this case, the `jsp:setProperty` is executed regardless 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:
<jsp:useBean id="myName" ... >
...
   <jsp:setProperty name="myName" property="someProperty" .../>
</jsp:useBean>
Here, the jsp:setProperty is executed only if a new object was instantiated, not if an existing one was found. Following is the list of attributes associated with setProperty action: Attribute Description name Designates the bean whose property will be set. The 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 The value that is to be assigned to the given property. The the parameter’s value is null, or the parameter does not exist, the setProperty action is ignored. param The 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. The Action The getProperty action is used to retrieve the value of a given property and converts it to a string, and finally inserts it into the output. The getProperty action has only two attributes, both of which are required ans simple syntax is as follows:
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值