jquery Ajax的helloworld例子

例 3.5
馬克- to-win:馬克 java社區(qū):防盜版實(shí)名手機(jī)尾號(hào): 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();
/*
參數(shù):
url (String): 裝入頁面的URL地址。
params (Map): (可選)發(fā)送到服務(wù)端的鍵/值對(duì)參數(shù)。
callback (Function): (可選) 當(dāng)數(shù)據(jù)裝入完成時(shí)執(zhí)行的函數(shù).
*/
        $.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元");
        }
    }
}