This question already has answers here:
How do servlets work? Instantiation, sessions, shared variables and multithreading
(8个答案)
3年前关闭。
我使用jsp和servlet创建了一个小型Web应用程序。我的ajax post方法每三秒钟调用一次Java类。我想每隔3秒就知道java类变量isBootRunning,istest1Running,istest1Running是否初始化为“ null”。
如果将针对每个请求进行初始化,则如何防止这种初始化。
我的JSP:
我的Servlet:
在这里,您的String对象存储在会话对象中。使用会话非常安全,因为会话管理是在您的Web容器中完成的,并且绝不会破坏用户的完整性。
这将防止为以后的请求初始化对象。
(8个答案)
3年前关闭。
我使用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发出新的Ajax请求时,都不会存储/保存先前请求的结果。
可以使用HttpSession解决此问题。您必须在会话对象中保存并获取字符串对象isBootRunning,istest1Running,istest2Running,如下所示:
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try{
HttpSession session =null;
if(request.getSession().isNew()){
session= request.getSession();//new session
}else{
session= request.getSession(false);//current session
}
if(null != session && null != session.getAttribute("isBootRunning") && null != session.getAttribute("istest1Running") && null != session.getAttribute("istest2Running")){
yourLogic(session);//compute your logic for not null values
}
else{
session.setAttribute("isBootRunning", "");
session.setAttribute("istest1Running", "");
session.setAttribute("istest2Running", "");
yourLogic(session);//compute your logic for null values
}
}catch(Exception e){
e.printStackTrace();
}
}
private void yourLogic(HttpSession session) {
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
String isBootRunning = (String)session.getAttribute("isBootRunning");
String istest1Running = (String)session.getAttribute("istest1Running");;
String istest2Running = (String)session.getAttribute("istest2Running");;
if (f1.exists() && f1.isDirectory()) {
session.setAttribute("isBootRunning", "Running");
session.setAttribute("istest1Running", "Scheduled");
session.setAttribute("istest2Running", "Scheduled");
} else if(f2.exists() && f2.isDirectory()){
session.setAttribute("istest1Running", "Scheduled");
session.setAttribute("istest2Running", "Scheduled");
if(isBootRunning=="Running"){
//here my logic
}
} else if(f2.exists() && f2.isDirectory()){
session.setAttribute("istest2Running", "Scheduled");
istest2Running = "Running";
if(isBootRunning=="Running"){
//here my logic
}
if(istest1Running=="Running"){
//here my logic
}
}
}
在这里,您的String对象存储在会话对象中。使用会话非常安全,因为会话管理是在您的Web容器中完成的,并且绝不会破坏用户的完整性。
这将防止为以后的请求初始化对象。
关于java - post方法调用一个java类初始化每个请求,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/39786941/