java web笔记:jquery ajax提交form表单到servlet(servlet 接收body体数据)

注意:<input type=”submit”> 改为<input type=”button”>

<html>
  <head>
    <title>$Title$</title>

    <style type="text/css">
      *{
        margin: 0;
        padding: 0;
      }
      .buttons{
        margin-left: 20px;
      }
    </style>

      <script src="javascript/jquery-3.6.0.js"></script>
  </head>
  <body>

  <div style="margin: 80px auto 0 500px">
    <script>
        $(function (){

            $("#submit").click(function (){
                var formJson = getFormJson();
                //alert($("#info table tr:eq(0) td").text()); //correct
                function getFormJson()
                {
                    var arry = $("#form").serializeArray();
                    var formJson = {};
                    for(var i=0; i<arry.length; ++i)
                        if(arry[i].name == "interests")
                        {
                            if( !formJson.hasOwnProperty("interests"))
                                formJson[arry[i].name] = [];
                            formJson[arry[i].name].push(arry[i].value);
                        }
                        else
                            formJson[arry[i].name] = arry[i].value;

                    return JSON.stringify(formJson);
                }

                $.ajax({
                    url:'acceptStudentServlet',
                    type:"post",
                    data:formJson,
                    dataType:'text',
                    success: function (data){
                        var jsonObj = JSON.parse(data);

                        $("#info").css("display", "block");

                        var old = $("#info table tr:eq(0) td").text();
                        $("#info table tr:eq(0) td").text(old + jsonObj.classes);

                        var old = $("#info table tr:eq(1) td").text();
                        $("#info table tr:eq(1) td").text(old + jsonObj.name);

                        var old = $("#info table tr:eq(2) td").text();
                        $("#info table tr:eq(2) td").text(old + jsonObj.password);

                        var old = $("#info table tr:eq(3) td").text();
                        $("#info table tr:eq(3) td").text(old + jsonObj.interests);

                        
                    },
                    error: function (message){
                        alert("fail");
                    }
                })
            })

        })
    </script>
    <form  id="form" method="post" onsubmit="false" action="acceptStudentServlet">
      请选择班级: <select name="classes">
                    <option value="193">193</option>
                    <option value="194">194</option>
                    <option value="195">195</option>
                </select><br>
      用户名: <input name="name" type="text"><br>
      密 码: <input name="password" type="password"><br>

      个人兴趣:<input type="checkbox" name="interests" value="唱歌">唱歌
              <input type="checkbox" name="interests" value="跳舞">跳舞
              <input type="checkbox" name="interests" value="阅读">阅读
              <input type="checkbox" name="interests" value="编程">编程
              <input type="checkbox" name="interests" value="旅游">旅游
      <div class="buttons">
      	<!-- 此处type是button!!!-->
        <input type="button" value="注册" id="submit">
        <input type="reset" value="重置">
      </div>

        <div id="info" style="display: none">
           <table border="1">
               <tr>
                   <td>班级:</td>
               </tr>

               <tr>
                   <td>用户名:</td>
               </tr>

               <tr>
                   <td>密码:</td>
               </tr>

               <tr>
                   <td>兴趣:</td>
               </tr>

           </table>

        </div>
    </form>

  </div>
  </body>
</html>

acceptStudentServlet:

package com.servlet;

import com.Bean.Student;
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.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class acceptStudentServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        ObjectMapper mapper = new ObjectMapper();

        BufferedReader reader = req.getReader();
        String readerStr = "";// 接收用户端传来的JSON字符串(body体里的数据)
        String line;
        while ((line = reader.readLine()) != null){
            readerStr = readerStr.concat(line);
        }

        System.out.println(readerStr);


        Student student = mapper.readValue(readerStr, Student.class);
        String json = mapper.writeValueAsString(student);
        resp.getWriter().print(json);

    }

    protected void jQueryAjax(HttpServlet req, HttpServletResponse resp) throws ServletException, IOException{

    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req,resp);
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值