jsp page element composition
The components of the jsp page are: directives, comments, static content, expressions, small scripts, and declarations.
jsp directive
Page directive: Usually located at the top of the jsp page, there can be multiple page directives on the same page.
include directive: embed an external file into the current jsp file, and parse the jsp statements in this page at the same time
taglib directive: Use the tag library to define new custom tags and start custom behavior in jsp page
Page directive syntax
<%@pageAttribute 1="Attribute Value" Attribute 2="Attribute Value 1, Attribute Value 2" Attribute n="Attribute Value n"%>
jsp comment
Comments on the jsp page.
HTML comments:
<!--html comment-->//The client is visible
jsp comments:
<%--html comment--%>//The client is not visible
jsp script comments:
//Single line comment
/**/Multiple line comment
jsp script
Java code executed in jsp page
grammar:
<%java code%>
jsp declaration
Define variables or methods in jsp page
grammar:
<%!java code%>
jsp expression
Expressions executed in jsp page
grammar:
<%=Expression%>//Note: The expression does not end with a semicolon
The life cycle of jsp page
The life cycle of JSP is divided into four main stages, which are very similar to the life cycle of Servlets, and have the following 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 is never compiled, or if the JSP has been modified, because it is the last page to compile the JSP engine compiled.
The compilation process includes three steps:
•Parse JSP.
•Open JSP into servlet.
•Compile this servlet.
JSP initialization:
When a container loads a JSP its service request before calling the jspInit() method. If you need to perform JSP specific initialization
JSP execution:
This stage of the life cycle of JSP represents all the requested interactions until the JSP is destroyed.
When the browser requests a JSP and the page has been loaded and initialized, the JSP engine calls the _jspService() method in the JSP.
JSP Cleanup:
The life cycle destruction phase of JSP represents the container used when the JSP is deleted.
The jspDestroy() method is the destroy method equivalent to the JSP servlet. Overwrite jspDestroy when you need to perform any cleaning, such as freeing the database connection or closing an open file.