JSP works and operation processes

Note on JSP container and Javaweb good site : https://blog.csdn.net/yw_1207/article/details/78706701

 

JSP origin

In many dynamic pages, most of the content is fixed, only local content needs to dynamically generate and change. 

If you use the Servlet program to export only local web content dynamically changing, in which all static content also requires the programmer to generate program code in Java, Servlet code for the entire program will be very bloated, write and maintain will be very difficult.  

A large number of graphic design and related HTML static content written statement, the programmer does not have to do the work, programmer this is not necessarily good at it. Web graphic design and production staff do not understand Java programming, it is unable to complete the job. 

To make up for deficiencies Servlet, SUN company on the basis of the Servlet launch JSP (Java Server Pages) technology as a solution. 

JSP is a technology that simplifies the preparation of Servlet, which will mix Java code and HTML statements written in the same file, only the content of the page to be dynamically generated using Java code to write, and static content for the use of fixed written in the usual way static HTML pages.

Establish an intuitive understanding of the JSP 

JSP page is a plain text file and the HTML statements nested within the Java code, the file extension JSP pages must be .jsp.

Java code written in Java code in JSP pages need nested in <% and%>, nested between <% and%> script is called fragment (Scriptlets), no nested in the <% and% content between> is called the JSP template element.

JSP Java code statement to be used out.println other Java code string generated by the output result to the client, you may also be used to print them System.out.println statement command line window. 

JSP file as plain HTML files, they can be placed in the WEB application in addition to the WEB-INF directory and its subdirectories any other, the JSP page and access path access path in the form of ordinary HTML pages are exactly the same .

JSP

JSP stands for Java Server Pages, and it servle technologies, are technologies for developing dynamic web resources defined by SUN.

JSP biggest feature of this technology is that, as written in jsp write html, but it is compared in terms of html, html only to provide users with static content, and technology allows nested Jsp java code in the page to provide users dynamic data.

Jsp Quick Start: Output current time in jsp page.

Whether or JSP Servlet, although dynamic web resources can be used for development. But because the two technical characteristics of each, in the long-term software practice, people gradually as a controller servlet web application components to use, and the JSP technology as a data template to use.

This is because the data of the program to beautify usually after the output:

Let jsp produces both dynamic data with java code, it did lead to beautify the page difficult to maintain.

Let servlet produces both data and nested inside the html code beautification data, the program will also lead to poor readability, it is difficult to maintain.

So the best way is based on the characteristics of these two technologies, each responsible for each of them, servlet only responsible for responding to requests generated data, and the data forwarding technology to bring jsp, jsp to display data to do.

The operating principle of JSP

When the WEB container (Servlet engine) receives .jsp extension to a URL access request, it will request access to the JSP engine to process. The Tomcat JSP Servlet engine is a program that is responsible for the interpretation and implementation of JSP pages.

Each JSP page when first accessed, JSP engine will translate it into a Servlet source, then sent to the source Servlet Servlet class files compiled into a class, and then by the WEB container (Servlet engine) like an ordinary call Servlet same way to load a program to perform this translation and interpretation by the JSP pages into Servlet program. 

Tomcat 5.x source files and the Servlet class class files created for JSP pages placed in the "<TOMCAT_HOME> \ work \ Catalina \ <hostname> \ <application name> \" directory, Tomcat JSP page will be translated into Servlet package called org.apache.jsp. <JSP pages directory name in the WEB application>.

JSP specification does not explicitly require script JSP code must use the Java language, the JSP script code can use other scripting language other than the Java language to write, however, JSP page must eventually be converted into Java Servlet program. 

WEB application can before the official release, which will be pre-compiled into all JSP pages Servlet program.

Analysis of the code generated by the JSP Servlet

JSP pages translated into Servlet inherited org.apache.jasper.runtime.HttpJspBase class, HttpJspBase class is a subclass of HttpServlet, so the JSP page is translated into Servlet HttpServlet class of a grandson. HttpJspBase class implements the interface portion javax.servlet.jsp.HttpJspPage method, therefore, HttpJspBase class is abstract. 

SUN offers a suite of specialized programs used in the development of JSP Java class for JSP developers of WEB container and JSP page developers, this Java class is called JSP API. HttpJspPage interface and the interface belongs JspPage JSP API, in the interface HttpJspPage _jspService only defines a method, it inherits the interface JspPage, JspPage interface defines two methods: jspInit () and jspDestroy ().

HttpJspBase the init method calls jspInit and _jspInit method, destroy method calls inside the jspDestroy and _jspDestroy methods, internal service method calls _jspService method. init, service and destroy methods are implemented in HttpJspBase become final type is declared.  

JSP page is located <%> outside each row and close <%> on both sides of each piece of text is converted into a section of text such as a statement to out.write parameters, JSP script fragment (located <%> section within the java code) in intact java code is moved into a position at the corresponding _jspService method, JSP expression among content (in <% =% and>) is to be converted to wherein variable or expression as out.print statement parameters. 

Execution of JSP

JSP implementation process can be divided into the following main points:

The client makes a request.

Web container translates JSP Servlet source code.

Web container generated source code is compiled.

Web container loads the compiled code and execute it.

The execution result of the response to the client.

Introduction process

Client makes a request, the request for the JSP, web will find the appropriate servlet container processing

The servlet converted into byte code file

Loading the byte code files to the web container

At this time we will create an instance of the web container

Initialized

By accepting service requests

And then automatically generate web container and servlet service last two objects to be destroyed

 

 

JSP execution of:
(1) the client sends Request (request);
(2) JSP Servlet Container translated into a JSP source code;
(. 3) Servlet generated source code of compiled and loaded into memory;
(4) the results of the response (response) to the client.

一般人会认为JSP的执行性能会和Servlet相差很多,其实执行性能上的差别只是在第一次的执行。因为JSP在执行第一次后,会被编译成Servlet文件,即为xxx.class,当在重复调用执行时,就直接执行第一次所产生的Servlet,而不用在重新把JSP编译成Servlet。因此,除了第一次的编译会花较久的时间之外,之后JSP和Servlet的执行速度就几乎相同了。

在执行JSP页面时,通常可分为两个时期:转移时期和请求时期:
 转移时期:JSP页面转译成Servlet类(将JSP页面转移为Servlet源代码(.java))
 请求时期:Servlet类执行后,响应结果至客户端(将Servlet源代码(.java)编译成Servlet类(.class))

当JSP页面在执行时,JSP Container会做检查的工作,若发现JSP页面有更新修改时,JSP Container才会再次编译JSP为Servlet;JSP没有更新时,就直接执行前面产生的Servlet

Guess you like

Origin blog.csdn.net/qq_37807989/article/details/82890201
jsp