Don't worry, I will study JSP with you (4) -- basic syntax of JSP [2]

Source: Internet
Author: User

In recent days, my ADSL cannot be connected to the Internet. Almost at home, you can only buy discs to watch movies. I don't know how many mm flood occurs in the Forum. Originally, this post will be sent out on Wednesday. Unexpectedly, China Unicom solved the network yesterday. 10010 of customer service, I started to fight from home, until am at night, no one cares. Ah...

The last time we talked about the JSP command operation. This time, let's talk about the last JSP action. Why do we need to use a single chapter. There are two main reasons: one is that there are too many content last time, and you may not be able to accept it. Another point is that JSP actions do not need to know much about the content, but are generally more about the content. So we have to give a single chapter to talk about it.

As in the previous cloud, JSP actions are used to control JSP Engine behaviors and perform common JSP page actions. Similar to JSP commands. Commands are used for notifications and are not directly displayed, but actions must be controlled and pages must be created.

JSP actions include the following:

1. jsp: include: dynamic and controllable include loading, which is more flexible than the include command.

2. jsp: usebean: Use the JavaBean control.

3. jsp: setproperty: Set the JavaBean attribute.

4. jsp: getproperty: outputs the JavaBean attribute.

5. jsp: Param: used to pass parameters. It must be used with other tags that support this parameter.

6. jsp: Forward: directs the request to a new page.

7. jsp: Plugin: insert an applet or bean.

I. jsp: Include

1. Description:

JSP: include action dynamically inserts a file on the page to be generated. It inserts the file only when the page is running and processes the inserted file.

2. format:

<JSP: Include page = "file name" Flush = "true"/>

Or

<JSP: Include page = "file name" Flush = "true"> <JSP: Param name = "parameter name 1" value = "parameter value 1"/> <JSP: param name = "parameter name 2" value = "parameter value 2"/> ...... </Jsp: Include>

Parameter description:

(1) page = "file name"

Specifies the URL of the file to be inserted. this parameter is a relative path.

(2) flush = "true"

You can only use True.

(3) <JSP: param>

<JSP: param> the clause is used to upload the value to the file to be inserted.

On the corresponding page, you only need to add the following statement to retrieve the value:

Request. getparameter ("parameter name ");

3. Differences from include:

JSP: include actions are dynamic. When a file is inserted, the JSP engine does not merge the inserted file and the original JSP file into a new file, but includes the inserted file during runtime. Both the inserter and the inserted object are independent of each other. If the inserted file is changed, it will be determined and re-compiled.

The include command is too static. After the inserted file is inserted to the current location, it is compiled. Once changed, it cannot be known.

4. For example:

Index. jsp

<% @ Page contenttype = "text/html" pageencoding = "UTF-8" %> <! Doctype HTML> 

Head. jsp

<! Doctype HTML> 

Run the following command to check the result:

Ii. jsp: forward action

1. Description:

JSP: The forward action stops the execution of the current page and redirects to another page. During execution, the JSP Engine no longer processes the remaining content on the current page, and clears the cache area (equal to this page ). The advantage is that the customer sees the original page address, but actually displays the content of another page. You can hide the address of the page.

2. format:

<JSP: Forward page = "file name"/>

Or

<JSP: Forward page = "file name"> <JSP: Param name = "parameter 1" value = "parameter value 1"/> <JSP: param name = "parameter name 2" value = "parameter value 2"/> ...... </Jsp: Forward>

Parameter description:

(1) page = "file name"

Specifies the file or URL to be redirected.

(2) JSP: Param

The same as the JSP: Param action in the JSP: include action. Is to pass the value. The receiving method is as follows.

3. For example:

In the JSP: Include example, change the include in the index. jsp page to forward to see the result. You do not need to change head. jsp. The Code is as follows:

Index. jsp

<% @ Page contenttype = "text/html" pageencoding = "UTF-8" %> <! Doctype HTML> 

Head. jsp

