jquery Ajax的helloworld例子
例 3.5
马克- to-win:马克 java社区:防盗版实名手机尾号: 73203。
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title></title>
<script src="jquery.js"></script>
<script type="text/javascript">
function check()
{
var txt = $("#item").val();
/*
参数:
url (String): 装入页面的URL地址。
params (Map): (可选)发送到服务端的键/值对参数。
callback (Function): (可选) 当数据装入完成时执行的函数.
*/
$.post("jqueryservlet35",{word:txt},function(data){
$("#info").html(data)
})
}
</script>
</head>
<body>
<input type="text" id="item" value="鱼">
<input type="button" value="test" onclick="check()">
<span id="info"></span>
</body>
</html>
package helloWorld;
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 jqueryservlet35 extends HttpServlet {
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=gbk");
PrintWriter out = response.getWriter();
String item = request.getParameter("word");
System.out.println("item is " + item);
/* the following, we must use UTF-8, instead of GBK */
// item=new String(item.getBytes("iso8859-1"),"UTF-8");
// System.out.println("item is "+item);
if (item.equalsIgnoreCase("鱼")) {
out.print("3元");
} else {
out.print("2元");
}
}
}