发送json数据时的nullpointer

时间:2018-05-29 11:09:45

标签: java mysql json ajax hibernate

我在从前端bootstrap发送json数据到表达servlet时遇到了问题。这是我尝试过的。我使用ajax将数据从我的表单发送到带有json的servlet类。请伸手解决这个问题。我的表格是注册学生:

<html>
<head>
<link rel="stylesheet" type="text/css" href="resources/css/bootstrap.min.css">
<script type="text/javascript" src="resources/js/jquery.min.js"></script>
<script type="text/javascript" src="resources/js/bootstrap.min.js">   </script>
<script type="text/javascript">
    function registerStudent() {
        var firstName = $("#firstName").val();
        var lastname = $("#lastname").val();
        var phone = $("#phone").val();
        var email = $("#email").val();
        var studentCode = $("#studentCode").val();
        $.ajax({
            type : "POST",
            url : "/RegisterStudent",
            data :{data: "firstName=" + firstName + "lastname=" + lastname + " phone=" +  phone + "email=" + email+"studentCode"+studentCode},
            success : function(data) {
                var ht = data.msg;
                $("#resp").html(ht);
            },
            error : function(data) {
                alert("Some error occured.");
            }
        });
    }
</script>
</head>
<body>

<div class="container">

<div class="card " style="width:750px;margin:0px auto">


    <div class="card-header">Student Register</div>

    <div class="card-body">


        <form data-toggle="validator" role="form" method="post" >


            <div class="form-group">

                <label class="control-label" for="firstName">Name</label>

                <input class="form-control" data-error="Please enter name field." id="firstName" placeholder="Name"  type="text" required />

                <div class="help-block with-errors"></div>

            </div>


            <div class="form-group">

                <label for="lastname" class="control-label">lastname</label>

                <input type="text" class="form-control"   id="lastname" placeholder="lastname" required>

                <div class="help-block with-errors"></div>

            </div>


            <div class="form-group">

                <label for="phone" class="control-label">phone</label>

                <div class="form-group">

                    <input type="text" data-minlength="5" class="form-control" id="phone" data-error="fill your phone" placeholder="phone" required>

                    <div class="help-block with-errors"></div>

                </div>

            </div>


            <div class="form-group">

                <label class="control-label" for="email">email</label>

                <textarea class="form-control" data-error="Please enter email field." id="email" placeholder="email" required=""></textarea>

                <div class="help-block with-errors"></div>

            </div>

            <div class="form-group">

                <label for="studentCode" class="control-label">studentCode</label>

                <div class="form-group">

                    <input type="text"  class="form-control" id="studentCode" data-error="fill your studentCode" placeholder="studentCode" required>

                    <div class="help-block with-errors"></div>

                </div>

            </div>


        </form>
    </div>
    <button onclick="registerStudent();" type="button" class="btn btn-success btn-block">Register</button>
    <div class="text-center" id="resp" style="margin-top: 14px;"></div>
</div>
</div> 

这是我的servletclass,用于将数据发送到数据库。

@WebServlet(urlPatterns = "/RegisterStudent")
public class RegisterStudentController extends HttpServlet {
StudentServiceInter service = new StudentServiceImpl();
ObjectMapper mapper = new ObjectMapper();
String json = "";

protected void doGet(HttpServletRequest req, HttpServletResponse resp)    throws ServletException, IOException {
    Student student = new Student();
    req.getSession().setAttribute("student", student);
    resp.sendRedirect("/addStudent.jsp");
}

@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    String jsonData = req.getParameter("data");
    resp.setContentType("application/json");
    PrintWriter out = resp.getWriter();
    out.println(jsonData);
    out.close();
}
}

2 个答案:

答案 0 :(得分:0)

一个问题是,客户端,你没有创建json,而服务器端你试图解析json。 Json格式没有//div[@id='rates_detail_desc']/strong[2] 符号。

有效对象是:

equal (=)

在发送之前,您应该data = { data: { firstName: 'Name', lastName: 'Surname', phone: '123-4456', email: 'a@b.c', studentCode: 12345 } };

答案 1 :(得分:0)

试试这个......

 $.ajax({
                            type: 'GET',
                            url: '<?php echo base_url(); ?>"/RegisterStudent"', // your syntax
                            data: {
                                'firstName ': firstName ,
                                'lastName'  :lastname,
                                'phone': phone,
                                'email' : email,
                                'studentCode' : studentCode,

                            },
                           success : function(data) {
                    var ht = data.msg;
                    $("#resp").html(ht);
                },
                error : function(data) {
                    alert("Some error occured.");
                }
                        });