以json类型从java表单提交数据

gcmastyq  于 2021-06-23  发布在  Mysql
关注(0)|答案(1)|浏览(308)

我正在尝试以json类型提交表单数据。这里有一堆代码,不起作用,我也不知道下一步该怎么办,请握着我的手,指引我:。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。

<%@include file="header.jsp" %>
<script src="resources/js/jquery.serializejson.js">
</script><script src="resources/js/jquery-1.10.2.min.js"></script>
<script>
$(document).ready(function() {
    $("#frmRegStudent").on('submit', function() {
        $.ajax({
            dataType: "json",
            data: $("#myformid").serialize(),
            success: function() {
               "/registerSuccessfully.jsp";
            }
        });
        return false; // don't reload the page
    });
});
  </script>
  <script type="text/javascript">

function isNumber(evt) {
    evt = (evt) ? evt : window.event;
    var charCode = (evt.which) ? evt.which : evt.keyCode;
    if ((charCode > 31 && charCode < 48) || charCode > 57) {
        return false;
    }
    return true;
}
</script>
<div class="container">
<div class="page-header">
    <h1>Register Student</h1>
    <p class="lead">Please fill in your information below</p>
</div>
<form id="frmRegStudent" name="frmRegStudent"action="/student  /studentRegister" method="post" data-toggle="validator" role="form">
    <div class="form-group has-feedback">
        <label for="studentCode">studentCode</label>
        <input type="text" onkeypress="return isNumber(event)" id="studentCode" name="studentCode"
               class="form-control" data-error="please fill studentCode" required/>
        <span class="glyphicon form-control-feedback" aria-hidden="true"></span>
        <span class="help-block with-errors"></span>
    </div>

    <div class="form-group has-feedback">
        <label for="firstname">Name</label>
        <input type="text" id="firstname" name="firstname" class="form-control" data-error="please fill name" required/>
        <span class="glyphicon form-control-feedback" aria-hidden="true"></span>
        <span class="help-block with-errors"></span>
    </div>

    <div class="form-group has-feedback">
        <label for="lastname">Family</label>
        <input type="text"  id="lastname" name="lastname" class="form-control" data-error="please fill lastName" required/>
        <span class="glyphicon form-control-feedback" aria-hidden="true"></span>
        <span class="help-block with-errors"></span>
    </div>

    <div class="form-group has-feedback">
        <label for="lastname">phone</label>
        <input type="text" onkeypress="return isNumber(event)"  id="phone" name="phone" class="form-control" data-error="please fill phone" required/>
        <span class="glyphicon form-control-feedback" aria-hidden="true"></span>
        <span class="help-block with-errors"></span>
    </div>
    <div class="form-group has-feedback">
        <label for="lastname">email</label>
        <input type="text" onkeypress="return isNumber(event)"  id="email" name="email" class="form-control" data-error="please fill email" required/>
        <span class="glyphicon form-control-feedback" aria-hidden="true"></span>
        <span class="help-block with-errors"></span>
    </div>

    <input type="submit" id="Register" value="Register" class="btn btn-  success"/>
</form>
</div>
<%@include file="footer.jsp" %>

这是我的servlet课程:。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。

public class StudentServlet extends HttpServlet {

StudentServiceInter service = new StudentServiceImpl();
ObjectMapper mapper = new ObjectMapper();
String json = "";

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    PrintWriter out = resp.getWriter();

    int id = Integer.parseInt(req.getParameter("data"));
    Student student = service.findOne(id);
    json = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(student);
    out.print(json);
}

@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    String json = req.getParameter("data");
    Student student = mapper.readValue(json, Student.class);
    service.saveOrupdate(student);

}

@Override
protected void doPut(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    PrintWriter out = resp.getWriter();
    String json = req.getParameter("data");
    out.print("edited successfully");
}

@Override
protected void doDelete(HttpServletRequest req, HttpServletResponse resp)       throws ServletException, IOException {
    PrintWriter out = resp.getWriter();
    int id = Integer.parseInt(req.getParameter("id"));
    service.delete(id);
    out.print("deleted successfully");
}
}

但没用谁能帮我。

ltskdhd1

ltskdhd11#

您还必须有web.xml,其中指明了servletMap:

<servlet>
    <servlet-name>StudentServlet </servlet-name>
    <servlet-class>package.to.servlet.StudentServlet </servlet-class>
</servlet>  
<servlet-mapping>
    <servlet-name>StudentServlet </servlet-name>
    <url-pattern>/student/studentRegister/StudentServlet</url-pattern>
</servlet-mapping>

相关问题