<% @ Page contenttype = "text/html" pageencoding = "UTF-8" %> <! Doctype HTML> 

The result is:

In index. jsp, "How do I feel about the two game consoles ?" No. Only the information in head. jsp is available.

Iii. jsp: plugin action

1. Description:

JSP: the function of the plugin action is to download the Java applet or JavaBean Component on the server to the browser for execution, which is equivalent to inserting the Java Plug-in the client browser.

JSP: the plugin action will dynamically generate <Object> or <embed> element tags so that the browser's Java Plug-in can run the applet. It also specifies that the object is an applet or a Java Bean. It may also be a Java class bytecode file (. Class), or a Java Plug-in to be downloaded.

2. format:

<JSP: plugin type = "bean | applet" code = "save class file name" codebase = "class path" [name = "Object Name"] [archive = "related file path"] [align = "bottom | top | Middle | left | right"] [Height = "displaypixels"] [width = "displaypixels"] [hspace = "leftrightpixels"] [vspace = "topbottompixels "] [jreversion =" Java environment version "] [nspluginurl =" plugin adding domain location for NC "] [iepluginurl =" plugin loading location for IE "] <JSP: params> <JSP: Param name = "parameter 1" value = "parameter value 1"> <JSP: param name = "parameter name 2" value = "parameter value 2"> </jsp: Params> [<JSP: fallback> error message </jsp: fallback>] </jsp: plugin>

Parameter description:

(1) type = "bean | applet"

Specify whether the type of the plug-in object is bean or applet (either ).

(2) code = "Save the class file name"

The name of the Java class bytecode file (*. Class) to be executed. This file must be saved in the directory specified by the codebase attribute to be introduced below.

(3) codebase = "class path"

This specifies the location of the Java class bytecode file (*. class. If it is not written, it is the path of the current JSP page by default.

(4) Name = "Object Name"

Bean or applet Instance name

(5) archive = "related file path"

Some Java class bytecode files (*. Class) files to be used are pre-installed. Each file is separated by a comma (,) to improve the applet performance.

(6) align = "bottom | top | Middle | left | right"

Specifies the position of the displayed image and applet.

(7) Height = "displaypixels" width = "displaypixels"

The length and width of the display.

(8) hspace = "leftrightpixels" vspace = "topbottompixels"

It can be seen from the English letter translation that a horizontal spacing is set, and a vertical spacing is set.

(9) jreversion = "Java environment version"

The Java Runtime Environment (JRE) version of the applet or Java Bean they run. The default version is 1.1.

(10) nspluginurl = "plugin and domain location for NC"

This is to specify the JRE required by the Netscape Navigator user. It is a URL address.

(11) iepluginurl = "plugin loading location for IE"

The difference here is that the browser is IE.

(12) JSP: Params action

Similar to the JSP: Params action in the JSP: Include or JSP: forward action, a value is passed.

(13) <JSP: fallback> error message </jsp: fallback>

To put it bluntly, you just need to provide yourself with a path. If an error occurs, how can this problem be solved.

There are many things and few are used. If you want to use other attributes, you can check the list.

3. Example

We first found a Java Applet application in the same directory as index. jsp (clock. class ).

Then modify the code in index. jsp:

<% @ Page contenttype = "text/html" pageencoding = "UTF-8" %> <! Doctype HTML> 

Then, run the website and view the result:

It feels a bit like <APPLET> in HTML. However, it is more powerful and flexible than the HTML <APPLET>. Many developers who have worked on JSP development know that Java Bean serves as data and business logic operations in the early classic Java MVC project. Therefore, what is powerful is not just half a bit. The following is the code on the client:

<! Doctype HTML> 

4. jsp: usebean action

1. Description

Finally, let's talk about Bean. Java Bean is a reusable program component. It is a Java class, so it must be written in Java. JSP: bean action is used to call this program component. In fact, it feels a bit like the previous example. However, the Java Applet in the previous example only completes the display of a client, and bean is used as a server for function processing, this is invisible to us.

Although we will not be able to access Java Bean in the future, we can first describe what it is. Java Bean displays the website presentation layer (the page displayed to the customer, written in JSP .) Make public with the business logic layer, and process the business and data to bean. Finally, JSP calls the results provided by bean and displays them to the customer. This reduces the complexity of the program and facilitates future maintenance and redevelopment.

If you still don't understand it, let me change it to a method that is not suitable for children and is difficult to taste. JSP is the healthy beauty we see, and bean is the organ of the action in the beauty body. Whether a beauty is healthy depends on the results of the body's operation. The body is running well, and the bean is in action; the beauty of the appearance is the result of calling the bean action ...... You don't understand. I understand it...

It doesn't matter. I will see it later...

2. Format

<jsp:useBean id="beanInstanceName" class="package.class" scope="page | request | session | application" /></jsp:useBean>

If the parameter is set:

<JSP: usebean id = "beaninstancename" class = "package. class "Scope =" Page | request | session | application "/> <JSP: setproperty name = "bean name" property = "property name" value = "property value"/>... <JSP: setproperty name = "bean name" property = "property name" value = "property value"/> </jsp: usebean>

If you want to obtain the attribute value returned by the bean:

<JSP: getproperty name = "bean name" property = "property name"/>

3. Example

For this example, we need to compile the Java development environment. Therefore, we need to modify the system environment.

Modify as follows:

(1) Right-click "my computer" and select "properties" to open the Properties window.

(2) Select "Advanced System settings" in the "properties" window"

(3) Click the "advanced" tab in the "System Properties" Panel and select "environment variables"

(4) Set "system variables" in "environment variables:

1) Select the variable "path" and click the "edit" button. In the variable value, add "Java path \ bin", such as "; L: \ Program Files \ Java \ jdk1.6.0 _ 23 \ bin;", and then click "OK.

