(071)jquery_异步_ajax

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN">
<html>
  <head>
    <title>ajax_ajax.html</title>
    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="this is my page">
    <meta http-equiv="content-type" content="text/html; charset=UTF-8">
    
    <!--<link rel="stylesheet" type="text/css" href="./styles.css">-->
    <style type="text/css">
        #divtest
        {
            width: 282px;
        }
        #divtest .title
        {
            padding: 8px;
            background-color:Blue;
            color:#fff;
            height: 23px;
            line-height: 23px;
            font-size: 15px;
            font-weight: bold;
        }
        ul
        {
            float: left;
            width: 280px;
            padding: 5px 0px;
            margin: 0px;
            font-size: 14px;
            list-style-type: none;
        }
        ul li
        {
            float: left;
            width: 280px;
            height: 23px;
            line-height: 23px;
            padding: 3px 8px;
        }
        .fl
        {
            float: left;
        }
        .fr
        {
            float: right;
        }
    </style>
    <script type="text/javascript" src="../js/jquery/jquery190.js"></script>
    <script type="text/javascript">
        $(function(){
            $("#btnCheck").bind("click",function(){
                var $this = $(this);
                var mUrl = "http://localhost:9090/JQuery/servlet/AjaxPostServlet?number=" + $("#txtNumber").val();
                $.ajax({
                    url:mUrl,
                    dataType:"text",
                    success:function(data){
                        $this.attr("disabled","true");
                        $("#resultDiv").append(data);
                    }
                });
            });
        });
    </script>
  </head>
  
  <body>
        <div id="divtest">
            <div class="title">
                <span class="fl">检测数字的奇偶性</span> 
                <span class="fr">
                    <input id="btnCheck" type="button" value="检测" />
                </span>
            </div>
            <ul>
               <li>请求输入一个数字 
                   <input id="txtNumber" type="text" size="12" />
               </li>
            </ul>
        </div>
        <div id="resultDiv"></div>
  </body>
</html>

 

package com.jquery.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 AjaxPostServlet extends HttpServlet {

    /**
     * Constructor of the object.
     */
    public AjaxPostServlet() {
        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 equals to get.
     * 
     * @param request the request send by the client to the server
     * @param response the response send by the server to the client
     * @throws ServletException if an error occurred
     * @throws IOException if an error occurred
     */
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
    
        String numStr = request.getParameter("number");
        System.out.println(numStr);
        int num = Integer.parseInt(numStr);
        if(num % 2 == 0) {
            out.println("oushu");
        } 
        else {
            out.println("jishu");
        }
        
        out.flush();
        out.close();
    }

    /**
     * The doPost method of the servlet. <br>
     *
     * This method is called when a form has its tag value method equals to post.
     * 
     * @param request the request send by the client to the server
     * @param response the response send by the server to the client
     * @throws ServletException if an error occurred
     * @throws IOException if an error occurred
     */
    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
    
        String numStr = request.getParameter("number");
        System.out.println(numStr);
        int num = Integer.parseInt(numStr);
        if(num % 2 == 0) {
            out.println("oushu");
        } 
        else {
            out.println("jishu");
        }
        
        out.flush();
        out.close();
    }

    /**
     * Initialization of the servlet. <br>
     *
     * @throws ServletException if an error occurs
     */
    public void init() throws ServletException {
        // Put your code here
    }

}

 

posted @ 2015-01-05 11:00  雪中飞雁  阅读(72)  评论(0)    收藏  举报