On the web page, I use JQuery to send Ajax call to my servlet.
function sendAjax() {
$.ajax({
url: "/AddOrUpdateServlet",
type: 'POST',
dataType: 'json',
data: {"name": "hello world"},
contentType: 'application/json',
mimeType: 'application/json',
success: function (data) {
},
error:function(data,status,er) {
alert("error: "+data+" status: "+status+" er:"+er);
}
});
}
In the AddOrUpdateServlet's doPost, I have the following code:
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
String name = request.getParameter("name");
if (name == null)
System.out.println("null");
/*
response.setContentType("application/json");
response.setCharacterEncoding("UTF-8");
response.getWriter().write(new Integer(1).toString());
response.getWriter().close();
*/
}
I can see the parameter was successfully sent and received by the servlet as the console printed out "null". But why was the servlet unable to get the "name" param?