JSP Running Mechanism
I. Overview
JSP is a dynamic web page technology based on Servlet specifications. Java code and JSP tags are embedded in web files to generate dynamic content.
This article briefly introduces the JSP Running Mechanism and JSP syntax.
Ii. JSP Operating Mechanism
When a JSP file is first requested, it is compiled into a Servlet and then processed by the Servlet.
JSP Container Management JSP page lifecycle is divided into two phases: the conversion phase and the execution phase. When a customer request to the JSP page arrives, the JSP Container converts the JSP page to the Servlet source file, then, call the javac tool to compile the source file to generate the bytecode file. Then, the Servlet container loads the converted Servlet class and instantiate an object to process client requests, after the request is processed, the response object is received by the JSP Container. The Container sends the HTML-format response information to the client.
Iii. JSP syntax
JSP pages are generally composed of elements and template data. Elements must be processed by the JSP Container. The template data is not processed by the JSP Container.
There are three types of elements: command element, script element, and Action element.
1 command element
It is mainly used to provide information about the entire JSP page for the Servlet phase of conversion, and the command does not generate any output.
1.1 page command: acts on the entire JSP page and defines page-related attributes for communicating with JSP containers.
The page command has 15 attributes:
<% @ Page language = "java" // specifies the script language import = "com. test. person "// specify the class extends =" className "that can be used in the script environment // specify the class from which the Servlet class after JSP page conversion inherits session =" true "// whether the page participates in an HTTP session, if it is true, you can use the implicit session Object buffer = "none" // In the JSP page to specify the buffer size used by the out object autoFlush = "true" // specify when the buffer is slow, whether the cached output should automatically refresh isThreadSafe = "true" // specifies whether the access to the JSP page is thread-safe info = "info_text" // specify the page information errorPage = "error_url "// specifies that when a JSP page exception occurs, which error processing page will be switched to isErrorPage = "false" // specify whether the current page is another page's error processing page contentType = "text/html; charset = ISO-8859-1 "// specify the MIME type and character encoding pageEncoding =" ISO-8859-1 "for the corresponding page to specify the character encoding that the page uses if this property is set, the page uses this attribute to specify the character set, if this parameter is not set, use the character set isErrorPage = "false" specified by the contextType attribute to define whether to execute or ignore the EL expression deferredSyntaxAllowedAsLiteral = "false" // indicates the template on the JSP page. whether the Character Sequence trimDirectiveWhitespaces = "false" is allowed in the text. // how to handle the blank space in the template? %>
Create a Person class in the com. test package. Use import to introduce this class in JSP to use:
<%@ page import="com.test.Person" %>
<% Person p = new Person ("xujian", 25); String name = p. getName (); String age = p. getAge () + ""; String info = name + ":" + age; out. write (info); %>
1.2 include command: Used to statically include a file on a JSP page, which can be a JSP page, HTML page, text file, or a Java code
<% @ Include file = "date. jsp" %> 1.3 taglib command: allows the page to use custom tag 2 script elements
The script element consists of three parts: Declaration, script end, and expression:
2.1 Declaration: Used to declare the variables and methods used in the script language on the JSP page
There are two methods to declare variables in JSP, one is in <%! %>, One is in <%>, they are different: declared in <%! The variables and methods in %> are variables and methods in a class, that is, member variables and member methods. The variable declared in <%> is a method variable, that is, a local variable. In addition, if you add a method () method in <%>, the program reports an error, declaring a method in this way means that the result of declaring a method in the method in a class is definitely incorrect.
2.2 script segment: Java code segment to be executed during request processing
<% Out. write ("hello world! "); %>
When the JSP Container converts the JSP page to a Servlet class, the code segment on the page is converted to the Code in the _ jspService () method in order of appearance.
2.3 expression
The expression script element is a complete expression in Java. These expressions are calculated during request processing. The calculation result is converted to a string and inserted into the output stream.
<%@ page import="java.util.Date" contentType="text/html; charset=UTF-8"%>
Current Time: <% = new Date (). toLocaleString () %>
3 Action Element
Action elements provide information for the request processing phase. They have a start tag containing the element name, which can have attributes, optional content, and end tags matching the start tag. A standard action is a tag embedded in the JSP page. When the page is converted to a Servlet, when the JSP Container encounters this tag, instead of the predefined Java code corresponding to the label.
The JSP Specification defines 20 standard action elements.