1

I am using Extjs 4 and Java Servlets in my app. I want to post some data to the server in json format.

The format of JSON should be like this:

{
    "credentials":[
    {
       "username":"george",
       "password":"xyz"
    }
    ]
}

I did this in extjs code:

buttons: [{
        text: 'Submit',
        handler: function() {               
            Ext.Ajax.request({
                url: '/Model/FormServlet',
                method: 'POST',
                jsonData : {
                    //Hardcoded values
                    username: "george", 
                    password: "xyz"
                },
                callback: function (options, success, response) {                       
                    alert(response.responseText);
                }
            });
        }           
    }]

1) I believe that I wont get the JSON format which I posted above. Please let me know how to get that format using jsonData.

2) How to retrieve this in servlet doPost method? Since I have not used params and have replaced it with jsonData hence request.getParameter is not going to work in the servlet doPost method.

Please let me know how to make this work.

Regards,

Abubakkar
  • 15,488
  • 8
  • 55
  • 83
user182944
  • 7,897
  • 33
  • 108
  • 174

3 Answers3

3

1) You are OK

2) This is a POST request; the data will not come in a parameter (e.g. not multipart/form-data) but will be the content of the HTTP request. In doPost, simply read everything in request.getInputStream() and transform it to String using the proper encoding or call request.getContent() directly if you have a proper ContentHandler registered.

Something along the lines of

byte[] ba = IOUtils.toByteArray( req.getInputStream(  ) );
String charset = TypedContentUtils.extractCharset( req.getContentType(  ) );
String json = new String( ba, charset );

IOUtils is Apache Commons IO (writing your own is 10 lines with proper Exception handling); TypedContentUtils.extractCharset is a simple home grown method extracting the charset from the Content-Type or defaulting to utf-8.

Bruno Grieder
  • 28,128
  • 8
  • 69
  • 101
3

I tried this bit of code in doPost method and it worked:

StringBuffer jb = new StringBuffer();
      String line = null;
      try {
        BufferedReader reader = req.getReader();
        while ((line = reader.readLine()) != null)
          jb.append(line);
        System.out.println(jb.toString());
      } catch (Exception e) { 
          e.printStackTrace();        
      }

Reference article:

HttpServletRequest get JSON POST data

Community
  • 1
  • 1
user182944
  • 7,897
  • 33
  • 108
  • 174
2

1) It will post the data as it is. The content will be : { username: "george", password: "xyz"}, if you want the format above and it is hardcoded then you can juste replace it.

2) You can easily deserialize your json into a java object with genson library http://code.google.com/p/genson/.

UserCredentials userCredentials = new Genson().deserialize(request.getReader(), UserCredentials.class);
eugen
  • 5,856
  • 2
  • 29
  • 26
  • Firstly, I dont want to use any third party library. Secondly, The problem is with retrieving the JSON in a servlet. Once I am able to retrieve it in servlet doPost, I can deserialize it into java object using some utility methods already present in my app workspace. – user182944 Oct 24 '12 at 12:42
  • 1
    @user182944 Ok, to retrieve the json content use request.getReader() or request.getInputStream() (both work) – eugen Oct 24 '12 at 12:49
  • Thanks for the help. It really showed me pointers towards right direction. – user182944 Oct 24 '12 at 13:15