2013-07-01 45 views
1

我想在插入USB时启用该按钮,并在USB被移除时将其禁用。每次插入或拔出USB时,如何使用ajax更改按钮?如何在每次servlet发生更改时执行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-我怎样才能重新向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(); 
    } 

} 

回答

0

您可以设置response.setContentType("application/json"); ,然后创建JSON字符串输出:{"status", "true"}

相关问题