I. Brief Introduction to JSP 1. What is JSP? JSP stands for Java Server Pages. Like servle technology, JSP is a technology defined by SUN for developing dynamic web resources. The biggest feature of JSP technology is that writing jsp is like writing html, but compared with html, html can only provide static data for users, jsp technology allows you to nest java code on pages to provide dynamic data. Note: When Using eclipse for development, there are usually no smart prompts for newer versions of eclipse jsp. Here I am using j2ee eclipse of version 4.3, which has the jsp files option, as shown in: many methods on the Internet are outdated. The solution here is: in the Properties-> Java Build Path-> Libraries-> Add Library-> Server Runtime of the project, select the required Library (you need to configure the Server, my tomcat Server. 2. the working principle of JSP is to dynamically compile java programs and use servlets for data processing. for more information, see tomcat source code learning. II. JSP syntax and Common commands 1. the HTML content on the JSP page is called a JSP template element. The JSP template element defines the basic skeleton of a webpage, that is, the structure and appearance of the page. 2. JSP expression JSP script expression (expression) is 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 converts the program data into a string and then uses out. print (...) Data is lost to the client. Variables or expressions in JSP script expressions cannot be followed by semicolons (;). 3. JSP script snippets (scriptlet) are used to write multiple lines of Java code on the JSP page. Syntax: <% multi-line java code %> Note: Only java code can appear in JSP script fragments, and no other template elements can appear. The JSP engine is on the translate JSP page, the Java code in the JSP script segment will be stored in the Servlet's _ jspService method. Java code in JSP script snippets must strictly follow the Java syntax. For example, each execution statement must end with a semicolon. A JSP page can contain multiple script fragments. Text, HTML tags, and other JSP elements can be embedded between two or more script fragments. Example: copy the Code <% int x = 10; out. println (x); %> <p> This is the JSP page text </p> <% int y = 20; out. println (y); %> the code in multiple script fragments of the copied code can access each other, as if all the code is placed in a pair of <%>. For example, out. println (x); the Java statements in a single script fragment can be incomplete. However, the result after the combination of multiple script fragments must be a complete Java statement, for example: copy the Code <% for (int I = 1; I <5; I ++) {%> <H1> hi, amos </H1> <% }%> copy code 4. JSP declarations allow jsp to have its own methods, except for existing objects in jsp. this is rarely used. all code written on the JSP page is translated to the servlet service method by default, and the java code in the Jsp declaration is translated to the _ jspService method. Syntax: <%! Java code %> therefore, JSP declarations can be used to define static code blocks, member variables, and methods of Servlet programs converted from JSP pages. Multiple Static code blocks, variables, and functions can be defined in one JSP declaration, or they can be separately defined in multiple JSP declarations. The scope of JSP implicit objects is limited to Servlet's _ jspService method. Therefore, these implicit objects cannot be used in JSP declarations. Example: copy the Code <%! Static {System. out. println ("loading Servlet! ");} Private int globalVar = 0; public void jspInit () {System. out. println (" initializing jsp! ") ;}%> <%! Public void jspDestroy () {System. out. println ("destroying jsp! ") ;}%> Copy Code 5. JSP comment format: <% -- Comment information -- %> when the JSP Engine translates a JSP page into a Servlet program, it ignores the content annotated on the JSP page. 6. JSP commands (directive) are designed for the JSP Engine. They do not directly generate any visible output, but only tell the engine how to process the rest of the JSP page. In the JSP 2.0 specification, three commands are defined: the page command Include command taglib command 1) the page command is used to define various attributes of the JSP page, no matter where the page command appears on the JSP page, it serves the entire JSP page. To keep the program readable and follow good programming habits, it is best to place the page instruction at the beginning of the entire JSP page. Multiple classes or packages can be introduced in the import attribute of a page command. Each package or class is separated by a comma: <% @ page import = "java. util. date, java. SQL. *, java. io. * "%> the preceding statement can also be rewritten to use the import attribute of multiple page commands to introduce each package or class: <% @ page import =" java. util. date "%> <% @ page import =" java. SQL. * "%> <% @ page import =" java. io. * "%> the JSP Engine automatically imports the following package: java. lang. * javax. servlet. * javax. servlet. jsp. * javax. servlet. http. * Complete syntax of the page command defined in the JSP 2.0 specification: copy the Code <% @ page [language = "java"] with the specified language java [extends = "pack Age. class "] specifies the inherited class [import =" {package. class | package. *},... "] // specify the imported package [session =" true | false "] // The default session value is true, indicating that a session object is built in and can be called directly. If not, you can also use request. getSession () obtain [buffer = "none | 8kb | sizekb"] // The default cache size is 8kb [autoFlush = "true | false"] // The cache is automatically cleared by default. [isThreadSafe = "true | false "] // is it thread-safe, the default value is true, that is, whether multithreading is supported. [info = "text"] // The prompt message [errorPage = "relative_url"] errorPage indicates to set the current page to introduce an error page. That is, if an error occurs on the current page on the floating surface, the page specified by errorPage is displayed. [IsErrorPage = "true | false"] // whether the current page is an error page. The default value is false. [contentType = "mimeType [; charset = characterSet]" | "text/html; charset = ISO-8859-1 "] // set page content encoding [pageEncoding =" characterSet | ISO-8859-1 "] // current page encoding [isELIgnored =" true | false "] // whether EL expressions are supported %> copy code note: 1. the JSP engine generates the corresponding call ServletResponse according to the contentType attribute of the page command. the setContentType method. The contentType attribute of the page command also describes the character encoding of the JSP source file. 2. the setting value of the errorPage attribute must use a relative path. If it starts with "/", it indicates the root directory relative to the current WEB application (note that it is not the site root directory). Otherwise, relative to the current page. You can. use the <error-page> element in the xml file to set the error handling page for the entire WEB application. The <exception-type> sub-element specifies the full qualified name of the exception class, <location> specifies the path of the error handling page starting. If the errorPage attribute of a JSP page is set, the error handling settings in the web. xml file will not work for the page.