jQuery 调用 Ajax 实现查询的局部更新 (jsp + servlet + jQuery + Ajax)

前端展示界面如下图所示,源代码见 ajaxDemo.jsp。

ajaxDemo.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>


    
    jQuery 调用 Ajax 实现查询的局部更新



查询信息:
 

servlet 源文件,TestAjaxServlet.java

package com.syrdbt.system.servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/TestAjaxServlet")
public class TestAjaxServlet extends HttpServlet {
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        request.setCharacterEncoding("utf-8");
        String name = request.getParameter("info");

        System.out.println("开始处理信息 " + name);

        // 判断信息,将结果返回到前端
        if(name.equals("abc")) {
            response.getWriter().print("success");
        } else {
            response.getWriter().print("failed");
        }
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doGet(request, response);
    }

}

测试结果截图:

你可能感兴趣的:(#,----jQuery,#,----Ajax)