Verve Posted February 2, 2008 Share Posted February 2, 2008 Hi, I am new to ajax. I am using a jsp from which a servelt is called which will do validation. I running application from tomcat. But in firebug following error is shown : req.responseXML has no properties var message = req.responseXML.getElementsByTagName("valid")[0].child If in url(see jsp below) i am not giving .java then file not found error is shown. I tried by calling same code from a jsp and gave its path in url and it was working. I have no idea why it is not calling java file. please help !!!!!!!!!!!!! Files are given below, both files are in same folder Validate.jsp <html> <head> <script type="text/javascript"> var req; var target; var isIE; function initRequest(url) { if (window.XMLHttpRequest) { req = new XMLHttpRequest(); } else if (window.ActiveXObject) { isIE = true; req = new ActiveXObject("Microsoft.XMLHTTP"); } } function validateUserId() { if (!target){ target = document.getElementById("userid");} var url = "http://localhost:8080/XXAjax/ValidationServlet.java?id=" + escape(target.value); alert(url); initRequest(url); req.onreadystatechange = processRequest; req.open("GET", url, true); req.send(null); } function processRequest() { if (req.readyState == 4) { if (req.status == 200) { var message = req.responseXML.getElementsByTagName("valid")[0].childNodes[0].nodeValue; setMessageUsingDOM(message); var submitBtn = document.getElementById("submit_btn"); if (message == "false") { submitBtn.disabled = true; } else { submitBtn.disabled = false; } } } } // This function is not used for now. You will use this later. function setMessageUsingInline(message) { mdiv = document.getElementById("userIdMessage"); if (message == "false") { mdiv.innerHTML = "<div style=\"color:red\">Invalid User Id</div>"; } else { mdiv.innerHTML = "<div style=\"color:green\">Valid User Id</div>"; } } // (5) Function in which message indicating the validity of the data gets displayed // through the "userIdMessage" <div> element. function setMessageUsingDOM(message) { var userMessageElement = document.getElementById("userIdMessage"); var messageText; if (message == "false") { userMessageElement.style.color = "red"; messageText = "Invalid User Id"; } else { userMessageElement.style.color = "green"; messageText = "Valid User Id"; } var messageBody = document.createTextNode(messageText); // if the messageBody element has been created simple replace it otherwise // append the new element if (userMessageElement.childNodes[0]) { userMessageElement.replaceChild(messageBody, userMessageElement.childNodes[0]); } else { userMessageElement.appendChild(messageBody); } } function disableSubmitBtn() { var submitBtn = document.getElementById("submit_btn"); submitBtn.disabled = true; } </script> <title>Form Data Validation using AJAX</title> </head> <body onload="disableSubmitBtn()"> <form name="ank" method="GET"> <input type="hidden" name="action" value="create"/> <table border="0" cellpadding="5" cellspacing="0"> <tr> <td><b>User Id:</b></td> <td> <input type="text" size="20" id="userid" name="id" onkeyup="validateUserId()"> </td> <%-- The "userIdMessage" div element specifies the location where input validation message gets displayed. --%> <td> <div id="userIdMessage"></div> </td> </tr> <tr> <td align="right" colspan="2"> <input id="submit_btn" type="Submit" value="Create Account"> </td> <td></td> </tr> </table> </form> </body> </html> ValidationServlet.java import java.io.*; import javax.servlet.*; import javax.servlet.http.*; import java.util.*; public class ValidationServlet extends HttpServlet { private ServletContext context; private HashMap accounts = new HashMap(); // Initialize the "accounts" hashmap. For the sake of this exercise, // two accounts are created with names "greg" and "duke" during // initialization of the Servlet. public void init(ServletConfig config) throws ServletException { this.context = config.getServletContext(); accounts.put("greg","account data"); accounts.put("duke","account data"); } public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { // Extract the data of the input form field whose name is "id" String targetId = request.getParameter("id"); if ((targetId != null) && !accounts.containsKey(targetId.trim())) { response.setContentType("text/xml"); response.setHeader("Cache-Control", "no-cache"); response.getWriter().write("<valid>true</valid>"); } else { response.setContentType("text/xml"); response.setHeader("Cache-Control", "no-cache"); response.getWriter().write("<valid>false</valid>"); } } public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { String targetId = request.getParameter("id"); if ((targetId != null) && !accounts.containsKey(targetId.trim())) { accounts.put(targetId.trim(), "account data"); request.setAttribute("targetId", targetId); context.getRequestDispatcher("/Test.html").forward(request, response); } else { context.getRequestDispatcher("/error.jsp").forward(request, response); } } } Thanks in advance ..... Verve Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.