日期:2014-05-17 浏览次数:21127 次
<%@ page language="java" import="java.util.*" pageEncoding="GBK"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'index_1.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<script type="text/javascript">
//定义变量xmlHttp
var xmlHttp;
//定义函数,用来为xmlHttp赋值
function createXMLHttpRequest(){
if(window.ActiveXObject){
xmlHttp= new ActiveXObject("Microsoft.XMLHTTP");
}else if(window.XMLHttpRequest){
xmlHttp=new XMLHttpRequest();
}
}
//定义函数,向服务器发送信息
function sendMessage(){
createXMLHttpRequest();
//处理缓存问题
xmlHttp.open("GET","http://localhost:80/ajax_0/servlet/Servlet_1?time="+new Date().getDay(),true);
xmlHttp.send(null);
}
</script>
<body>
<b>利用JavaScript的事件处理机制,通过XMLHttpRequest对象执行服务器端的程序</b><br><br><br>
1、onMouseOver事件
<a href="#" onMouseOver="sendMessage()">哈哈</a>
<br><br><br>
2、onClick事件
<input type="button" value="按钮" onClick="sendMessage()" />
<br><br><br>
3、onBlur事件
<input type="text" name="user" onBlur="sendMessage()"/>
<br><br><br>
4、onChange事件
<input type="text" name="user" value="tianyitime" onChange="sendMessage()"/>
<br><br><br>
<a href="index.jsp">返回</a>
</body>
</html>
package com.tianyitime.ajax; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class Servlet_1 extends HttpServlet { /** * Constructor of the object. */ public Servlet_1() { super(); } /** * Destruction of the servlet. <br> */ public void destroy() { super.destroy(); // Just puts "destroy" string in log // Put your code here } /** * The doGet method of the servlet. <br> * * This method is called when a form has its tag value method