• 使用jQuery实现ajax请求


    <%--
      Created by IntelliJ IDEA.
      User: Administrator
      Date: 2021/3/13
      Time: 14:54
      To change this template use File | Settings | File Templates.
    --%>
    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <html>
      <head>
        <title>$Title$</title>
        <script type="text/javascript" src="js/jquery-3.4.1.js"></script>
        <script type="text/javascript">
          $(function () {
            $("#pBtn").click(function () {
              var pid = $("#pid").val();
              $.ajax(
                      {
                        url:"search",
                        type:"get",
                        data:{
                          "pid":pid
                        },
                        dataType:"json",
                        success:function (resp) {
                          //resp是json对象
                          $("#pname").val(resp.name);
                          $("#pjiancheng").val(resp.jiancheng);
                          $("#pshenghui").val(resp.shenghui);
                        }
                      }
              )
            });
            })
    
        </script>
      </head>
      <body>
        <table border="1">
          <tr>
            <td>城市编号:</td>
            <td><input type="text" id="pid"> <input type="button" id="pBtn" value="搜索"></td>
          </tr>
          <tr>
            <td>城市名称:</td>
            <td><input type="text" id="pname"></td>
          </tr>
          <tr>
            <td>简称:</td>
            <td><input type="text" id="pjiancheng"></td>
          </tr>
          <tr>
            <td>省会:</td>
            <td><input type="text" id="pshenghui"></td>
          </tr>
        </table>
      </body>
    </html>
    

      

    package com.demo.controller;
    
    import com.demo.dao.ProvinceDao;
    import com.demo.entity.Province;
    import com.fasterxml.jackson.databind.ObjectMapper;
    
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import java.io.IOException;
    import java.io.PrintWriter;
    
    public class SearchServlet extends HttpServlet {
    
        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            String pid = request.getParameter("pid");
            Province p = null;
    
            if (pid != null  && !"".equals(pid)){
                ProvinceDao dao = new ProvinceDao();
                p = dao.getProvinceById(Integer.valueOf(pid));
            }
    
            ObjectMapper om = new ObjectMapper();
            String jsonStr = om.writeValueAsString(p);
    
            response.setContentType("application/json;charset=utf-8");
            PrintWriter out = response.getWriter();
            out.print(jsonStr);
        }
    }
    

      

  • 相关阅读:
    5.1.5 JunkMail Filter
    POJ1067 取石子游戏 跪跪跪,很好的博弈论
    USACO Section 3.2 Magic Squares (msquare)
    5.1.1 A Bug's Life
    USACO Section 3.3 Riding The Fences (fence)
    USACO Section 3.1 Stamps (stamps)
    5.2.7 Entropy
    USACO Section 3.1 AgriNet (agrinet)
    5.1.8 How Many Answers Are Wrong
    4.3.6 N皇后问题
  • 原文地址:https://www.cnblogs.com/ethnic/p/14529078.html
Copyright © 2020-2023  润新知