[求助]一个很简单的验证问题,就是要报错啊!求救!
<P><FONT color=#f73809>HTML代码是这样的</FONT></P><P><html><BR> <head><BR> <title>Using Ajax for validation</title><BR> <BR> <script type="text/javascript"><BR> var xmlHttp=false;</P>
<P> function createXMLHttpRequest() {<BR> if (window.ActiveXObject) {<BR> xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");<BR> } <BR> else if (window.XMLHttpRequest) {<BR> xmlHttp = new XMLHttpRequest(); <BR> }<BR> if (!xmlHttp) { // 异常,创建对象实例失败<BR> window.alert("不能创建XMLHttpRequest对象实例.");<BR> return false;<BR> } <BR> }<BR> function validate() {<BR> createXMLHttpRequest();<BR> var f = document.form1;<BR> var birthDate = f.birthDate.value;<BR> var url = "servlet/Servlet1?birthDate"+birthDate;<BR> xmlHttp.open("GET", url, true);<BR> xmlHttp.onreadystatechange = callback;<BR> xmlHttp.send(null);<BR> }</P>
<P> function callback() {<BR> if (xmlHttp.readyState == 4) {<BR> if (xmlHttp.status == 200) {<BR> var mes = xmlHttp.responseXML.getElementsByTagName("message")[0].firstChild.data;<BR> var val = xmlHttp.responseXML.getElementsByTagName("passed")[0].firstChild.data;<BR> alert(mes);<BR> alert(val);<BR> <BR> <BR> }<BR> }<BR> }<BR> </P>
<P> </script><BR> </head><BR> <body><BR> <h1>Ajax Validation Example</h1><BR> <form name="form1" action="" method="POST"><BR> Birth date: <input type="text" size="10" id="birthDate" onchange="validate();"/></P>
<P> </form><BR> </body><BR></html></P>
<P><FONT color=#ff3300>servlet代码是这样的:</FONT></P>
<P>package mypackage;<BR>import java.text.ParseException;<BR>import java.text.SimpleDateFormat;<BR>import javax.servlet.*;<BR>import javax.servlet.http.*;<BR>import java.io.PrintWriter;<BR>import java.io.IOException;</P>
<P>public class Servlet1 extends HttpServlet <BR>{<BR> private static final String CONTENT_TYPE = "text/html; charset=GBK";</P>
<P> public void init(ServletConfig config) throws ServletException<BR> {<BR> super.init(config);<BR> }</P>
<P> public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException<BR> {<BR> response.setContentType(CONTENT_TYPE);<BR> PrintWriter out = response.getWriter();<BR> boolean passed = validateDate(request.getParameter("birthDate"));//调用方法检验<BR> <BR> response.setHeader("Cache-Control","no-cache");<BR> request.setCharacterEncoding("UTF-8");<BR> //设置输出的信息的格式及字符集<BR> response.setContentType("text/xml; charset=UTF-8");<BR> String message = "You have entered an invalid date.";<BR> if (passed) {<BR> message = "You have entered a valid date.";<BR> }<BR> out.println("<response>");<BR> out.println("<passed>" + Boolean.toString(passed) + "</passed>");<BR> out.println("<message>" + message + "</message>");<BR> out.println("</response>");<BR> out.close();<BR> }<BR> private boolean validateDate(String date) {<BR> <BR> boolean isValid = true;<BR> if(date != null) {<BR> SimpleDateFormat formatter= new SimpleDateFormat("MM/dd/yyyy");<BR> try {<BR> formatter.parse(date); <BR> } catch (ParseException pe) {<BR> System.out.println(pe.toString());<BR> isValid = false;<BR> }<BR> } else {<BR> isValid = false;<BR> }<BR> return isValid;<BR> }<BR>}<BR><FONT color=#f70909>运行HTML没有问题,但是输入字符后网页左下角就提示出错了 真的不晓得是怎么回事了.谢谢[em01]<BR></FONT></P> <P>过来看看</P>
页:
[1]
