loadServlet.java
package servlet;
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 LoadServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
System.out.println("ajaxservlet:doget");
String username = request.getParameter("username");
String gender = request.getParameter("gender");
System.out.println("用户名:" + username);
System.out.println("性别:" + gender);
response.getWriter().write("success");
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
System.out.println("ajaxservlet:doget");
String username = request.getParameter("username");
String gender = request.getParameter("gender");
System.out.println("用户名:" + username);
System.out.println("性别:" + gender);
response.getWriter().write("success");
}
}
loadl.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<script type="text/javascript" src="js/jquery-1.6.js"></script>
</head>
<body>
<input type="button" value="基于ajax的体验" />
<hr />
<span id="spanID">
</span>
<script>
$("input").first().click(function()
{
var url="servlet/LoadServlet?time="+new Date().getTime();
var sendData={"username":'jack',"gender":'male'};
//$("#spanID").load(url);
$("span").first().load(url,sendData,function()
{
alert("success");
});
});
</script>
</body>
</html>
运行: