簡體   English   中英

每當servlet發生變化時,如何執行ajax函數?

[英]How to execute an ajax function everytime there is a change in the servlet?

我想在插入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();
    }

}

您可以設置response.setContentType("application/json"); 並以json字符串形式創建輸出: {"status", "true"}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM