У меня возникла проблема при отправке данных json из начальной загрузки в экспресс-сервлет.Вот что я попробовал.Я использую ajax для отправки данных из моей формы в класс сервлетов с помощью json.Пожалуйста, помогите мне решить эту проблему. Вот моя форма регистрации студента:
<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>
, и это мой класс сервлетов для отправки данных в базу данных.
@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();
}
}