Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the knowledge points of JSP grammar

2025-01-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article is to share with you what are the knowledge points about JSP grammar. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

1.JSP instruction

Use the JSP directive (including) to specify the scripting language used, the interface implemented by Servlet, the class extended by Servlet, and the package imported by Servlet. The general syntax form of the JSP instruction is:. The valid directives are:

(1) language: the scripting language used in the file. At this point, only Java is a valid value and a default value for the Java programming language. This directive acts on the entire file. Only * uses are valid when the instruction is used multiple times. Examples are as follows:.

(2) Method: the name of the method generated by the embedded Java code (scriptlet). The generated code becomes the body of the specified method name. The default method is service. Only * uses are valid when the instruction is used multiple times. Examples are as follows:.

(3) A list of Java language package names or class names imported by Import:Servlet, separated by commas. In the JSP file, you can specify this directive multiple times to import different packages. Examples are as follows:.

(4) content_type: the MIME type of the generated response. The default is text/html. Only * uses are valid when the instruction is used multiple times. This directive can be used to specify the character set in which the page is encoded. Examples are as follows:.

(5) mplements: used to generate a comma-separated list of Java language interfaces for Servlet implementations. You can use this command multiple times in a JSP file to implement different interfaces. Examples are as follows:.

(6) the name of the Java language class extended by extends:Servlet. This class must be valid and cannot be a Servlet class. This directive acts on the entire JSP file. Only * uses are valid when the instruction is used multiple times. Examples are as follows:.

2.class-wide variables and methods

Use the and tags to describe the class-wide variables and class-wide methods of the Servlet class. The general syntax is as follows:

. / / Code for class-wide variables and methods

The attribute runat=server is required to indicate that the tag is used for server-side processing. An example of specifying class-wide variables and methods is as follows:

/ / class-wide variable init I = 0; String foo = "Hello"; / / class-wide method private void foo () {/ / the code of this method}

3. Visit JavaBean

JSP syntax support for JavaBean enables you to reuse components through Web sites. JavaBean can be a class file or serialized Bean, or it can be generated dynamically by Servlet. Use tags to create an instance of Bean so that it can be accessed from anywhere in the JSP file. The syntax of the tag is:

The attributes and their meanings are as follows:

(1) name: the name used to find the Bean within the corresponding scope (specified by the scope attribute). For example, this might be the session (session) key value used to store the Bean. The value is written in different cases.

(2) varname: the name used to reference the Bean anywhere in the JSP file. This property is optional. The default value is the name property value. The value is written in different cases.

(3) the class file name of type:Bean. This name is used to illustrate the Bean instance in code. The default value is of type Object. The value is written in different cases.

(4) Introspect: when this value is true, the JSP processor checks all request properties and invokes the set of set property methods that match the request attribute (which is passed in BeanInfo). The default value for this property is.

(5) the .class file name of BeanName:Bean, the Bean package name, or the file name of the serialized file (.ser file) containing Bean. The name is for the Bean instance program. This property is used only if the Bean is not in the specified range and the creation property is set to Yes. The value is written in different cases. The file path must be specified in the Java classpath of the application server, unless the file is in the applicationserver_root\ servlets directory.

(6) Create: when this value is true, if the processor does not find Bean within the specified range, JSP will create an instance of Bean. The default value is true.

(7) the life cycle of Scope:Bean. This property is optional and its default value is request. Valid values are:

Request-Bean is set to the context of the request by Servlet, and the Servlet uses the API call described in JSP API

Use the JSP file. If the Bean is not part of the request context, the Bean is created and stored in the request context unless the creation property is set to No.

Session-if Bean exists in the current conversation, Bean has been reused. If there is no Bean, and you create a property setting

Yes, it has been created and stored as part of the session.

Userprofile-A user profile file that is retrieved, modeled into a specified type, and introspected by an Servlet request object. (in

In IBM WebShere application server, the default type is com.ibm.servlet.personalization.userprofile.UserProfile).

(8) param: a list of attribute and value pairs. Property is automatically set with introspection in Bean. The property is set only when the Bean is instantiated.

