调用servlet并从JavaScript调用Java代码以及参数我有会话键,它是从RESTAPI调用中获得的JavaScript变量。我需要在servlet中调用我的Java代码,并将该键作为参数传递。我可以使用什么JavaScript函数来完成这个任务?
3 回答
慕容森
TA贡献1853条经验 获得超18个赞
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"));}添加回答
举报
0/150
提交
取消
