Section 14.4. Actions

   

14.4 Actions

To begin with, there are few JSP tags with XML-compatible syntax. These most closely resemble ColdFusion tags and take this form:

 <jsp:tagname attribute="value" /> 

A commonly used JSP tag that takes this form has a direct counterpart in ColdFusion's <cfinclude> tag: <jsp:include/> , which looks like this:

 <jsp:include page="header.jsp" /> 

Calling this tag includes the content of header.jsp directly in the calling page.

You notice a couple of things here. First, there is a colon separating "jsp" and the name of the tag. This syntax allows developers to create coherent names for their own custom tags. For instance, you might write a custom tag to handle the checkout process for a user 's shopping cart. It might look like this:

 <ecommerce:cart action="checkout"/> 

We'll address custom actions later. The second thing we notice about the tag is that, because it is XML-compatible, it is closed with a trailing slash, indicating that there is no separate closing tag.

Now let's look at the tags themselves and then see how they work in some examples. Much of what you know in working with ColdFusion Web application design will be useful in working with JSPs.

14.4.1 <jsp:forward>

This action is used to end the execution of the current template and forward the request to another URL. The specified URL can be another JSP, an HTML page, a servlet, or other Web resource. The resource is specified as relative to the root of the current application.

Usage:

 <jsp:forward page="/javaforcf/secure.jsp" /> 

We have seen the forwarding mechanism at work already, in our servlet example. While we used the forward() method of the servlet request object in that case, that is what is happening under the hood here. Notice too that method is available in ColdFusion MX.

You can also use the <jsp:param> action to forward request parameters to the specified page:

 <jsp:forward page = "/javaforcf/secure.jsp">    <jsp:param name="userid" value="007" /> </jsp:forward> 

The </jsp:forward> action attribute is shown in Table 14.2.

Table 14.2. <jsp:forward> Action Attribute

Attribute

Description

Notes

page

Page location

(required) Specifies the location of the page to forward to, relative to the root of the application.

The <jsp:forward> tag works like <cflocation> .

14.4.2 <jsp:include>

This action is used to include a static or dynamically referenced file at run time. Because of when the file is included, files included using the <jsp:include> action do not share page or request -scoped variables with the calling page. For this reason, files included with the action cannot process directives. This is in distinction to the page include directive we saw previously in this chapter.

Usage:

 <jsp:include file="header.jsp" flush="true" /> 

The <jsp:include> action attributes are shown in Table 14.3.

Table 14.3. <jsp:include> Action Attributes

Attribute

Description

Notes

page

Page location

(required) Specifies location of the file to be included.

flush

Flush buffer

(optional) Specifies whether or not to flush the output buffer before including the file. Default value is false .

Note that the page attribute can be the result of a runtime expression; that is, it can be dynamically specified. In JSP 1.1, the flush attribute was required, and its only possible value was true .

Use the <jsp:include> action when you would use <cfinclude> . It is perfect for including headers, navigation bars, and footers, for instance. Also, you can append parameters to the original request with the <jsp:param> action. If the parameter already exists, the new value overwrites it. Multiple parameters can be specified in a comma-separated list.

14.4.3 <jsp:param>

This action allows you to pass parameters in the request scope. It is used within the bodies of <jsp:forward> , <jsp:include> , or <jsp:plugin> .

Usage:

 <jsp:param name="userid"     value='<%=request.getParameter(\"userid\")%>" /> 

The <jsp:param> action attributes are shown in Table 14.4.

Table 14.4. <jsp:param> Action Attributes

Attribute

Description

Notes

name

Parameter name

(required) Name of the parameter.

value

Parameter value

(required) Value of the parameter.

14.4.4 <jsp:params>

The <jsp:params> action wraps multiple <jsp:param> actions for passing to an applet.

Usage:

 <jsp:params>    <jsp:param name="Dept" value="Engineering" />   <jsp:param name="Emp" value="Dilbert" /> </jsp:params> 

14.4.5 <jsp:plugin>

The <jsp:plugin> action allows you to embed an applet or JavaBeans component directly in the JSP returned to the browser. It automatically generates the <embed> or <object> tags for you.

Usage:

 <jsp:plugin    type = "applet"   code = "somClass.class"   codebase = "intranet"   width = "300"   height = "400" /> 

The <jsp:plugin> action attributes are shown in Table 14.5.

Table 14.5. <jsp:plugin> Action Attributes

Attribute

Description

Notes

align

Alignment

(optional) Specifies alignment of applet area.

archive

Applet classes

(optional) Additional classes or resources, specified by URL, that must be loaded.

code

Class name

(required) Specifies name of the class containing the applet or bean.

codebase

Class location

(required) Specifies relative URL location of the class specified in the code attribute.

height

Height in pixels

(optional) Specifies height of the applet area.

hspace

Horizontal border in pixels

(optional) Specifies number of pixels wide the border on the side of the applet should be.

iepluginurl

URL for IE plugin

(optional) Location of the Java plugin for Internet Explorer.

jreversion

JRE version

(optional) Specifies minimum requirement for the Java Runtime Engine version. Default is 1.1.

name

Applet name

(optional) A name for the applet to keep it distinguished from other applets or beans on the page.

nspluginurl

URL for Netscape plugin

(optional) Location of the Java plugin for Netscape.

title

Label

(optional) A text description for the applet, which can appear in mouseover.

type

Object type

(optional) Whether the attribute is an applet or bean.

vspace

Vertical border in pixels

(optional) Defines a pixel border above and below the applet.

width

Width in pixels

(optional) Width of the applet.

That covers all of the tags in the JavaServer Pages specification ”except for three. The remaining tags all have to do with JavaBeans, and we cover that in its own section, so those tags will be introduced at that point.

What we quickly notice is that there is no <jsp:query> action, no <jsp:file> action. That means that much of what developers are used to writing in JSP is with JavaBeans. We've seen in this section a few different methods that you use with beans ” useBean , setProperty , and getProperty . We will examine beans more later in this chapter. For now, you can think of them as being like ColdFusion Components.


   
Top


Java for ColdFusion Developers
Java for ColdFusion Developers
ISBN: 0130461806
EAN: 2147483647
Year: 2005
Pages: 206
Authors: Eben Hewitt

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net