调用servlet并从JavaScript调用Java代码以及参数

调用servlet并从JavaScript调用Java代码以及参数

我有会话键,它是从RESTAPI调用中获得的JavaScript变量。我需要在servlet中调用我的Java代码,并将该键作为参数传递。我可以使用什么JavaScript函数来完成这个任务?



牛魔王的故事
浏览 593回答 3
3回答

动漫人物

没有JavaScript函数本身,但是浏览器通常*提供一个XMLHttpRequest对象,您可以经历一下.图书馆比如悦和jQuery提供辅助函数以简化其使用。*对于“通常”的值,该值包含几乎所有支持JavaScript的浏览器,并且在Netscape 4死后发布。

拉丁的传说

发送POST时,添加标题xhttp.setRequestHeader(“Contenttype”、“application/x-www-form-urlencode”);代码看起来像客户:    function executeRequest(req) {         var xhttp = new XMLHttpRequest();         xhttp.onreadystatechange = function() {             if (this.readyState == 4 && this.status == 200) {                // Typical action to be performed when the document is ready:                document.getElementById("response").value = xhttp.responseText;             }         };         xhttp.open("POST", "execute/cardbrowser", true);         xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");         xhttp.send("lorem=ipsum&name=binny");     }服务器:protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {     System.out.println(req.getParameter("lorem"));}
打开App,查看更多内容
随时随地看视频慕课网APP