post方法调用一个java类初始化每个请求
我使用jsp和servlet创建了一个小型web应用程序。我的ajax post方法每三秒调用一次java类。我想知道每3秒钟,java类变量isBootRunning、istest1Running、istest1Running是否初始化为“null”。 如果它将为每个请求初始化,如何防止此初始化 我的JSP:post方法调用一个java类初始化每个请求,java,ajax,jsp,servlets,Java,Ajax,Jsp,Servlets,我使用jsp和servlet创建了一个小型web应用程序。我的ajax post方法每三秒调用一次java类。我想知道每3秒钟,java类变量isBootRunning、istest1Running、istest1Running是否初始化为“null”。 如果它将为每个请求初始化,如何防止此初始化 我的JSP: setInterval(function(){ TestReport(); }, 3000); function TestReport(){
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
}
}
}
这里,字符串对象存储在会话对象中。使用会话是非常安全的,因为会话管理是在您的web容器中完成的,它不会破坏用户的完整性。这将阻止为以后的请求初始化对象。您面临这个问题,因为每次向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
}
}
}
这里,字符串对象存储在会话对象中。使用会话是非常安全的,因为会话管理是在您的web容器中完成的,它不会破坏用户的完整性。这将阻止为以后的请求初始化对象。必须写入才能获取变量:
String isBootRunning = (String) getServletContext().getAttribute("isBootRunning");
getServletContext().setAttribute("isBootRunning", isBootRunning);
必须写入才能设置变量:
String isBootRunning = (String) getServletContext().getAttribute("isBootRunning");
getServletContext().setAttribute("isBootRunning", isBootRunning);
必须写入才能获取变量:
String isBootRunning = (String) getServletContext().getAttribute("isBootRunning");
getServletContext().setAttribute("isBootRunning", isBootRunning);
必须写入才能设置变量:
String isBootRunning = (String) getServletContext().getAttribute("isBootRunning");
getServletContext().setAttribute("isBootRunning", isBootRunning);
另一件事是,目前的设计是相当糟糕的(可能的比赛条件)。应用程序/web容器是多线程的。由于您没有使用任何同步化,当请求由另一个线程提供时,您可能看不到结果 另一件事是当前的设计相当糟糕(可能是竞争条件)。应用程序/web容器是多线程的。由于您没有使用任何同步化,当请求由另一个线程提供时,您可能看不到结果 servlet类仅由web容器实例化一次,因此永远不会重新初始化变量。您可以登录进行验证。servlet类仅由web容器实例化一次,因此您的变量永远不会重新初始化。您可以登录到verify.IMHO。这不应该在会话中完成,因为会话不是在客户端之间共享的。@PatrykRogosch,为什么要共享会话。我们从不在客户之间共享任何会话。e、 g:您登录到银行账户,那么只会为您创建和维护一个会话(每个用户一个会话)。我仍然声称他希望这些变量具有“全局”可见性(至少是他在代码中显示的)。在这种情况下,sessions对您没有帮助。我已经尝试并实现了代码,效果很好,我建议您浏览一些视频教程和网站,以明确与servlet相关的概念。IMHO这不应该在session中完成,因为session不是在客户端之间共享的。@PatrykRogosch,为什么你应该分享这个课程。我们从不在客户之间共享任何会话。e、 g:您登录到银行账户,那么只会为您创建和维护一个会话(每个用户一个会话)。我仍然声称他希望这些变量具有“全局”可见性(至少是他在代码中显示的)。在这种情况下,sessions对您没有帮助。我已经尝试并实现了代码,效果很好,我建议您阅读一些视频教程和网站,以明确与servlet相关的概念。问题在于初始化,请检查问题。每次他向您的servlet发出新的ajax请求时,字符串对象都被设置为null。这可能是一种可行的解决方案,但每个用户/客户机/用户名都可以访问相同的上下文对象,并且所有用户都具有相同的字符串对象值。所以这不好。问题是初始化,检查问题。每次他向您的servlet发出新的ajax请求时,字符串对象都被设置为null。这可能是一种可行的解决方案,但每个用户/客户机/用户名都可以访问相同的上下文对象,并且所有用户都具有相同的字符串对象值。因此,这并不好。您需要了解问题在于如何维护字符串对象的状态isBootRunning、istest1Running、istest2Running。同步永远解决不了这个问题!我不能同意。如果添加同步,所有线程将共享相同的状态。同步只意味着一次只有一个线程可以访问共享资源。这里没有同步的概念,因为字符串对象不是共享的。对于向servlet发出的每个新请求,都会创建一个新线程,并有一个java类对象的新实例,因此它们不会被共享。想象一下,按照您的建议,这将是多么复杂,开发人员必须始终考虑同步,而不是实现业务逻辑。如果您允许同步,那么一次只能有一个线程访问您的web应用程序。e、 g SingleThreadModel.deprecated:您需要了解问题在于如何维护字符串对象的状态isBootRunning、istest1Running、istest2Running。同步永远解决不了这个问题!我不能同意。如果添加同步,所有线程将共享相同的状态。同步只意味着一次只有一个线程可以访问共享资源。这里没有同步的概念,因为字符串对象不是共享的。对于向servlet发出的每个新请求,都会创建一个新线程,并有一个java类对象的新实例,因此它们不会被共享。想象一下按照你的建议会有多复杂