JSP page Element composition
JSP page components are: Directives, comments, static content, expressions, small script, declaration.
JSP directives
Page instruction: typically at the top of a JSP page, the same page can have multiple page directives
Include directive: Embed an external file into the current JSP file and parse the JSP statement in the page
TAGLIB directive: Use tag Library to define new custom label, start custom behavior in JSP page
Page instruction Syntax
<% @page Property 1 = "Property Value" Property 2 = "Property value 1, property value 2" Property n= "Property value N"%>
JSP comments
Comments on the JSP page.
Comments for HTML:
<!--HTML annotation-->//client visible
Comments for JSP:
<%--html Note--%>//Client not visible
JSP script annotations:
Single-line Comment
/**/Multiple Line comments
JSP scripts
Java code executed in a JSP page
Grammar:
<%java Code%>
JSP declaration
Define a variable or method in a JSP page
Grammar:
<%!java Code%>
JSP expression
Expressions executed in a JSP page
Grammar:
<%= expression%>//Note: expression does not end with a semicolon
Life cycle of JSP pages
The lifecycle of the JSP is divided into four major phases that are very similar to the servlet lifecycle and have the following points:
JSP Compilation:
When the browser requests a jsp,jsp engine first check if it needs to compile the page. If the page has never been compiled, or if the JSP has been modified because it is the last page compiled by 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 a JSP for any of its service requests. If you need to perform JSP-specific initialization
JSP Execution:
This phase of the JSP's lifecycle represents all of the requested interactions until the JSP is corrupted.
When the browser requests a JSP and the page has been loaded and initialized, the JSP engine invokes the _jspservice () method in the JSP.
JSP cleanup:
The lifecycle destruction phase of the JSP represents the container used when the JSP is deleted.
The
Jspdestroy () method is the Destroy method for the equivalent JSP servlet. Overwrite Jspdestroy when you need to perform any cleanup, such as releasing the database connection or closing the open file.