JSP operating principles and advantages

Source: Internet
Author: User

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:


[Html]
<% @ Page language = "java" import = "java. util. *" pageEncoding = "gb2312" %>
<Html>
<Head>
<Title> simple JSP page example </title>
</Head>
<Body>
This is a simple JSP page example <br>
</Body>
</Html>
<% @ Page language = "java" import = "java. util. *" pageEncoding = "gb2312" %>
<Html>
<Head>
<Title> simple JSP page example </title>
</Head>
<Body>
This is a simple JSP page example <br>
</Body>
</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:

 

[Java]
/*
* Generated by the Jasper component of Apache Tomcat
* Version: Apache Tomcat/7.0.22
* Generated at: 08:40:10 UTC
* Note: The last modified time of this file was set
* 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. util .*;
 
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 (getServletConfig ());
}
 
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 pageContext;
Javax. servlet. http. HttpSession session = null;
Final javax. servlet. ServletContext application;
Final javax. servlet. 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;
Application = pageContext. getServletContext ();
Config = pageContext. getServletConfig ();
Session = pageContext. getSession ();
Out = pageContext. getOut ();
_ Jspx_out = out;
 
Out. write ("\ r \ n ");
Out. write ("Out. write ("Out. write ("<title> simple JSP page example </title> \ r \ n ");
Out. write ("Out. write ("<body> \ r \ n ");
Out. write ("this is a simple JSP page example <br> \ r \ n ");
Out. write ("</body> \ r \ n ");
Out. write ("} Catch (java. lang. Throwable t ){
If (! (T instanceof javax. servlet. jsp. SkipPageException )){
Out = _ jspx_out;
If (out! = Null & out. getBufferSize ()! = 0)
Try {out. clearBuffer ();} catch (java. io. IOException e ){}
If (_ jspx_page_context! = Null) _ jspx_page_context.handlePageException (t );
}
} Finally {
_ JspxFactory. releasePageContext (_ jspx_page_context );
}
}
}
/*
* Generated by the Jasper component of Apache Tomcat
* Version: Apache Tomcat/7.0.22
* Generated at: 08:40:10 UTC
* Note: The last modified time of this file was set
* 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. util .*;

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 (getServletConfig ());
}

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 pageContext;
Javax. servlet. http. HttpSession session = null;
Final javax. servlet. ServletContext application;
Final javax. servlet. 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;
Application = pageContext. getServletContext ();
Config = pageContext. getServletConfig ();
Session = pageContext. getSession ();
Out = pageContext. getOut ();
_ Jspx_out = out;

Out. write ("\ r \ n ");
Out. write ("Out. write ("Out. write ("<title> simple JSP page example </title> \ r \ n ");
Out. write ("Out. write ("<body> \ r \ n ");
Out. write ("this is a simple JSP page example <br> \ r \ n ");
Out. write ("</body> \ r \ n ");
Out. write ("} Catch (java. lang. Throwable t ){
If (! (T instanceof javax. servlet. jsp. SkipPageException )){
Out = _ jspx_out;
If (out! = Null & out. getBufferSize ()! = 0)
Try {out. clearBuffer ();} catch (java. io. IOException e ){}
If (_ jspx_page_context! = Null) _ jspx_page_context.handlePageException (t );
}
} Finally {
_ JspxFactory. releasePageContext (_ jspx_page_context );
}
}
}

 

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.
 

From Yan Xiaoyong ---- advanced information technology class
 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.