JSP introduction JSP is a dynamic technical standard proposed by Sun Microsystems and participated by many companies. In the traditional web page HTML file (*. htm ,*. java program snippets (Scriptlet) and JSP tags are added to html to form JSP web pages. java program snippets can manipulate databases, redirect webpages, and send E-mails, implement the functions required to establish a dynamic website. All program operations are performed on the server, and the results are only obtained after the network is uploaded to the client. This greatly reduces the requirements on the client browser, even if the client browser does not support Java, you can also access the JSP webpage. The full name of JSP is java server page, which is simply a simplified Servlet design. It implements java expansion in Html syntax (in the form of <%, %> ). Step 1 of the execution process: the user sends a request. The server receives the request Step 2. The server searches for the physical address of the server through the logical address in the request and checks whether the corresponding JSP file exists, if yes, perform the next step. If no, a third Error 404 error is returned. When the server finds that the JSP file exists, it will check whether the file is modified or newly created, if so, perform Step 4. If not, perform step 6 and perform step 4 of the java bytecode to obtain the corresponding JSP file, call JSP Parser (a jSp Analysis Program), compile it into step 5 of the Servlet program, call jdk, compile the corresponding Servlet bytecode in step 6, and execute the existing corresponding bytecode. If the bytecode is not loaded, it is first loaded and then executed. If the implementation process of the intermediate layer is removed, JSP is actually a Servle file that is convenient for HTML writing. Its implementation is also dependent on Servlet. Of course, its jsp is converted to Servlet through Server (tomcat) implementation, so we don't have to worry about its implementation method. JSp syntax overview JSP code contains JSP elements and Template date. The former refers to the part directly processed by the JSP engine, which must comply with the JAVA syntax. Otherwise, compilation errors will occur (Because java is executed according to JDK after being converted to Servlet). The latter refers to the content not processed by the JSP Engine (the data will be directly returned to the client) example: JSP syntax annotation 1. Common html annotation code: [html] <! -- Annotation --> This annotation is sent to the client but not directly displayed. It can be viewed in the source code. The java code generated by JSP parse is: [java] out. write ("<! -- Annotation --> "); 2. html comments with expressions [java] <! -- <% = New String ("this is using java code in comments") %> --> such comments compile the java code in the expression. This annotation is sent to the client but not directly displayed. It can be viewed in the source code. The java code generated by jsp parse is: [java] <span style = "font-size: 14px;"> out. write ("<! -- "); Out. print (new java. util. string ("this is using java code in comments"); out. write ("--> "); </span> therefore, no additional points are added later. 3. jsp comments [java] <% -- this is a JSP comment -- %> <% -- new String ("this is the use of java in jsp comments. code "); -- %> Such annotations are not sent to the client compiler in html format. The Compiler instructions include "include instructions ", "Page commands" and "taglib commands" are included in the "<% @ %>" volume. The two main Commands are page and include. The "<% @ page %>" command of the page command acts on the entire JSP page, and also contains static files. However, the "<% @ page %>" command cannot act on dynamic inclusion files. You can use multiple "<% @ page %>" commands on a page, however, the attribute can only be used once, but there are exceptions, that is, the import attribute. Because the import attribute is similar to the import Statement in Java (referring to Java Language, the import Statement introduces classes in Java), this attribute can be used multiple times. No matter where the "<% @ page %>" command is placed in the JSP file, it applies to the entire JSP page. However, for the sake of readability and good programming habits of the JSP program, it is best to put it on the top of the JSP file. [Java] <% @ page import = "java. io. file "%> <% @ page language =" java "import =" java. util. * ", import =" java. io. file "pageEncoding =" UTF-8 "%> in general, we use jsp is the use of java, so language is generally not changed, when we use java package needs to import package, of course, myeclipse will automatically import the package when you use it. The following packages have been imported and do not need to be displayed for import: Custom jsp [javascript] After an exception occurs <% @ page errorPage = "error. jsp "%> expression [java] <% int a = 3; %> <% = a %> script segment [javascript] <%! Int a = 10; %> <% switch (a) {case 10: System. out. print ("code block output succeeded"); break; default: System. out. print ("-----") ;}%> JSP declares [java] <%! Int a = 3; %> <% = a %> although it is the same as the script segment <% int a = 3; %> In the expression, but there is actually a world of difference. Check the following code: [java] <%! Int a = 3; %> <% int B = 3; %> <% = a -- %> <% = B -- %> multiple outputs, you will find that the value of B remains unchanged, but a is decreasing. To understand this, we must look at the generated Servlet. When we refresh the browser, the _ jspService (request, resp) method is called (for tomcat ), a generated by defining int in jsp is a member variable (loaded in memory), while B defined in script language is stored in the _ jspService method. When a browser is refreshed, B will be re-set to int. The Servlet is the final embodiment. For these two options, we generally use the script segment without the declaration. Because our website is visited by many people, if A has accessed, modified, B has accessed, and modified, in this way, the declared variables are uncontrollable (Servlet is single-instance, and only one member variable is available ). The include command when you compile jsp, you will find that some pages have the same content. jsp has prepared the include command for us, which can remove a lot of our code repetition. Code 1 (jsp file called using include): [javascript] <% @ page language = "java" import = "java. util. * "pageEncoding =" UTF-8 "%> <% = new Date (). toString () %> Code 2 (Use include to call the jsp file of code 1) [javascript] <body> root directory file <% @ include file = "task. jsp "%> <br> </body> forward page redirection. When jsp is executed to forward, jsp redirects. The following is a forward code with data transmission. jsp [html] <body> This is my forward page. <br> <jsp: forward page = "forwardTo. jsp "> <span style =" white-space: pre "> </span> <jsp: param value =" Neal "name =" userName "/> </jsp: forward> the subsequent code will not be executed </body> forwardTo, jsp [html] <body> This is my forwardTo page. <br> <% String userName = request. getParameter ("userName"); String outStr = "come in"; outStr + = userName; out. println (o UtStr); %> </body> prompt: The value passing method is the same in include mode, and the forward is generated in tomcat. in java, you will find some code. Because of the return, the embedded jsp object after forwar will not be executed. JSP has the following nine built-in objects, including: request, request object, response, response object, pageContext, page Context object session, session object application, application object out, output object, config, configuration object, page, page Object exception, exception object.