2) In terms of above, click to set the environment variable "classpath" (if there is no "classpath", click "new" to create one ).

Then, set the variable value .; java path \ JRE \ Lib \ RT. jar; for example, ".; l: \ Program Files \ Java \ jdk1.6.0 _ 23 \ JRE \ Lib \ RT. jar ;":

3) Click OK to exit. Maybe, you need to restart the computer (some, some, but not others ).

Now, open notepad and write Java Bean. Type the following Java code and save it as box. Java:

package bean;import java.io.*;public class Box{int length, width, height;public Box(){length = 1;width = 1;height = 1;}public void setlength(int newlength){length = newlength;}public void setwidth(int newwidth){width = newwidth;}public void setheight(int newheight){height = newheight;}public int getlength(){return length;}public int getwidth(){return width;}public int getheight(){return height;}public int BoxVolume(){return length * width * height;}public double BoxArea(){return 2 * (length * width + width * height + length * height);}}

OK. In the run dialog box, press Ctrl + R and Enter cmd and press Enter.

Then, in the open character command line, run the command to enter the directory saved by box. Java, and enter javac box. Java for compilation.

If there are no errors, the following situations will occur:

A new file "box. Class" will appear in the directory where box. Java is located ". This is the compiled and compiled Java Bean file.

Okay, now it's all done. Then we started to write JSP code. Open the netbeans editor and copy the line-based box. Class file to the bean folder in the classes folder of the WEB-INF folder under the website directory.

Now. Let's change the code of the index. jsp file. The Code is as follows:

<% @ Page contenttype = "text/html" pageencoding = "UTF-8" %> <! Doctype HTML> 

The head. JSP code is as follows:

<% @ Page contenttype = "text/html" pageencoding = "UTF-8" %> <% @ page import = "bean. box "%> <JSP: usebean id =" box "class =" bean. box "Scope =" request "> </jsp: usebean> <! Doctype HTML> 

Run the following command to input data in index. jsp:

Then click the calculation button to display the result:

In the compiler, we can see a lot of things in the directory. These things are very useful for server development. Next time, let's take a day to explain. Today, we will be here first.

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.