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

Servlet - 每次触发事件侦听器并再次执行 ajax 函数时,如何重新向 JSP 发送响应?

    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 回答