JSP syntax (2 )--

Source: Internet
Author: User

<JSP: Forward>: redirects an HTML file, JSP file, or program segment.

JSP syntax
<JSP: Forward page = {"relativeurl" | "<% = expression %>"}/>
Or
<JSP: Forward page = {"relativeurl" | "<% = expression %>"}>
<JSP: Param name = "parametername" value = "{parametervalue | <% = expression % >}"/> +
</Jsp: Forward>

 

Example

<JSP: Forward page = "/servlet/login"/>

<JSP: Forward page = "/servlet/login">
<JSP: Param name = "username" value = "jsmith"/>
</Jsp: Forward>

Description
<JSP: Forward> the tag transmits a request object containing user requests from one JSP file to another.

<JSP: Forward> the code below the tag cannot be executed.

You can send parameters and values to the target file. In this example, the parameter name is username and the value is Scott. If you use the <JSP: param> label, the target file must be a dynamic file that can process parameters.

If you use non-buffered output, be careful when using <JSP: Forward>. If the JSP file already has data before you use <JSP: Forward>, the file execution will fail.

Attribute

 

Page = "{relativeurl | <% = expression %> }"
Here is an expression or a string used to indicate the file or URL you want to target. this file can be JSP, program segment, or other files (such as ASP, CGI, PHP) that can process request objects ).

 

<JSP: Param name = "parametername" value = "{parametervalue | <% = expression % >}"/> +
Send one or more parameters to a dynamic file. This file must be a dynamic file.

If you want to pass multiple parameters, you can use multiple <JSP: param> In a JSP file. Name specifies the parameter name and value specifies the parameter value.

 

<JSP: getproperty>: gets the bean property value, which is displayed on the page.

JSP syntax

<JSP: getproperty name = "beaninstancename" property = "propertyname"/>
Example
<JSP: usebean id = "calendar" Scope = "page" class = "employee. Calendar"/>

<H2> calendar of <JSP: getproperty name = "calendar" property = "username"/> </H2>
Description

The <JSP: getproperty> element obtains the bean property value and can be used or displayed on the JSP page. before using <JSP: getproperty>, you must use <JSP: usebean> to create it.
<JSP: getproperty> elements have some restrictions:

You cannot use <JSP: getproperty> to retrieve an indexed property.

You can use <JSP: getproperty> with the JavaBeans component, but not with the enterprise Bean.

 

 

Attribute

 

 

Name = "beaninstancename"
Bean name, specified by <JSP: usebean>

 

 

Property = "propertyname"
Attribute name of the specified bean.

 

 

TIPS:

 

As mentioned in Sun's JSP reference, if you use <JSP: getproperty> to retrieve a value that is null, nullpointerexception will occur. If you use a program segment or expression to retrieve the value, in the browser, null is displayed ).

<JSP: Include>: contains a static or dynamic file.

JSP syntax
<JSP: Include page = "{relativeurl | <% = expression %>}" Flush = "true"/>

Or

<JSP: Include page = "{relativeurl | <% = expression %>}" Flush = "true">

<JSP: Param name = "parametername" value = "{parametervalue | <% = expression % >}"/> +

</Jsp: Include>

 

Examples

<JSP: Include page = "scripts/login. jsp"/>

<JSP: Include page = "copyright.html"/>

<JSP: Include page = "/index.html"/>

 

<JSP: Include page = "scripts/login. jsp">

<JSP: Param name = "username" value = "jsmith"/>

</Jsp: Include>

 

 

 

Description

<JSP: Include> elements allow you to include dynamic files and static files. The results of these two types of files are different. If the file is only a static file, this inclusion only adds the content of the contained file to the JSP file. If the file is dynamic, the included file will be executed by the JSP compiler (similar to ASP)

You cannot determine whether a file is dynamic or static from the file name, such as aspcn. asp.

It may only contain some information and does not need to be executed. <JSP: Include> these two types of files can be processed at the same time. Therefore, you do not need to determine whether the file is dynamic or static.

If the file is dynamic, you can also use <JSP: param> to pass the parameter name and parameter value.

 

 

Attribute

 

Page = "{relativeurl | <% = expression %> }"
The parameter is a relative path or an expression that represents a relative path.

 

 

Flush = "true"
Here you must use flush = "true". You cannot use false. The default value is false.

 

 

