What is a JSP?
The JSP full name is Java Server Pages, which, like Servle technology, is a technology defined by sun to develop dynamic Web resources. The biggest feature of JSP technology is that writing JSP is like writing HTML, but: it can only provide static data to the user compared to HTML, and JSP technology allows to nest Java code in the page and provide dynamic data to the user. It is difficult for servlets to typeset data compared to Servlets, and JSP can easily typeset data while generating dynamic Data in Java code. JSP QuickStart: Output The current time in a JSP page.
JSP principle
- Tomcat finds the index.jsp under the Myetest project by parsing
- Determine if the Servelet object that corresponds to the index.jsp exists
- If it does not exist, the index.jsp is first translated into a Java file, then the JVM is compiled into the corresponding class bytecode file and the _jspint initialization (3) Related object is called, and then the _jspservice method is called to send the response to the browser
- If present, the response result is sent directly to the browser by calling the _jspservice method (4).
JSP script expression
- JSP script expressions (expression) are used to output program data to the client
- Syntax: <%= variable or expression%>
- Example: Current time: <%= new Java.util.Date ()%>
- When the JSP engine translates the script expression, it turns the program data into a string and then loses the data to the client at the appropriate location with Out.write (...).
- You cannot have a semicolon (;) after a variable or expression in a JSP script expression.
JSP script Fragment
- Note: Only Java code can appear in the JSP script fragment, and no other template elements can appear, and the JSP engine translates the Java code in the JSP script fragment into the servlet's _jspservice method in the translation JSP page.
- The Java code in the JSP script fragment must strictly follow the Java syntax, for example, each execution statement must end with a semicolon (;).
- The code in multiple script fragments can be accessed from one another as if all the code were placed in a pair of <%%>. such as: Out.println (x);
- The Java statements in a single script fragment can be incomplete, but the result of a combination of multiple script fragments must be a complete Java statement, for example:
% for (int i=1; i<5; i++) {%> }
JSP declaration
All code written in the
- SP page is translated by default into the service method of the servlet, and the Java code in the JSP declaration is translated to the outside of the _jspservice method, the instance variable.
- Syntax: <%! Java code%> Therefore, JSP declarations can be used to define static code blocks, member variables, and method
- for a servlet program that is converted to a JSP page. Multiple static code blocks, variables, and functions can be defined in a JSP declaration or individually in multiple JSP declarations.
- JSP implicit objects are scoped only to the _jspservice method of the servlet, so these implicit objects cannot be used in JSP declarations.
<%! Static { System.out.println ("Loading servlet!" ); }privateint globalvar = 0; Public void Jspinit () { System.out.println ("Initializing jsp!") );} %><%! Public void Jspdestroy () { System.out.println ("Destroying jsp!") );} %>
JSP annotations
- Format of JSP annotations: <%--Comment Information--%>
- When the JSP engine translates a JSP page into a servlet program, it ignores the content that is commented on the JSP page.
JSP and JSP parsing principle