-1

I try to send data in JSON format from angularJS client thanks to post http request and get it thanks to j2ee servlet. But I meet a mistake. My complete data can be access thanks to getParameterNames method in my servlet and I can't get it in other way.

I don't understand why my data is the Key and not a value.

AngularJS Client

setParametersForTools : function (toolName, data) {
    var jsonData = JSON.stringify(data) // I try with json and stringify json
    var promise = 
    $http({
        url: configuration.root_url_api+"SetParametersServlet?tool="+toolName,
        method: "POST",
        dataType: 'json',
        data: data,
        headers: {'Content-Type': 'application/x-www-form-urlencoded'}
    })          
    .then(function (response){
        console.log(response);
    }, function (error){
        console.log(error);
    })
    return promise;
}

Servlet

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    String toolname = request.getParameter("tool"); //toolname is correct

    String json = request.getParameter("data");  // return null...

    Enumeration<String> paramsName = request.getParameterNames();
    for (;paramsName.hasMoreElements();) {
        String paramName=paramsName.nextElement();
        System.out.println("param:"+paramName);
    }
}

Servlet log

//For Parameter names
param:tool
param:{ my correct data in json format}

Maybe I don't send data correctly but after many searches I don't understand what's wrong.

Willi Mentzel
  • 27,862
  • 20
  • 113
  • 121
Kael
  • 858
  • 9
  • 21

3 Answers3

0

Please make the following changes in your code.

setParametersForTools : function (toolName, data) {
    $http.post(configuration.root_url_api+"SetParametersServlet?tool="+toolName, data)          
         .then(function (response){
               console.log(response);
          }, function (error){
               console.log(error);
     });
 }
Mohan Singh
  • 883
  • 8
  • 18
  • First, to resolve CORS problem, I must insert `{headers: {'Content-Type': 'application/x-www-form-urlencoded'}`. Then, I already test this, the problem persists. I really don't understand why data is present as parameter name and not value.. – Kael Aug 26 '15 at 10:58
  • Can i have look at data what does it contain ? – Mohan Singh Aug 26 '15 at 11:06
  • Yes, it's : `{"particularProperties":[{"PROP_A":[{"libelle":"a","value":50,"key":"A"},{"libelle":"b" ,"value":50,"key":"B"},{"libelle":"c","value":50,"key":"C"},{"libelle":"d","value":50 ,"key":"D"}]}],"generalProperties":[{"PROP_B":[{"libelle":"e","value":65,"key":"E"},{"libelle":"f","value":5,"key" :"F"},{"libelle":"g'","value":10,"key":"G"},{"libelle":"h","value":20,"key":"H"}]}]}` I don't change the syntax but change data for confidentiality ;) – Kael Aug 26 '15 at 11:10
  • mh, sorry but I'm new in java and I can't print it. It's memory address.. ^^ How do this ? – Kael Aug 26 '15 at 11:25
  • @mukesh-kamboj Can you help him for java part – Mohan Singh Aug 26 '15 at 11:31
  • The problem is fixed thanks to [link](http://stackoverflow.com/a/30582142/3629127). I'll post the good answer soon. – Kael Aug 26 '15 at 11:43
0

If you want to use json I'd suggest to implement JAX-RS service instead of Servlet, it will be much easier, especially if you will later use some more complex json.

Pseudo code for service (you will need to add jax-rs configuration to web.xml also):

@Path("/myPath")
public class MyService {

    @POST
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    public OutputData setParameters(InputData data, @QueryParam("tool") String tool) {

        System.out.println("Input data: " + data);
        System.out.println("Tool name: " + tool);
        ...
        return outputData;
    }
}

where inputData and outputData are Java objects representing json.

Gas
  • 17,601
  • 4
  • 46
  • 93
  • I'm not choice. I must do this with servlet. But thank you – Kael Aug 26 '15 at 10:55
  • 1
    @Kael In that case check this [post](http://stackoverflow.com/questions/11442632/how-can-i-post-data-as-form-data-instead-of-a-request-payload) it may provide you with some hints/solutions. – Gas Aug 26 '15 at 11:14
0

I found a good answer with this post.

$httpParamSerializer method fixes the problem

Community
  • 1
  • 1
Kael
  • 858
  • 9
  • 21