In essence, JSP nesting Java code into HTML, and then compiled and executed by JSP containers (tomcat, resin, WebLogic, etc, then, the corresponding HTML code is generated based on the running results of the dynamic code, which can be normally displayed in the browser of the client.
Operating principle
If the JSP page is requested to run for the first time, the server's JSP compiler will generate the Java code corresponding to the JSP page and compile it into a class file. When the server receives a request for this JSP page again, it will determine whether the JSP page has been modified. If it has been modified, it will generate a new Java code and recompile it, in addition, the garbage collection method on the server will delete useless class files. If it has not been modified, the server will directly call the previously compiled class files.
For example, the Code is as follows:
<% @ Page Language = "Java" Import = "Java. util. * "pageencoding =" gb2312 "%> <HTML>
When the preceding JSP page is requested, the JSP compiler in the web server generates the corresponding Java file. The Java code generated by the JSP program on the server is as follows:
/** Generated by the Jasper component of Apache Tomcat * version: Apache Tomcat/7.0.22 * generated at: 2012-03-22 08:40:10 UTC * Note: the last modified time of this file was set to * The Last Modified Time of the source file after * generation to assist with modification tracking. */package Org. apache. JSP; import javax. servlet. *; import javax. servlet. HTTP. *; import javax. servlet. JSP. *; import Java. UT Il. *; public final class simple_jsp extends Org. apache. jasper. runtime. httpjspbase implements Org. apache. jasper. runtime. jspsourcedependent {Private Static final javax. servlet. JSP. jspfactory _ jspxfactory = javax. servlet. JSP. jspfactory. getdefafactory Factory (); Private Static Java. util. map <Java. lang. string, Java. lang. long> _ jspx_dependants; private javax. el. expressionfactory _ el_expressionfactory; private Org. apache. tomcat. instancemanager _ jsp_instancemanager; Public Java. util. map <Java. lang. string, Java. lang. long> getdependants () {return _ jspx_dependants;} public void _ jspinit () {_ el_expressionfactory = _ jspxfactory. getjspapplicationcontext (getservletconfig (). getservletcontext ()). getexpressionfactory (); _ jsp_instancemanager = org. apache. jasper. runtime. instancemanagerfactory. getinstancemanager (get Servletconfig ();} public void _ jspdestroy () {} public void _ jspservice (final javax. servlet. HTTP. httpservletrequest request, final javax. servlet. HTTP. httpservletresponse response) throws Java. io. ioexception, javax. servlet. servletexception {final javax. servlet. JSP. pagecontext; javax. servlet. HTTP. httpsession session = NULL; Final javax. servlet. servletcontext application; Final javax. s Ervlet. servletconfig config; javax. servlet. JSP. jspwriter out = NULL; Final Java. lang. object page = This; javax. servlet. JSP. jspwriter _ jspx_out = NULL; javax. servlet. JSP. pagecontext _ jspx_page_context = NULL; try {response. setcontenttype ("text/html; charset = gb2312"); pagecontext = _ jspxfactory. getpagecontext (this, request, response, null, true, 8192, true); _ jspx_page_context = pagecontext; applica Tion = pagecontext. getservletcontext (); Config = pagecontext. getservletconfig (); Session = pagecontext. getsession (); out = pagecontext. getout (); _ jspx_out = out; out. write ("\ r \ n"); out. write ("<HTML> \ r \ n"); out. write ("
The above code is the Java code corresponding to the JSP. If the server you use is tomcat, the Java code is located in Tomcat (my version is 7.0) work \ Catalina \ localhost \ your project name \ org \ apache \ JSP under the directory. There should be two corresponding files in this directory. One is a class file and the other is a Java file.
The above process is essentially a servelet, which prints out all the page display content, including every HTML Tag. Therefore, JSP pages are essentially an embodiment of servelet, in JSP programs, the shadows of servelet are inseparable. You do not need to go into the specific syntax of this Code. These tasks are completed by the JSP compiler on the server. This process is automatically completed without manual intervention.
It should be noted that only the requested page can generate the corresponding Java file. The page without a request will generate a Java file at the first request. After the JSP page is modified, if you request this page again, the corresponding Java file will be regenerated.
Advantages of JSP
JSP is embedded with Java code in HTML, so in essence, JSP programs are Java programs, and JSP programs inherit all the advantages of Java. JSP programs have strict Java syntax and rich Java class library support.
JSP pages are compiled into the corresponding servlet by the JSP compiler on the server, so it has the advantages of Java cross-platform, all JSP programs, you can easily migrate data to other operating system platforms without any changes, which is unimaginable in other dynamic scripts.
In JSP, You Can Use Javabean for logical encapsulation to reuse the logic function code, which greatly improves the reusability of the system and the development efficiency of the program.
JSP programs are easy to use. If you have basic knowledge about html and Java, learning JSP programs is not difficult.
Many open-source Java tools can be used in JSP, Which is unmatched by other dynamic web pages.
Thanks to the above advantages, JSP has become one of the most popular languages for developers in many dynamic languages.