JSP Series 3: JSP script elements

Source: Internet
Author: User

I. Script element: insert Java code into the servlet corresponding to the JSP page.
 
The form of script element 3:
Expression, scriptlet, declaration.

2. expression: Convert the data or data operation result to a string and insert it to the input stream of the current servlet (_ jspservice method ).

1. jsp expression standard syntax:
<% = Java expression %>

JSP expression XML Syntax:
<JSP: expression> JAVA expression </jsp: expression>

2. the JSP expression is not followed by a plus sign;

3. The operation result is converted to a string and inserted into the input stream of the current servlet.

4. jsp expressions generate servlet print statements.
When HTML is converted to a print statement, text is caused by double quotation marks.
Convert JSP expressions to print statements without double quotation marks.

5. These statements will be placed in the _ jspservice method. The _ jspservice method is called no matter doget or dopost.

3. scriptlet: inserts Java code into the Servlet's _ jspservice method.

1. Standard scriptlet Syntax:
<% Java code %>

The XML syntax format of scriptlet:
<JSP: scriptlet> scriptlet </jsp: scriptlet>

2. The number of points to be added after the scriptlet statement.

3. scriptlet can access all automatically Defined variables that can be accessed by expressions.

4. The scriptlet code is inserted to the _ jspservice method in the order of appearance.

5. All variables declared in scriptlet are translated as local variables in the _ jspservice method. Therefore, variables are thread-safe.

4. Declaration: inserts a method or field definition into the main definition body of the servlet class (except for the _ jspservice method ).

1. Declared Syntax:
<%! Domain or method declaration %>

Declared XML syntax format:
<JSP: Declaration> declaration of fields or methods </jsp: Declaration>

2. The declaration is only valid on the current JSP page.

3. declare that no output is generated.

4. Do not use JSP declarations to overwrite servlet (service method ).

5. You can use JSP declaration to overwrite jspinit or jspdestroy methods to initialize and destroy JSP pages, instead of using init and destroy methods directly.
Note: Here are jspinit and jspdestroy instead of the _ jspinit and _ jspdestroy methods. The JSP Container calls the custom jspinit and jspdestroy methods after calling the _ jspinit and _ jspdestroy methods.
In my opinion, the template method mode is used here. jspinit and jspdestroy are the hook methods of the template method _ jspinit and _ jspdestroy.
Since JSP implementation provides such a mechanism, there are sufficient reasons to implement these methods.

When obtaining initialization parameters, you must configure Servlet and servletmapping on the JSP page in Web. xml.
<Servlet-Name> it is the JSP file name xxx. jsp with a suffix.
<JSP-File> replace <servlet-class> to map JSP files.
<URL-pattern> specifies the full name of the relative path of the JSP file, including the suffix.

The focus is on how to solve the problem of concurrent data access.
The JSP Container has only one servlet instance. Different request threads may cause concurrency problems. Shared by multiple threads, which causes Data Consistency Issues in the Declaration.

6. Use a separate Java class instead of a JSP declaration to define most methods.

7. For concurrent access, the declared domain or variable is not thread-safe unless it is read-only.

8. The declaration is to generate code other than the _ jspservice method. Therefore, in the declaration, the "implicit object" cannot be accessed directly ".
 

5. Comparison of expressions, scriptlet, and declarations
 
Syntax:
Both scriptlet and declaration are Java statements, while JSP expressions are only expressions. A complete statement requires a semicolon to indicate the end, but the expression does not.

Java code Insertion Location:

Expression: In the print method of the output stream of the _ jspservice method;
Scriptlet: the location corresponding to the _ jspservice method body.
Declaration: the Main definition body of the servlet class (except for the _ jspservice method );

Variable access scope:
Expression: You can access the domain of the class in the Serlvet class and the local variables in the _ jspservice method.
Scriptlet: same expression.
Declaration: you cannot access non-declared local variables, such as local variables (implicit objects) in the _ jspservice method ).

Thread security:
Expressions and scriptlet are thread-safe.
The Declaration is thread unsafe.

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.