<JSP: Param name = "parametername" value = "{parametervalue | <% = expression % >}"/> +
<JSP: param> the clause allows you to pass one or more parameters to a dynamic file.

You can use multiple <JSP: param> in a single page to pass multiple parameters.

<JSP: usebean id = "news" Scope = "request" class = "com. Backing. newsbean">
<JSP: setproperty name = "news" property = "newsid" value = "1"> </jsp: setproperty>
<JSP: setproperty name = "news" property = "currentpage" value = "1"> </jsp: setproperty>
</Jsp: usebean>

<JSP: usebean>: Create a bean instance and specify its name and scope.

JSP syntax

<JSP: usebean id = "beaninstancename" Scope = "Page | request | session | application"

{Class = "package. Class" | type = "package. Class" | class = "package. Class"

Type = "package. Class" | beanname = "{package. Class | <% = expression %>}" type = "package. Class"

} {/> |> Other elements </jsp: usebean>}

 

Example

<JSP: usebean id = "cart" Scope = "session" class = "session. Carts"/>

<JSP: setproperty name = "cart" property = "*"/>

<JSP: usebean id = "checking" Scope = "session" class = "bank. Checking">

<JSP: setproperty name = "checking" property = "balance" value = "0.0"/>

</Jsp: usebean>

Description

<JSP: usebean> is used to locate or sample A JavaBeans component. <JSP: usebean> first tries to locate a bean instance. If this bean does not exist, <JSP: usebean> will take an example from a class or template.

To locate or sample a bean, <JSP: usebean> performs the following steps:

 

Attempts to locate a bean by specifying the name and range.
The variables referenced in this bean object are named by the name you specified.
If this bean is found, the reference will be stored in this variable. If you also specify the type, the bean is also set to the corresponding type.
If this bean is not found, it will be used from the class you specified and stored in a new variable. If the class name represents a template, the bean is used by Java. Beans. Beans. instantiate.
If <JSP: usebean> has been used as an example (not to locate) bean, and <JSP: usebean> and </jsp: usebean> have elements, the code will be executed.

The main body of the <JSP: usebean> element usually contains the <JSP: setproperty> element, which is used to set the bean property value. As mentioned in step 5 above, the main body of <JSP: usebean> will only be executed when <JSP: usebean> sample bean. If this bean already exists, <JSP: usebean> can locate it, so the content in the subject will not work

Attributes and usage

Id = "beaninstancename"
Confirm the bean variable in the scope you define. You can use this variable name in subsequent programs to distinguish different beans.

This variable name is case sensitive and must comply with the requirements of your scripting language.

In programming language, this rule is in Java language

The specification has been stated. If this bean has been created in another <JSP: usebean>, the ID value must be consistent with the original ID value.

Scope = "Page | request | session | application"
The valid range of bean and ID variable name. The default value is page, which is described as follows:

 

Page-you can use the bean in the JSP file that contains the <JSP: usebean> element and all static include files in the file, after the page is executed, send a response to the client or go to another file.
Request-you can use beans in any JSP file that executes the same request until the page execution is complete and the response is sent back to the client or transferred to another file. You can use the request object to access beans, such as request. getattribute (beaninstancename)
Session-from bean creation, you can use bean in any JSP file that uses the same session. this bean exists throughout the session lifecycle. Any JSP file that shares this session can use the same bean. note that session = true must be specified in the <% @ Page %> command in the JSP file of the created bean.

 

Application-from bean creation, you can use bean in any JSP file that uses the same application. this bean exists throughout the application lifecycle. Any JSP file that shares this application can use the same bean.
Class = "package. Class"
Use the New Keyword and the class constructor to sample a bean from a class. this class cannot be abstract and must have a common constructor without parameters. the package name is case sensitive.

Type = "package. Class"
If the bean already exists in the specified range, write this bean to a new database type.

. If you do not use the class or beanname to specify the type, bean will not be named by the example. Package and class names, case sensitive.

Beanname = "{package. Class | <% = expression %>}" type = "package. Class"
Use the java. Beans. Beans. instantiate method to sample a bean from a class or continuous template, and specify the bean type.

Beanname can be package or class, and its value will be passed to beans. instantiate. tupe with the same value as bean.

The package and class names are case sensitive.

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.