1

USBが挿入されたときにボタンを有効にし、USBが取り外されたときにボタンを無効にしたい。USBが挿入または削除されるたびに、ajaxを使用してボタンを変更するにはどうすればよいですか?

アヤックス

function loadXML(){
var xmlhttp;
if (window.XMLHttpRequest)
{
    xmlhttp = new XMLHttpRequest();
}
else
{
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}

xmlhttp.onreadystatechange=function()
{
    if (xmlhttp.readyState==4 && xmlhttp.status==200)
        {
            alert (xmlhttp.responseText);
            if(xmlhttp.responseText == "true") {
                document.getElementById('scan').disabled=false;
            }
            else {
                document.getElementById('scan').disabled=true;
            } 

        }
}
xmlhttp.open("GET", "ScanJobServlet", true);
xmlhttp.send();
}

サーブレット - イベントリスナーがトリガーされるたびに JSP に応答を再送信し、ajax 関数を再度実行するにはどうすればよいですか?

    public static boolean status = false;

protected void doGet(HttpServletRequest req, HttpServletResponse resp)
    throws ServletException, IOException {
    // TODO Auto-generated method stub
    //super.doGet(req, resp);       

    PrintWriter out = resp.getWriter();

    RemovableStorageEventListener listener = new RemovableStorageEventListener() 
    { 
        public void inserted(Storage storage) {
            status = true;
    }
        public void removed(Storage storage) {
            status = false;
        } 
    }; 

    BundleContext bc = AppManager.getInstance().getBundleContext();
    StorageManager sm = StorageManager.getInstance(KSFUtility.getInstance().getApplicationContext(bc));
    sm.addListener(listener);

    if (status==true)
    {
        out.print("true");
    }
    else
    {
        resp.reset();
    }

}
4

1 に答える 1

0

response.setContentType("application/json"); 出力を json 文字列として設定および作成できます。{"status", "true"}

于 2013-07-01T05:43:40.323 に答える