Ajax学习笔记(四)
来源:互联网 发布:天刀捏脸数据女 清纯 编辑:程序博客网 时间:2024/06/08 07:12
验证日期合法性的例子:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></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="PostData?birthDate="+escape(date.value)+"&timeStamp="+(new Date()).getTime();
xmlHttp.open("Get",url,true);
xmlHttp.onreadystatechange=handleStateChange;
xmlHttp.send(null);
}
function handleStateChange()
{
if(xmlHttp.readyState==4)
{
if(xmlHttp.status==200)
{
var msg=xmlHttp.responseXML.getElementsByTagName("message")[0].firstChild.nodeValue;
var val=xmlHttp.responseXML.getElementsByTagName("passed")[0].firstChild.nodeValue;
setMessage(msg,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>
<form action="#">
<input type="text" size=10 name="birthDate" /><h4>例:01/02/2003</h4>
<input type="button" value="检测" onclick="javascript:validate();">
<br>
<br>
<div id="dateMessage"></div>
</form>
</body>
</html>
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
import java.io.*;
import java.net.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.text.SimpleDateFormat;
public class PostData extends HttpServlet
{
protected void doGet(HttpServletRequest req,HttpServletResponse res)throws ServletException,IOException
{
doPost(req,res);
}
protected void doPost(HttpServletRequest req,HttpServletResponse res)throws ServletException,IOException
{
req.setCharacterEncoding("UTF-8");
String passed=req.getParameter("birthDate");
res.setContentType("text/xml");
res.setHeader("Cache-Control","no-cache");
String message="非法的日期!";
boolean isValid=true;
if(passed!=null)
{
SimpleDateFormat sdf=new SimpleDateFormat("MM/dd/yyyy");
try
{
sdf.parse(passed);
}
catch(Exception e)
{
isValid=false;
}
}
else
{
isValid=false;
}
if(isValid)
{
message="日期合法!";
}
res.setCharacterEncoding("UTF-8");
res.getWriter().print("<response><passed>"+Boolean.toString(isValid)+"</passed><message>"+message+"</message></response>");
}
}
- Ajax学习笔记(四)
- AJAX IN ACTION 学习笔记(四)
- ajax 学习笔记之四 数据分页
- jQuery 学习笔记四 Ajax提交数据
- jQuery学习笔记四(jQuery - AJAX)
- ajax,json学习笔记(四)跨域
- java web 学习笔记四 使用ajax响应异步请求
- AJAX学习笔记(四)——HTPP请求原理
- AJAX学习笔记(四)_ onreadystatechange 事件
- js学习四-ajax
- Ajax学习 四
- AJAX 学习笔记[四] AJAX 对服务器返回的XML 的处理
- (学习Asp.net Ajax笔记四)Ajax Control Toolkit的AutoComplete功能
- ajax自学笔记(四)
- ajax学习笔记---什么是Ajax
- [ajax 学习笔记] ajax初试
- Ajax学习笔记-Ajax数据格式
- AJAX学习笔记--慕课网Ajax
- 8月份的最后一天了。..
- 打扫打扫,重整旗鼓
- DataGrid实现增删(带提示)改和分页
- 网络命令全解(二)
- 输入法问题
- Ajax学习笔记(四)
- 经典对联
- 脑疲劳仅靠睡觉不解乏
- Ajax学习笔记(五)
- Ajax学习笔记(六)
- WinCE LCD Rotation
- 纵向查询显示小示例
- 工作日总结
- 38400¥的价值(6)