gpt4 book ai didi

java - 每次 servlet 发生变化时如何执行 ajax 函数?

转载 作者:行者123 更新时间:2023-11-28 09:03:28 24 4
gpt4 key购买 nike

我想在插入 USB 时启用该按钮,并在拔出 USB 时禁用该按钮。如何使用ajax在每次插入或拔出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-每次触发事件监听器时如何重新发送响应到 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();
}

}

最佳答案

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

关于java - 每次 servlet 发生变化时如何执行 ajax 函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17396636/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com