2016-09-30 4 views
2

jspとサーブレットを使用して小さなWebアプリケーションを作成しました。私のajaxのpostメソッドは3秒ごとにjavaクラスを呼び出します。私は3秒ごとにJavaクラス変数isBootRunning、istest1Running、istest1Runningが "null"に初期化されているかどうかを知りたい。 リクエストごとに初期化される場合、この初期化を防止する方法。ポストメソッドコール1つのJavaクラスはすべてのリクエストを初期化します

マイ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); 
}); 
} 

マイサーブレット:

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

サーブレットクラスはWebコンテナによって一度だけインスタンス化されるため、変数は再初期化されません。確認のためにログをとることができます。 –

答えて

0

たびに以前の要求の結果が格納されていないサーブレットへの新しいAJAX要求を行うので、あなたがこの問題に直面しています/保存されました。
この問題は、HttpSessionを使用して解決できます。あなたは以下のようにセッションオブジェクトに、istest1Running、istest2Runningを保存してisBootRunning文字列オブジェクトを取得する必要があります。

ここ
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コンテナで行われるため、セッションの使用は非常に安全です。ユーザーの整合性が損なわれることはありません。
これにより、後で要求するためにオブジェクトが初期化されなくなります。

+0

IMHOこれは、セッションがクライアント間で共有されていないため、セッションでは実行しないでください。 –

+0

@PatrykRogosch、なぜあなたはセッションを共有する必要があります。クライアント間でセッションを共有することはありません。例:あなたの銀行にログインしている場合、1つのセッションのみが作成され、あなたのために維持されます(ユーザーあたり1セッション) –

+0

私はこれらの変数の「グローバルな」可視性を望んでいると主張しています彼のコードで提示された)。その場合、セッションはあなたを助けるつもりはありません。 –

0

あなたは、変数を取得するために記述する必要があります:

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

あなたは、変数を設定するために記述する必要があります:

getServletContext().setAttribute("isBootRunning", isBootRunning); 
+0

問題は初期化で問題を確認しています。彼がサーブレットに新しいAjax要求を行うたびに、文字列オブジェクトはnullに設定されます。 –

+0

これは可能な解決策の1つですが、同じコンテキストオブジェクトはすべてのユーザー/クライアント/ユーザー名にアクセスでき、すべてのユーザーは同じ文字列オブジェクト値を持ちます。だからこれは良くありません。 –

-1

もう一つは、現在の設計は、(可能な競合状態)非常に悪いということです。アプリケーション/ Webコンテナはマルチスレッド化されています。シンクロナイズを使用していないので、リクエストが別のスレッドによって提供されたときに結果が表示されないことがあります。

+0

あなたは、StringオブジェクトisBootRunning、istest1Running、istest2Runningの状態を維持することに問題があることを理解する必要があります。同期はこの問題を解決しません! –

+0

私は同意できません。同期を追加すると、すべてのスレッドが同じ状態を共有します。 –

+0

同期は、一度に1つのスレッドだけが共有リソースにアクセスできることを意味します。ここでは、Stringオブジェクトは共有されないため、同期の概念はありません。サーブレットに対する新しい要求ごとに、新しいスレッドが作成され、Javaクラスのオブジェクトの新しいインスタンスが存在するため、共有されません。あなたの提案によればどれほど複雑なのか想像してみてください。開発者はビジネスロジックを実装するのではなく、常に同期を考える必要があります。同期を許可すると、一度に1つのスレッドだけがWebアプリケーションにアクセスできます。 SingleThreadModel。 –

関連する問題