【发布时间】:2016-09-30 08:26:27
【问题描述】:
我使用 jsp 和 servlet 创建了一个小型 Web 应用程序。我的 ajax post 方法每三秒调用一次 java 类。我想知道每 3 秒,java 类变量 isBootRunning,istest1Running,istest1Running 是否初始化为“null”。 如果每次请求都会初始化,如何防止这种初始化。
我的 JSP:
setInterval(function(){
TestReport();
}, 3000);
function TestReport(){
var tbname = $("#tbname").attr('class');
var userName = $("#userName").attr('class');
var name = tbname;
var url ="TestReport";
var params = {
tbname: tbname,
userName:userName
};
$.post('TestReport', {
tbname: tbname,
userName:userName,
}, function(responseText) {
alert(responseText);
});
}
我的 Servlet:
public class TestReport extends HttpServlet {
private static final long serialVersionUID = 1L;
String isBootRunning = null;
String istest1Running = null;
String istest2Running = null;
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
File f1 = new File("myfirstpath");//this directory is visible for 10 mins only
File f2 = new File("mythirdpath");//this directory is visible for 10 mins only
File f3 = new File("mythirdpath");//this directory is visible for 10 mins only
if (f1.exists() && f1.isDirectory()) {
isBootRunning = "Running";
istest1Running = "Scheduled";
istest2Running = "Scheduled";
} else if(f2.exists() && f2.isDirectory()){
istest1Running = "Running";
istest2Running = "Scheduled";
if(isBootRunning=="Running"){
//here my logic
}
} else if(f2.exists() && f2.isDirectory()){
istest2Running = "Running";
if(isBootRunning=="Running"){
//here my logic
}
if(istest1Running=="Running"){
//here my logic
}
}
}
}
【问题讨论】:
-
servlet 类只被 web 容器实例化一次,所以你的变量永远不会被重新初始化。您可以登录验证。