378
I. Introduction to JSP
JSP is a dynamic web page technology built on the functions provided by Servlet specifications.
1. servlet and JSP: servlet is a Java code containing HTML, and JSP is an HTML containing Java code.
The servlet executes and interprets browser requests on the server, assumes the intermediate role between the client and the application, and mixes dynamic content into static content to generate HTML.
JSP embeds Java Script code and JSP tag in HTML to separate static (HTML) and dynamic (Java code) content to separate content and representation.
2. servlet deficiency: insufficient handling of representation (Display.
A. It is difficult to compile and maintain HTML in servlet.
B. Standard HTML tools cannot be used.
C. It is difficult for non-Java developers to process HTML.
3. Benefits of JSP: convenience, production efficiency and maintainability
JSP will eventually be converted to servlet. All the tasks that can be executed on the JSP page can be completed using Servlet, but the underlying equality does not mean that the servlet and JSP page are applicable to all situations.
The problem lies not in technology, but in convenience, production efficiency, and maintainability.
A. html in JSP has good maintainability.
B. You can use the website development tool.
C. Easy for team development. Weak Separation and strong separation of static html and dynamic content improve production efficiency.
II. Introduction to JSP Operating Mechanism
1. jsp Lifecycle
Conversion phase
(1) the client sends a request to the JSP page,
(2) The JSP Container (Web Container) checks the syntax of the JSP page and translates the JSP page into a Servlet Source file (Java file ).
This class must implement the javax. servlet. jsp. jsppage interface.
Httjsppage is a jsppage extension interface applied to HTTP requests and responses.
Org. Apache. Jasper. runtime. httpjspbase is the implementation class of Tomcat for the httjsppage interface.
The source file is stored in the corresponding project directory under the/work/Catalina/localhost directory of Tomcat.
Execution phase
(3) servlet container load (compiled into class file) Servlet Source file,
(4) create a servlet instance and load the servlet into the server memory to process the request.
(5) initialize the JSP page and call the _ jspinit method.
(6) The container automatically defines the call _ jspservice method to generate the response content.
(7) destroy the JSP page, _ jspdestroy.
2. The first time the JSP page is loaded and the JSP page is modified
Only when the first time the JSP page is loaded and the JSP page is modified, the "JSP Container converts the JSP page to the Servlet Source File ".
For future requests, the JSP Container will check whether the JSP page is modified. After the JSP page is modified, the JSP Container translates the JSP page to update the servlet source file.
3. The subsequent request will only "automatically call the _ jspservice method by the container to generate the response content ".
4. After the server is restarted, the container "creates a servlet instance and loads the servlet into the server memory to process requests" from the execution stage ".
Iii. jsp syntax
A JSP page consists of elements and template data.
Elements must be processed by the JSP Container, while the template data is not processed by the JSP Container. The elements are directly transmitted to the client without being blocked.
1. Composition of template data:
Static content: generally consists of HTML.
Note:
2. element composition:
Directive: Page, include, and tiglib.
Script: expression, scriptlet, declaration;
Action:
* <JSP: userbean>, <JSP: setproperty>, <JSP: getproperty>, and <JSP: param>
* <JSP: Include> and <JSP: Forward>
* Other actions related to defining JSP documents.
4. jsp implicit objects
1. Input and Output objects: httpservletrequest's request, httpservletresponse's response, and jspwriter's out;
2. Scope communication objects: httpsession session, application of servletcontext, and pagecontext of pagecontext;
3. servlet object: Object page, servletconfig config;
4. Error object: throwable exception;