我正在尝试使用带有java servlet的ajax应用程序但是ajax无法从服务器获得任何响应,我在apache tomcat中使用它,任何人都可以告诉如何在xampp或tomcat中使用ajax与java?
我正在使用的代码在servlet.java
中有xmlhttp.open(get,servlet.java,true)
而且根本不起作用,请指导我?
Servlet代码: -
import java.io.*;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import javax.servlet.*;
import javax.servlet.http.*;
public class ValidationServlet extends HttpServlet {
/** Handles the HTTP <code>GET</code> method.
* @param request servlet request
* @param response servlet response
*/
protected void doGet(HttpServletRequest request, HttpServletResponse
response)
throws ServletException, IOException {
PrintWriter out = response.getWriter();
boolean passed = validateDate(request.getParameter("birthDate"));
response.setContentType("text/xml");
response.setHeader("Cache-Control", "no-cache");
String message = "You have entered an insvalid date.";
if (passed) {
message = "You have entered a valid date.";
}
out.println("<response>");
out.println("<passed>" + Boolean.toString(passed) + "</passed>");
out.println("<message>" + message + "</message>");
out.println("</response>");
out.close();
}
boolean validateDate(String date) {
boolean isValid = true;
if(date != null) {
SimpleDateFormat formatter= new SimpleDateFormat("MM/dd/yyyy");
try {
formatter.parse(date);
} catch (ParseException pe) {
System.out.println(pe.toString());
isValid = false;
}
} else {
isValid = false;
}
return isValid;
}
}
这是html Validation.html
: -
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" >
<html>
<head>
<title>Using Ajax for validation</title>
<script type="text/javascript">
var xmlHttp;
function createXMLHttpRequest() {
if (window.ActiveXObject) {
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
else if (window.XMLHttpRequest) {
xmlHttp = new XMLHttpRequest();
}
}
function validate() {
createXMLHttpRequest();
var date = document.getElementById("birthDate");
var url = "ValidationServlet?birthDate=" + escape(date.value);
xmlHttp.open("GET", url, true);
xmlHttp.onreadystatechange = callback;
xmlHttp.send(null);
}
function callback() {
if (xmlHttp.readyState == 4) {
if (xmlHttp.status == 200) {
var mes =
xmlHttp.responseXML
.getElementsByTagName("message")[0].firstChild.data;
var val =
xmlHttp.responseXML.getElementsByTagName("passed")[0].firstChild.data;
setMessage(mes, val);
}
}
}
function setMessage(message, isValid) {
var messageArea = document.getElementById("dateMessage");
var fontColor = "red";
if (isValid == "true") {
fontColor = "green";
}
messageArea.innerHTML = "<font color=" + fontColor + ">" å
+ message + " </font>";
}
</script>
</head>
<body>
<h1>Ajax Validation Example</h1>
<form action="ValidationServlet" method="get">
Birth date: <input type="text" size="10" id="birthDate"
Onchange="validate();"/></form>
<div id="dateMessage"></div>
</body>
</html>
答案 0 :(得分:0)
在你的validate()
中,你有一个拼写错误: -
更改: -
xmlHttp.onreadystatechange = callback;
对此: -
xmlHttp.onreadystatechange = callback();
这似乎是结果没有出现的最直接的问题。可能还有其他几个原因。如果这没有帮助,请检查您的浏览器控制台是否有错误消息。