What are the common instructions of JSP (compile instructions / action instructions)

Time:2021-3-4

JSP contains three compile instructions and seven action instructions.
The three compiler instructions are: page, include and taglib.
The seven action commands are as follows: jsp:forward 、 jsp:param 、 jsp:include 、 jsp:plugin 、 jsp:useBean 、 jsp:setProperty 、 jsp:getProperty .
1、 Compiler instruction
1. Page instruction
a. It is usually located at the top of the JSP page, and the same page can have multiple page instructions. The syntax format is as follows:

Copy codeThe code is as follows:
〈% @page
[language=”Java”]
[extends=”package.class”]
[import= “package. class I package. *},…”]
[session=”true I false”]
[buffer=”none I 8kb I size kb” 1
[autoFlush=”true I false”]
[isThreadSafe=”true I false”]
[info=”text”]
[errorPage=”relativeURL”]
[contentType=”mimeType[ ;charset=characterSet]” I”text/html;charset= “808859-1”]
[isErrorPage=” true I false”]
%〉

1) The language attribute, Java in general, represents the scripting language used by JSP pages.
2) The extensions property determines the Java class generated by JSP program compilation, the parent class to be inherited, or the fully qualified class name of the interface to be implemented.
1. The import attribute is used to import packages. The following packages are automatically imported by default, and do not need to be explicitly imported. The default imported packages are:

Copy codeThe code is as follows:
java.lang.*
javax.servlet.*
javax.servlet.jsp.*
javax.servlet.http.*

2. Session property to set whether the JSP page needs htip session.
3. Buffer property, which specifies the size of the output buffer. JSP internal object of output buffer: out is used to cache the output of JSP page to client browser. The default value is 8KB, which can be set to none or other values, and the unit is KB
4. Autoflush attribute, when the output buffer is about to overflow, whether to force the content of the output buffer. If it is set to true, it is normal output; if it is set to false, an exception will be generated when the buffer overflows.
5. Info property. Setting the information of the JSP program can also be regarded as its description. The value can be obtained through the servlet. Getservletinfo() method. If you are in a JSP page, you can directly call the getservletinfo method to get the value, because the essence of a JSP page is a servlet.
The errorpage property specifies the error handling page. If the program has an exception or error, and the JSP page has no corresponding processing code, it will automatically call the JSP page specified by the instruction. When using JSP pages, you can not handle exceptions, even checked exceptions. (redirecting to the corresponding error handling page, but the URL is still the original URL, which does not change) if this property does not exist in this page, then once there is a problem with the code, an error will be prompted in the development environment and IE browser. It can be seen that the effect of exception handling controlled by this attribute is much better in the form of expression.
6. Iserrorpage property to set whether the JSP page is an error handler. If the page itself is an error handling page, you do not need to use the errorpage property.
7. Contenttype attribute, which is used to set the file format and encoding method of generating web pages, that is, MIME type and page character set type. The default MIME type is text / HTML; the default character set is iso-8859-1.
2. Include instruction
Using the include instruction, you can embed an external file into the current JSP file and parse the JSP statements (if any) in the page. This is a static include language that does not check for changes to the included JSP pages.
Include can contain both static text and dynamic JSP pages. The static compile instruction include is to add the included page to generate a complete page.
Syntax of the include compile instruction:
〈% @include file=”relativeURLSpec” %〉
If the embedded file needs to be changed frequently, it is recommended to use< jsp:include >Operation instruction, because it is a dynamic include statement.
After deployment, JSP pages containing include instructions are accessed and compiled to generate java files. Corresponding java files will be generated in Tomcat’s work / Catalina / localhost / project / org / Apache / JSP directory. These files have the same name as JSP. They contain compiled code, and even the code of another JSP included in include is added correspondingly.

2、 Action command
1. Forward instruction: forward instruction is used to forward page response control to another page. It can be forwarded to static HTML page, dynamic JSP page or servlet in container.
The format of JSP forward instruction is as follows:
For JSP 1.0, use the following syntax:

