------------------web/jslib/verify.js
function verify(){
// ,
// javascript alert ,
// alert(" ");
// 1
// document.getElementById("userName"); dom
//Jquery , # id , css23
//Jquery jquery , jquery
// $ jQuery.api jQuery(expression,[context]) $("");
//# ID
var jqueryObj = $("#userName");
//
// .val() jQuery.api val()
//
//html <input type="text" value="some text"/>
//jQuery $("input").val(); some text
var userName =jqueryObj.val();
// alert(userName);
// 2 servlet
// jquery XMLHTTPrequest
// $.get() jQuery.api Ajax Ajax jQuery.get(url,[data],[callback],[type])
//
$.get('AJAXServer?name='+userName,null,callback);
}
function callback(data){
// alert(" ");
// 3
alert(data);
// 4
// id
//$("div").html(val); jQury.api html(val)
$("#result").html(data);
}
------------------web/web-inf
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">
<servlet>
<servlet-name>AJAXServer</servlet-name>
<servlet-class>AJAXServer</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>AJAXServer</servlet-name>
<url-pattern>/AJAXServer</url-pattern>
</servlet-mapping>
</web-app>
------------------ajax.html
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title></title>
<script type="text/javascript" src="jslib/verify.js"></script>
<script type="text/javascript" src="jslib/jquery.js"></script>
</head>
<body>
AjaxServer <br>
<!-- Ajax form -->
<!-- Ajax name-->
username:<input type="text" id="userName"/>
<input type="button" value=" " onclick="verify()"/>
<!-- div , -->
<!-- id dom , -->
<div id="result"></div>
<!--
<div>123</div> <div>456</div>
<span>123</span> <span>456</span>
div span div span -->
</body>
</html>
------------------src/AJAXServer
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
/**
* Created by IntelliJ IDEA.
* User: Administrator
* Date: 2011-1-24
* Time: 14:16:34
* To change this template use File | Settings | File Templates.
*/
public class AJAXServer extends HttpServlet{
protected void doPost(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws ServletException, IOException {
doGet(httpServletRequest, httpServletResponse);
}
protected void doGet(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws ServletException, IOException {
try {
httpServletResponse.setContentType("text/html;charset=GB2312");
PrintWriter out = httpServletResponse.getWriter();
//1.
String old = httpServletRequest.getParameter("name");
//2.
if (old == null || old.length() == 0) {
out.println(" ");
} else {
//3.
String name = old;
if (name.equals("zyl")) {
//4.
out.println(" [" + name + "] , ");
} else {
out.println(" [" + name + "] , ");
}
}
out.println(" ");
} catch (Exception e) {
e.printStackTrace();
}
}
}
------------------web/jslib/jQuery.js