1. 程式人生 > 其它 >有關Session和servletContext長期佔用伺服器記憶體的解決方案

有關Session和servletContext長期佔用伺服器記憶體的解決方案

有關Session和servletContext長期佔用伺服器記憶體的解決方案

馬克-to-win:有關Session和servletContext長期佔用伺服器記憶體的解決方案就是:對於確定不再需要的資料,一定要及時釋放所佔 資源,比如:context.removeAttribute("mysqlds");否則的話,嚴重的情況可能造成記憶體洩露。(請參見我Java部分第 三章記憶體洩漏部分知識。)

4) pageContext: 儲存的鍵值僅在本個頁面有效。在未來學習Taglib過程當中,將發揮巨大作用。類變數被所有使用者(瀏覽器)只在這一頁時共享(例如例1.1),而pageContext 被某個使用者(瀏覽器)只在這一頁時才有。pageContext範圍比類變數小,和區域性變數是一樣的,但區域性變數可以在非service的方法中用,而 pageContext只能在service方法中用。 見例子2.4
5)區域性變數:轉化成servlet後的某個方法中的區域性變數。
6)類變數:轉化成servlet後的類變數。
例 2.3
<%@ page contentType="text/html; charset=GBK" %>
<html>
<body>
<%
request.setAttribute("rName","rmark-to-win");
application.setAttribute("aName","amark-to-win");
session.setAttribute("sName","smark-to-win");
request.getRequestDispatcher("/Cookie/AddCookie").forward(request,response);
/*如用下面的response,request就取不出來了。 結果就變成如下了 null amark-to-win smark-to-win*/
// response.sendRedirect("http://localhost:8080/ServletHello/Cookie/AddCookie");
%>
</body>
</html>

更多內容請見原文,原文轉載自:

https://blog.csdn.net/qq_44638460/article/details/104233596