In addition to using properties to set the Bean property, you can use three other methods: *, when you request the URL of a Web page (JSP file) that contains Bean, specify the query parameters. The introspection property must be set to Yes. An example is as follows: http://www.myserver.com/signon.jsp?name=jones&password=d13x, where the Bean property name will be set to jones. The second method is to specify the attribute as a parameter submitted by the HTML tag. The mothod property must be set to post. Set the action property to the URL of the JSP file that invokes Bean. The introspect property must be set to Yes. Examples are as follows:

The third method is to set the Bean property using JSP syntax.

Once the tag is specified, the Bean can be accessed anywhere in the JSP file. There are three ways to access the Bean attribute: using JSP scriptlet, using JSP expressions, and using tags (as described in the HTML template syntax for variable data). See the JSP example (DisplayData.jsp file) for three examples of each of the methods that access the Bean method.

4. Directly inserted Java code (scriptlet)

You can embed any valid Java language code directly between tags in an JSP file. The code embedded in this way is called scriptlet. If no method directive is specified, the generated code becomes the body of the service method. Scriptlet for Servlet can use a predefined set of variables that conforms to the basic Servlet, output, and input classes:

(1) request: Servlet request class defined by javax.servlet.http.HttpServletRequest

(2) responses: Servlet response class defined by javax.servlet.http.HttpServletRequest

(3) out: output transcription program class defined by java.io.PrintWriter

(4) in: input reader class defined by java.io.BufferedReader

Examples are as follows:

5. HTML template syntax for variable data

When the page is returned to the browser, the application server HTML template syntax enables you to place variable fields on the HTML page and allows Servlet and JavaBean to dynamically replace variables with database values. This feature is an IBM extension of JSP that makes it easy to reference variable data. This syntax can only be used in JSP files. The HTML template syntax includes:

Basic HTML template syntax

Replace HTML template syntax

Mark.

These tags are designed to communicate interoperability through the HTML authoring tool. Each tag has a corresponding closing tag. Each tag is case-sensitive, and some of their attributes are case-by-case. IBM WebSphere Studio makes it easier to develop JSP files to contain HTML template syntax.

(1) basic HTML template syntax

Tags are the basic tags used to specify variable fields. The general syntax is:

The attributes and their meanings are as follows:

Requestparm: the parameter to be accessed within the request object. This property is case-sensitive and cannot be used with the Bean and property properties.

Requestattr: the attribute to be accessed within the request object. Property should be set using the setAttribute method. This property is case-sensitive and cannot be used with the Bean and property properties.

Bean: the JavaBean name indicated by the tag in the JSP file. See visit JavaBean for an explanation of the markup. The value of this property is case-sensitive. When the Bean attribute is specified, but the property attribute is not specified, the full Bean is used in the replacement. For example, if Bean is of type String and no attribute is specified, the value of string is overridden.

Property: access the properties of the replaced Bean. The value of the property is case-sensitive and is the independent locale name of the property. This property cannot be used with the requestparm property.

Default: displays an optional string when the value of the Bean property is empty. If a string contains more than one word, the string must be enclosed in a pair of double quotes (for example, "HelpDesk number"). The value of this property is case-sensitive. If no value is specified, an empty string is used when the property value is empty.

Examples of basic JSP syntax are as follows:

In most cases, the value of the property attribute is the property name. However, you can access one of the attributes (child attributes) of an attribute by specifying the full format of the property property. This full format also provides options for you to specify an index of index properties. The optional index can be a constant (for example, 2) or as described in the repeatedly set HTML tag. A full-format example of using attribute tags is as follows:

(2) replace HTML template syntax

The HTML standard does not allow embedding HTML tags in HTML tags. Therefore, it is not possible to embed a tag in another HTML tag. Instead, use HTML templates instead of syntax. To use alternative syntax:

Use and to include the HTML tag in which the alternative content is indicated.

Specify the Bean and property properties:

To specify Bean and attribute properties, use the following format: $(bean=b property=p default=d), where b, p, and d

As a value that describes the basic syntax.

To specify the requestparm property, use the following format: $(requestparm=r default=d), where r and d are used as

A value that describes the basic syntax.

To specify the requestattr property, use the following format: $(requestattr=r default=d), where r and d are the descriptions

The value of the basic JSP syntax.

Examples of alternative HTML template syntax are as follows:

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report