JSP knowledge point summary (recommended favorites), jsp knowledge point summary favorites
JSP, short for Java Server Page, is a WEB Development Technology Based on Java Servlet and the entire Java system. It is a dynamic WEB Page technical standard.
Operating principle:
The client requests a JSP page from the server. If the JSP page is requested for the first time, the JSP engine of the server first translates the JSP page file into a Java source file (in fact, it converts the JSP file into a Servlet ), then, compile the Java source file, store the generated bytecode file in the localhost \ work \ website directory \ of the server, and finally execute the bytecode file to respond to the customer's request; when the JSP file is requested for the second time, the server will check whether the JSP file has been modified since the last access, if no modification is made, the request is sent to the jspService () method of the servlet in the memory. If the modification is made, the file is re-compiled and replaced by the servlet in the memory, and the jspService () method is executed.
Jsp internal method:
JspInit () {}: this method is called when the jsp Page is initialized, and this method is only executed once during initialization. Therefore, you can perform one-time jobs such as initialization parameter configuration here, created by the author
JspDestroy () {}: this method is called when the jsp Page is disabled for some reason. It is created by the author.
JspService () {}: the jsp Page processing method automatically created by the jsp Container, which is created by the jsp Container and cannot be defined by the author.
When the jsp file is processed for the first time, it is converted into a servlet. The jsp Engine first converts the jsp file into a java source file. If an error occurs during the conversion process, it immediately terminates the file and sends an error message report to the server and client; if the conversion is successful, a class will be generated. Then create a Servlet object. First, execute the jspInit () method to initialize the object. Since the jspInit () method only runs once, therefore, you can perform some necessary operations in this method, such as connecting to the database and initializing some parameters, and then execute the _ jspService () method to process client requests, A thread will be created for each request. If multiple requests need to be processed at the same time, multiple threads will be created. Because the servlet is stored in memory for a long time, the execution speed is fast, however, because initialization requires compilation, the first execution is still slow. If the jsp page is closed or destroyed for some reason, the jspDestroy () method will be executed.
What are the differences between JSP and CGI (Common Gateway Interface?
Both are server-side dynamic programming technologies, but the main difference is that when multiple customers request a JSP page, the JSP Engine Starts a thread for each customer, which is managed by the JSP engine server; the traditional CGI starts a process for each customer. In contrast, threads consume less CPU resources, so the efficiency is high.
Basic Content contained in JSP
1. JSP variables, methods, and expressions <% = %> calculated expressions must have returned results, not ending;
2. annotation elements: <%---%> (not sent to the client). Unlike html annotations, annotations in this way are not exposed to the client at all, however, '//' is still used for comment in <%>.
HTML! Sent to the client and viewed
3. Script element and program segment <%>: write any logic required on the JSP page. This part of code is compiled into the servlet service method. By using the import parameter in the page command, you can call all Java APIs from <%> because any JSP code is actually compiled into a Java Servlet, which is a class, therefore, in JSP, we have the right to use a complete set of Java APIs, so there are almost no limitations.
4. Declaration element <%! %>: Defines global variables or Java methods accessible anywhere on the page, which is equivalent to defining global variables and member methods in the servlet class, declared variables or methods do not generate any output on the JSP page, unless referenced in <%>, they are only used for definition.
5. command element: JSP commands can contain non-Java code modules, such as HTML files. JSP defines three in-page commands for setting JSP parameters or extended code.
1) <% @ page %>: You can set some basic parameters for the webpage, including programming languages, page encoding formats, import classes, and turning to error pages.
2) <% @ include %>: enables a JSP file to contain the content of other files (html, jsp), such as the HTML header and footer stored in a separate file. If the introduced page has declared variables and methods, you can directly use them without having to declare them again. The introduced page will also be part of the compilation cost page, and the declaration can extend the scope, that is to say, it can be extended to any static JSP file, such as: xx. jsp declares some variables and methods and uses the include command to introduce yy. jsp, so yy. jsp can inherit and use these variables and methods. yy is used in jsp. jsp, if yy. jsp is a static file, and the declared variables and methods can be inherited and used. Dynamic files are not supported.
3) <% @ taglib %>: Used to expand the standard JSP tag set. Indicates that the JSP file uses a custom tag, the tag library is referenced, and the tag prefix is specified.
Uri attribute: Tag library descriptor. A uri is collectively referred to as a 'Resource identifier', which indicates available resources on the web. If the tag library file is stored on a remote server, it must be accessed using an absolute path, if the file is downloaded locally, you only need to specify the path of the local directory that marks the library file in the uri.
6. Action element: JSP actions use the tag of xml syntax to control servlet Engine behaviors, such as inserting files, using JavaBean components, and redirecting requests.
1) jsp: include: the difference between this action and the include command is that the file introduced by include will be inserted when the jsp file is converted to servlet, that is, two JSP pages are merged into one JSP, the file is then translated into Servlet, while the file introduced by jsp: include will be inserted when the page is requested, that is, the file is introduced when the container calls the JSP page to implement class processing requests and responds, therefore, when jsp: include is introduced into the page, the actual reference is a Servlet class file generated after the JSP file is converted and compiled. In this way, the referenced JSP file is called only after being executed as a separate file.
2) jsp: useBean
3) jsp: forward
4) jsp: setProperty
5) jsp: getProperty
6) jsp: plugin
Nine built-in JSP objects
1) page: page Object. The PageContext context object is introduced in JSP, through which many attributes of the page can be accessed,
2) You can also obtain these nine built-in objects through the PageContext object, such as getOut () and getRequest.
Indicates the current JSP page, just like this defined by Java.
3) request: This object encapsulates the information submitted by the user, and some methods can be used to obtain the encapsulated information. When you obtain the Chinese characters submitted by the customer, garbled characters may occur, the solution is to encode the retrieved string with a ISO8859-1, store the encoding in a byte array, and then convert the array to a string. Example:
String name = request. getParameter ("username ");
Byte [] by = name. getBytes ("ISO8859-1 ");
Name = new String ();
4) session: it is automatically created when the user accesses the first JSP page to complete session Period Management. It starts when a customer opens a browser and connects to the server, and ends when the customer closes the browser and leaves the server, it is called a session. When a Session object is created, a String-type idnumber will be assigned to the session object, and the idnumber will be sent to the client and saved in the cookie. In this way, the session object will be closed until the client closes the browser, the session Object of the client is canceled on the server.
5) application: This object is generated after the server is started. It is an application-level object, that is, all customers share this object when Browsing each page until the server is closed.
6) out: it is an output stream used to output data to the customer.
7) config: configuration object, indicating the ServletConfig of the JSP
8) response: responds dynamically to customer requests and sends data to the client.
9) exception: You can directly access this object on the page for handling exceptions.
No cookie: A file stored on the user's hard disk by the web server. Each cookie object is stored as a key/value pair; sending a cookie to the client mainly uses the addCookie (Cookie c) Method of the response object; reading the cookie from the client mainly returns an array through the getCookies () method of the request object; you can set the cookie expiration time. It is mainly used to count the number of visits of a user.