I am looking to dynamically enable or disable a button based on the insertion or removal of a USB device. How can I utilize ajax to update the button status whenever a USB is detected?
Using Ajax
function checkUSBStatus(){
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", "CheckUSBStatusServlet", true);
xmlhttp.send();
}
Sending Response to JSP from Servlet: Is there a way to trigger an event listener each time and resend the response back to JSP to execute the ajax function again?
public static boolean usbConnected = 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) {
usbConnected = true;
}
public void removed(Storage storage) {
usbConnected = false;
}
};
BundleContext bc = AppManager.getInstance().getBundleContext();
StorageManager sm = StorageManager.getInstance(KSFUtility.getInstance().getApplicationContext(bc));
sm.addListener(listener);
if (usbConnected == true)
{
out.print("true");
}
else
{
resp.reset();
}
}