The JSP process and life cycle

1.JSP processing

Web server is to create Web pages using JSP through the following steps:

 

  1. Client browser sends an HTTP request to the server;
  2. Web server recognizes this as a request for a JSP page, and passes the request to the JSP engine. It is accomplished by using a URL or .jsp file;
  3. JSP JSP engine load files from the disk, and then converting them to servlet. This transformation will simply switch all template text println () statements, and all JSP elements converted into Java code;
  4. JSP engine servlet compiled into an executable class, and the original request is passed to the servlet engine;
  5. A component of the Web server will call the servlet engine, and then load and execute the servlet class. During execution, servlet generates HTML output and to the embedded Web server with the HTTP Response;
  6. Web server in the form of static HTML pages will return the HTTP response to the browser;
  7. Eventually, Web browsers handle HTTP response in dynamically generated HTML pages, as if in the process the same as static pages;

FIG process is as follows:

In general, JSP JSP engine checks to see if the file already exists corresponding servlet and JSP file to check whether the modification date earlier than the servlet. If the modification time is earlier than the JSP file corresponding servlet, the container can then determine the JSP file has not been modified and servlet effective. This makes the whole process and other scripting languages ​​to be efficient and quick compared to some.

3.JSP life cycle

Understand the life cycle of JSP JSP is the key to understanding the underlying functionality.

JSP life cycle includes the following phases:

  • Compile phase: servlet servlet container compiling a source file, generating a servlet classes;
  • Initialization Phase: loading the servlet class corresponding to the JSP, create an instance and call its initialization method;
  • The implementation phase: Call the service method of the JSP servlet instance corresponding;
  • Destruction Stage: The method calls upon a servlet instance and the corresponding JSP, servlet instance and then destroyed;

Life cycle as shown below:

(1) JSP compiler

When a browser initiates a request, JSP engine compiles the JSP file. FIG process is as follows:

There are three steps:

  1. Parsing JSP files;
  2. The JSP file into a servlet;
  3. 编译servlet;

这里,首先判断是不是第一次请求,如果是的话,也就是说JSP还没有被编译过,JSP引擎就把相应的JSP文件编译成servlet,生成字节码文件,并调用jspInit();如果不是第一次请求,说明已经有了字节码文件,那么就开始解析执行,调用jspServive()。

jspService()方法被调用来处理客户端的请求,对于每一个请求,JSP引擎将创建一个新的线程来处理请求。如果有多个客户端同时请求JSP文件,则JSP引擎将会创建多个线程,每个客户端对应一个线程。同时,servlet始终存在内存中,因此相应很快。

(2)JSP初始化

容器载入JSP文件后,它会在为请求提供任何服务前调用jspInit()方法。如果需要执行自定义的JSP初始化任务,复写jspInit()方法就行了,就像下面这样:

 
  1. public void jspInit() {

  2. //init code

  3. }

通常,程序只初始化一次。

(3)JSP执行

这一阶段描述了JSP生命周期中一切与请求相关的交互行为,直到被销毁。

当JSP页面完成初始化后,调用jspService()方法执行。

(4)JSP清理

JSP生命周期的销毁阶段描述了当一个JSP网页从容器中被移除时所发生的一切。
jspDestroy()方法在JSP中等价于servlet中的销毁方法。当您需要执行任何清理工作时复写jspDestroy()方法,比如释放数据库连接或者关闭文件夹等等。

--------------------- 本文来自 Ezioooooo 的CSDN 博客 ,全文地址请点击:https://blog.csdn.net/u012877472/article/details/50637527?utm_source=copy

Guess you like

Origin blog.csdn.net/qq_37807989/article/details/82874634