我想我已经知道变量何时有值。 但我很惊讶,当我在另一个浏览器中打开它时,该变量已经有了一个值。 我想这个值仍然驻留在Web服务器上。我一直在想,当我将它打开到另一台浏览器时,甚至到另一台计算机时,它将在内存中拥有自己的变量。什么时候变量值在Java中死亡或删除
我在我的Servlets中的全局范围内声明变量设置为null。
List<RecordsInfo> recordsInfo = null;
//with getter and setter;
然后我有这样
function exportToExcel(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
String filePath = "";
try {
if(recordsInfo != null){
/*I need to check first if recordsInfo has already value before creating an excel file.
This is to make sure that the records viewed are the same and will not make another query.*/
filePath = excelExporter.generateExcel(recordsInfo); //do something...
request.setAttribute("filePath ",filePath);
getServletContext().getRequestDispatcher("/docs/download.jsp").forward(request, response);
} else {
//so something....
}
} catch (Exception e)
e.printStackTrace();
}
}
download.jsp功能(JSTL)
Click <a href="${filePath}">here</a> to download.
我知道我能得到recordsInfo的价值后,我初始化它,所以我在exportToExcel中使用它。但我的问题是其他浏览器使用相同的功能得到相同的结果,我认为它是空的,因为它有不同的会话。
虽然我已经有计划解决这个问题,但我只想给专家一些建议。
我的问题是什么时候变量die(当Java垃圾收集器处理它时) 以及声明每个会话都是唯一变量的最佳做法是什么? 我希望我明确提出了我的问题。我会感谢任何帮助。谢谢!
相关:http://stackoverflow.com/questions/3106452/java-servlet-instantiation-and-session-variables/3106909#3106909 – BalusC 2011-03-11 11:11:27