2

I created a small web application using jsp and servlet. My ajax post method call the java class for every three seconds. I want to know for every 3 secs, java class variables isBootRunning,istest1Running,istest1Running is initialized to "null" or not. If it will initialized for every request, how to prevent this initialization.

My 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);
});
}

My 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
            }
        }
    }
}
  • The servlet class is instantiate only once by web container, so you variable are never reinitialized. You can log to verify. – Mr_Thorynque Sep 30 '16 at 09:22

3 Answers3

0

You are facing this issue because every time you make a new ajax request to your servlet the results of previous request is not stored/saved.
This issue can be solved using HttpSession. You have to save and fetch the string objects isBootRunning, istest1Running, istest2Running in the session object as below:

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
        }
    }
}

Here, your String objects are stored in session object. And it is quite safe to use session because session management is done your web container and it never breaches the integrity of a user.
This will prevent the initialization of objects for later requests.

Rohit Gaikwad
  • 2,992
  • 3
  • 12
  • 32
  • IMHO this shouldn't be done in session as session is not shared between clients. – Patryk Rogosch Sep 30 '16 at 09:28
  • @PatrykRogosch, why you should share the session. we never share any session between clients. e.g: you logged-in into your bank accnt, then only one session will be created and maintained for you.(one session per user) – Rohit Gaikwad Sep 30 '16 at 09:33
  • I still claim that he want to have "global" visibility of these variables (at lease what he presented in his code). In that case sessions is not going to help you. – Patryk Rogosch Sep 30 '16 at 10:12
  • I have tried and implemented the code and it works fine, I suggest you to please go through some video tutorials and website like http://www.javatpoint.com/servlet-tutorial to get cleared your concepts related to servlets. – Rohit Gaikwad Sep 30 '16 at 10:17
0

You have to write to get variable:

    String isBootRunning = (String) getServletContext().getAttribute("isBootRunning");

You have to write to set variable:

    getServletContext().setAttribute("isBootRunning", isBootRunning);
  • The problem is with initialization, check the question. every time he makes a new ajax request to your servlet the string objects are set to null. – Rohit Gaikwad Sep 30 '16 at 09:31
  • this can be one of the possible solution, but the same context object will be accessible to every user/clients/username and all the users will have same string object values. So this is not good. – Rohit Gaikwad Sep 30 '16 at 11:23
-1

Another thing is that current design is quite bad (possible race condition). Application/web containers are multithreaded. As you are not using any sychonronization, you may not see the result when request is served by another thread.

  • you need to understand the issue is with maintaining the state of String objects isBootRunning, istest1Running, istest2Running. synchronization will not solve this issue ever! – Rohit Gaikwad Sep 30 '16 at 09:40
  • I can't agree. If you add synchronization all threads will share the same state. – Patryk Rogosch Sep 30 '16 at 10:03
  • synchronization just means that only one thread can access a shared resource at a time. Here there is no concept of synchronization, because the String objects are not shared. For each new request made to a servlet a new thread is created and has a new instance of the objects of java class hence they are not shared. Imagine how complicated it would be as per your suggestion, a developer have to always think of synchronization instead of implementing the business logic. If you allow synchronization then only one thread can access your web application at a time. e.g SingleThreadModel. – Rohit Gaikwad Sep 30 '16 at 10:10
  • deprecated : http://www.javatpoint.com/SingleThreadModel-interface – Rohit Gaikwad Sep 30 '16 at 10:12