簡體   English   中英

當 JSP 完成執行時會發生什么?

[英]What happens when a JSP finishes execution?

當一個 JSP 完成執行時,在 JSP 頁面中聲明的所有變量是否都會被用於垃圾回收? If I declare a number of memory intensive Hashtables in the JSP, and I let the JSP finish execution without setting the variables to null beforehand, will the object stay in memory even after the JSP has finished executing?

(我沒有將它們存儲在持久變量中,例如 session。只是在局部變量中。)

如果變量在請求或頁面 scope 中聲明,是的,它們有資格進行垃圾收集。

Even if you set an object reference to null it is still consuming memory, only the reference count decreases by 1. If the reference count is 0 the garbage collector will free the memory.

Well, the JSP engine removes the JSP page from the memory once the execution has finished (if the scope is not set to session). 然而,為了避免 memory 泄漏,您應該使用 jspDestroy() 方法來釋放 memory。

如果您想確切了解 JSP 翻譯成的 Java 邏輯代碼是什么,您可以使用Jasper生成代碼。 (Different JSP engines will likely generate differing output, but the scope of variables and so on should conform to the spec.) You'll need Tomcat and Ant .

此示例批處理腳本在output目錄中生成用於test.jsp的 Java 代碼:

@ECHO OFF
SETLOCAL EnableDelayedExpansion
SET ANT_HOME=C:\dev\apache-ant-1.7.1
SET TOMCAT_HOME=C:\Program Files\Apache Software Foundation\Tomcat 6.0
SET CLASSPATH="
FOR /r "%TOMCAT_HOME%\bin\" %%a IN (*.jar) DO SET CLASSPATH=!CLASSPATH!;%%a
FOR /r "%TOMCAT_HOME%\lib\" %%a IN (*.jar) DO SET CLASSPATH=!CLASSPATH!;%%a
SET CLASSPATH=!CLASSPATH!;%ANT_HOME%\lib\ant.jar"
MKDIR output
java org.apache.jasper.JspC -d .\output -webapp .\WebContent test.jsp

WebContent是 web 應用程序的根目錄。 生成的代碼是一個 servlet,將遵循規范中定義的 servlet 生命周期。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM