Java 中的变量值何时消亡或删除
我以为我已经知道变量何时具有值。 但令我惊讶的是,当我在另一个浏览器中打开该变量时,该变量已经有了值。 我猜想该值仍然驻留在网络服务器中。我在想,当我将它打开到另一个浏览器甚至另一台计算机时,它将在内存中拥有自己的变量。
我在 Servlet 的全局范围内第一次出现时将变量声明为 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中使用了它。但我的问题是使用相同功能的其他浏览器得到相同的结果,我认为它是空的,因为它有不同的会话。
虽然我已经计划解决这个问题,但我只是想听听专家的一些建议。
我的问题是变量什么时候死(当Java垃圾收集器处理它时) 以及声明每个会话唯一的变量的最佳实践是什么。 我希望我的问题说清楚了。我将不胜感激任何帮助。谢谢!
I thought I already know when a variable has a value.
But I was surprised that the variable already have a value when I open it in another browser.
I guess that the value is still reside in the web server. I was thinking that when I open it to another browser even to another computer it will have its own variable located in memory.
I declare the variable set to null on it first occurrence in a global scope in my Servlets.
List<RecordsInfo> recordsInfo = null;
//with getter and setter;
then I have a function like this
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.
I know I can get the value of recordsInfo after I initialized it, so I used it in exportToExcel. But my problem is other browser that use the same function get the same result, which I thought it is null because it have different session.
Although I already have plan to fixed this, I just want to have some advise from the expert.
My question is when do variable die (when Java garbage collector disposed it)
and what is the best practice to declare a variable that is unique for every session.
I hope I made my question clear. I'll appreciate any help. Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Servlet 在第一次被请求时被加载到内存中。
现在,对于每个请求,其服务方法都会被调用。但是您在全局范围内拥有
recordsInfo
,因此它将为所有会话/请求共享,直到 servlet 被类加载器重新加载为止。因此,对于您的场景,您可以将
recordsInfo
设置为会话属性,因为您需要针对不同的会话
使用不同的属性,并为请求
缓存更新您的新问题:
内存将是您机器的内存,特别是运行服务器的 jvm 的堆内存。
是的,如果太大而无法缓存,则不要
Servlet is loaded once into memory when it is requested first..
Now for each request its service method will get called. but you have
recordsInfo
at the global scope so it would be shared for all the session/request until the servlet is reloaded by class loaders.So for your scenario , you can set
recordsInfo
as session attribute as you need it different for differentsession
and to be cached forrequests
Update for your new Q.:
Memory will be your machine's memory, particularly heap memory of your jvm on which server is running.
yes if its too big to cache, don't