1

This is my js file where I am sending json data to singup.java page using ajax.

function signup()
{
    var name = document.getElementById('name').value;
    var mobileNo = document.getElementById('mobileNo').value;
    var emailId = document.getElementById('emailId').value;
    var password = document.getElementById('password').value;
    alert(name);
    alert(mobileNo);
    alert(emailId);
    alert(password);
    var data = {"signUp":[{"name":name,"password":password,"mobileNo":mobileNo,"emailId":emailId}]};
    alert(JSON.stringify(data));
    var sendData = function(data){   
    alert(JSON.stringify(data));
      $.ajax({
     url:'/Signup/signup',
     type: 'POST',
     contentType: 'application/json',
    data: JSON.stringify(data),
    success: function(response)
    {
            alert(response);
    },
});
};
sendData(data);
}

This is my signup.java page where I am trying to get json values. I don't know correct syntax. How to do json parsing in java?

package Json;

    try
    {
            String name =request.getParameter("name");
            String mobileno =request.getParameter("mobileNo");
            String email =request.getParameter("emailId");
            String password =request.getParameter("password");
            Class.forName("com.mysql.jdbc.Driver"); 
            java.sql.Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/demo","root","admin"); 
            PreparedStatement ps=con.prepareStatement("insert into comments values(?,?,?,?)");
            ps.setString(1, name);
            ps.setString(2,mobileno);
            ps.setString(3, email);
            ps.setString(4, password);
            ps.executeUpdate();
            out.println("inserted");
    }
    catch(Exception ee)
    {
       out.println("error"+ee.toString());
    }
}
Cœur
  • 37,241
  • 25
  • 195
  • 267

1 Answers1

1

request.getParameter is for Query String when your URI has the part ?name=toto&firstName=titi or with POST data from forms that use the same format.

In order to read json you have either to parse it yourself or use a parser.

I suggest you to use jackson https://github.com/FasterXML/jackson.

Once imported the basic thing to do is

new ObjectMapper().readerFor(MyClass.class).readValue(request.getInputStream());

For this to work your class must have : getter/setter, public constructor without arguments -> don't forget that if you declare one with argument, the default one become private so you have to declare it.

Edit : i suggest you if you chosse to use jackson to do send it like this : var data = [{"name":name,"password":password,"mobileNo":mobileNo,"emailId":emailId}];

And declare a class "MyClass" with the fields name,password, mobileNo, emailId

Walfrat
  • 5,363
  • 1
  • 16
  • 35
  • I am beginner to json. iI don't know which packages should I have to import in my singup.java page.So,will you please tell me that process in details .s – Dhondiram Jadhav Feb 06 '16 at 07:37
  • If you use maven or any dependecies manager get the library here : http://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind/2.7.1. Then the only import that you need is : import com.fasterxml.jackson.databind.ObjectMapper – Walfrat Feb 08 '16 at 08:06