gpt4 book ai didi

java - 如何使用jsp返回AJAX响应?

转载 作者:行者123 更新时间:2023-12-01 14:25:07 25 4
gpt4 key购买 nike

我是jsp新手,所以我想知道如何返回AJAX响应。这是使用 dojo 和 AJAX 提交表单的代码:

<script type="text/javascript">
dojo.require("dojo.io.iframe");

dojo.addOnLoad(function() {
upload = function( ) {
dojo.io.iframe.send({
form : "fileUploader",
handleAs : "html", //response type from the server
url : "url to jsp",//just example, not the real url
load : function(response, ioArgs) {
console.log(response, ioArgs);
return response;
},
error : function(response, ioArgs) {
console.log("error");
console.log(response, ioArgs);
return response;
}
});
};
});

这是返回响应的 jsp 代码片段:

r = "<p >You have successfully uploaded your video. </p>";
response.setContentType("text/html");
response.getWriter().write(r);

如果我只使用 System.out.println(r),它会在控制台中打印结果,因此正在提交表单。所以我认为问题在于我返回响应的方式或我使用道场处理它的方式。

提前致谢!

最佳答案

这里是简单的 AJAX 和 JSP(无 jquery),用于单击按钮并在输入框中返回时间。查看“document.myForm.time.value=xmlhttp.responseText; ”行,它在其中获取响应并在 jsp 中设置结果。

<html>
<head>
<title>JSP and Servlet using AJAX</title>
<script type="text/javascript">



function getXMLObject() //XML OBJECT
{
var xmlHttp = false;
try {
xmlHttp = new ActiveXObject("Msxml2.XMLHTTP") // For Old Microsoft Browsers
}
catch (e) {
try {
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP") // For Microsoft IE 6.0+
}
catch (e2) {
xmlHttp = false // No Browser accepts the XMLHTTP Object then false
}
}
if (!xmlHttp && typeof XMLHttpRequest != 'undefined') {
xmlHttp = new XMLHttpRequest(); //For Mozilla, Opera Browsers
}
return xmlHttp; // Mandatory Statement returning the ajax object created
}

var xmlhttp = new getXMLObject(); //xmlhttp holds the ajax object

function ajaxFunction() {
var getdate = new Date(); //Used to prevent caching during ajax call
if(xmlhttp) {

xmlhttp.open("GET","ControlServlet?gettime=gettime" ,true);
xmlhttp.onreadystatechange = handleServerResponse;
xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
xmlhttp.send(null);
}
}

function handleServerResponse() {
if (xmlhttp.readyState == 4) {
if(xmlhttp.status == 200) {
document.myForm.time.value=xmlhttp.responseText; //Update the HTML Form element
}
else {
alert("Error during AJAX call. Please try again");
}
}
}
</script>
</head>
<body>
<form name="myForm">
Server Time:<input type="text" name="time" />
<br />
<input type="button" onClick="ajaxFunction()" value="Click"/>
<br />
</form>
</body>
</head>
</html>

这是它的 servlet:

public void doPost(HttpServletRequest request,HttpServletResponse response) throws IOException, ServletException {
StringBuffer requestURL = request.getRequestURL();
if (request.getQueryString() != null) {
requestURL.append("?").append(request.getQueryString());
}
String completeURL = requestURL.toString();

if(request.getParameter("gettime")!= null && !request.getParameter("gettime").toString().equals("")){
PrintWriter out = response.getWriter();
Date df = new Date();
out.println(df.getTime());
}
}

public void doGet(HttpServletRequest request,HttpServletResponse response) throws IOException, ServletException {
doPost(request,response);
}
}

运行上述代码后您应该看到以下内容: enter image description here

关于java - 如何使用jsp返回AJAX响应?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17216927/

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