Chapter 1 Basic JSP syntax and chapter 1 jsp syntax
Jsp page element structure
The jsp page consists of commands, comments, static content, expressions, small scripts, and declarations.
Jsp commands
Page command: Usually located at the top of the jsp page. The same page can have multiple page commands.
Include command: embed an external file into the current jsp file and parse the jsp statement on this page
Taglib command: Use the tag library to define a new custom tag and start the custom behavior on the jsp page.
Page instruction syntax
<% @ Page attribute 1 = "attribute value" attribute 2 = "attribute value 1, attribute value 2" attribute n = "attribute value n" %>
Jsp comments
Comment on the jsp page.
HTML comment:
<! -- Html annotation --> // visible to the client
Jsp comments:
<% -- Html comment -- %> // invisible to the client
Jsp script annotation:
// Single line comment
/**/Multi-line comment
Jsp script
Java code executed on the jsp page
Syntax:
<% Java code %>
Jsp Declaration
Define variables or methods on the jsp page
Syntax:
<%! Java code %>
Jsp expressions
Expressions executed on the jsp page
Syntax:
<% = Expression %> // Note: The expression does not end with a semicolon
Lifecycle of a jsp page
The lifecycle of JSP is divided into four major stages, which are very similar to those of Servlet. The following are some key points:
JSP Compilation:
When the browser requests a JSP, the JSP Engine first checks whether it needs to compile the page. If the page has never been compiled, or if the JSP has been modified, it is the last page for compiling the JSP Engine.
The compilation process consists of three steps:
Parse JSP.
Open JSP into servlet.
Compile this servlet.
JSP initialization:
Call the jspInit () method before a container loads any service request of a JSP. If you need to perform JSP-specific Initialization
JSP execution:
This stage of the JSP lifecycle represents all the interactions of the request until the JSP is destroyed.
When the browser requests that a JSP and page have been loaded and initialized, the JSP Engine calls the _ jspService () method in JSP.
JSP cleanup:
The JSP lifecycle destruction stage indicates that the container used when the JSP is deleted.
The jspDestroy () method is equivalent to the destroy method of JSP servlet. Override jspDestroy when you need to perform any cleanup, such as releasing the database connection or closing open files.