Copy codeThe code is as follows:
<jsp:forward page=”{relativeURL |<%=expression%>}” />

You can use the following syntax:

Copy codeThe code is as follows:
<jsp:forward page=”{relativeURL |<%=expression%>}”>
{<jsp:param…/>}
</jsp:forward>

The second syntax is used to add additional request parameters when forwarding. The value of the added request parameter can be obtained through the getparameter method of the HttpServletRequest class.

2. Include instruction, which is a dynamic instruction, can be used to import a page. Its import will check the changes of the imported page each time. The following is the format of the include instruction:

Copy codeThe code is as follows:
<jsp:include page=”{relativeURL |<%=expression%>}” flush=”true” />

Or:

Copy codeThe code is as follows:
<jsp:include page=”{relativeURL |<%=expression%>}” flush=”true”>
<jsp:param name=”paramName” value=”paramValue”/>
</jsp:include>

The flush property is used to specify whether the output cache is transferred to the imported file. If specified as drama. If it is specified as false, it will be included in the original file. For older versions below JSP 1.1, it can only be set to false.
< include > and< jsp:include >They are static import and dynamic import. Their difference: static import is to insert the code of the imported page completely, and the two pages generate a whole servlet; while dynamic import uses dynamic import in the servlet to import the page.

3. Usebean, setproperty, and getproperty instructions
These three instructions are related to JavaBean. Usebean is used to initialize a Java instance in JSP page, setproperty is used to modify the properties of JavaBean instance, and getproperty is used to obtain the properties of JavaBean instance.
Syntax format of usebean:

Copy codeThe code is as follows:
<jsp:useBean id=”” class=”” scope=”page | request |session | application” >

ID is the instance name of JavaBean, and the class attribute determines the implementation class of JavaBean. Scope property determines the scope of life [page, request, session, application]
Syntax format of setproperty:

Copy codeThe code is as follows:
<jsp:setProperty name=”” property=”” value=”” />

The name property determines whether the

4. Plugin instruction
The plugin instruction is mainly used to download JavaBeans or applets from the server to the client for execution. Since the program is executed on the client, the client must install the virtual machine.
The syntax format of plugin is as follows:

Copy codeThe code is as follows:
<jsp:plugin type=”bean | applet”
code=”classFileName”
codebase=”classFileDiretoryName”
[name=” instanceName”]
[archive=”URLtoArchive”]
[align= “bottom I top I middle I left I right”]
[heigh=”displayPixels”]
[width=”displayPixels”]
[hspace=”leftRightPixels”]
[vspace=”topBottomPiexels”]
[jreversion=JREVersionNumber|1.2″]
[nspluginurl=”URLToPlugin”]
[iepluginurl=”URLToPlugin”]>
[<jsp:parames>
[jsp:param name=”parameterName” value=”parameterValue” />]
</jsp:params>]
[<jsp:fallback>text message for user that can no see the plugin
</jsp:fallback> ]
</jsp:plugin>

These attributes are described as follows:
Type: Specifies the type of Java program to be executed, a bean or an applet
Code: Specifies the name of the file to be executed. The attribute value must end with the “. Class” extension
Codebase: Specifies the directory of files to be executed.
Name: give the program a name to identify it.
Archive: the path to some classes to be preloaded and used.
Hspace, vSpace: displays the left and right margins.
Jreversion: the JRE version required to run the program correctly.
Nsplugin, ieplugin: Netscape Navigator, Internet Explorer, download the address of JRE needed to run.
< jsp:fallback >Command: when the applet cannot be displayed correctly, it will replace the prompt information displayed.

5. Param instruction
The param instruction is used to set parameter values. This instruction itself cannot be used alone because it is meaningless to use it alone. It can be used in combination with the following instructions
jsp:include
jsp:forward
jsp:plugin