0

I am using ajax to pass data to servlet. From what I know the standard way usually goes like this:

$.ajax({

               url: 'page.jsp',
               type: 'POST',
               data: {

                     id:value,
                     name:value2

               },

               success: function (data) {
                   alert("Successfully initiated email to queue");
               },
               error: function (request, error) {
                   alert("Request: " + JSON.stringify(error));
               }
           });

And in the jsp page, one would retrieve the data like this:

String id = request.getParameter("id");
String name = request.getParameter("name");

This would work no doubt. Now what if I want to store the data as objects. Like this in my JavaScript:

 var data;

     if(condition){

       data={
                'recipient': recipient,
                'subject': subject,
                'content': content,
                'id':"<%=id%>",
                'hash':"<%=hash%>",
                'multiemail':"no"

            }

    }else{

          data= {
                'recipient': recipient,
                'subject': subject,
                'content': content,
                'arrayList':<%=array%>,
                'multiemail':"yes"

            }

    }
  $.ajax({

               url: 'page.jsp',
               type: 'POST',
               data: {

                     info:data

               },

               success: function (data) {
                   alert("Successfully initiated email to queue");
               },
               error: function (request, error) {
                   alert("Request: " + JSON.stringify(error));
               }
           });

Then, I used the same way:

 String recipient = request.getParameter("recipient");

And this would returns null value. How do I exactly retrieve the data value I want from the object?

halfer
  • 19,824
  • 17
  • 99
  • 186
Daredevil
  • 1,672
  • 3
  • 18
  • 47
  • https://stackoverflow.com/questions/34179986/how-to-retrieve-data-from-json-file-using-jquery-and-ajax check if you can find anything similar here – Akshay Mulgavkar Apr 26 '19 at 04:30
  • @AkshayMulgavkar: He mentions `servlet`. He's trying to get the JSON value in Java – slebetman Apr 26 '19 at 04:32
  • @AkshayMulgavkar No that's not what i was looking for. – Daredevil Apr 26 '19 at 04:33
  • Don't have time to write full reply but you should google about `java jackson` for how to deal with JSON data (the serialized form of js objects) and read this answer for how to get that data: https://stackoverflow.com/questions/14525982/getting-request-payload-from-post-request-in-java-servlet – slebetman Apr 26 '19 at 04:34
  • Based on the link you gave,it tells me to use `request.getreader`. But how exactly to I get the "particular" data in the object? – Daredevil Apr 26 '19 at 04:48
  • I pointed out [in the comments here](https://stackoverflow.com/q/55467579/472495) that the community has removed home-made tags from your titles. Volunteer editors do not want to keep doing so, as they are busy enough as it is. **Please refrain from adding tags to your titles**. A canonical meta discussion is available on request. – halfer Apr 26 '19 at 19:20

2 Answers2

0

When you used the following line of code String recipient = request.getParameter("recipient"); It looks for a recipient key in data in

           $.ajax({
           url: 'page.jsp',
           type: 'POST',
           data: {

                 info:data

           }

but hardluck there is no recipient, there is only info key in your ajax. Therefore you can use getParameter("info") to get the data. Now you have your data.

Refer the following code

$.ajax({
           url: 'page.jsp',
           type: 'POST',
           data: data

I think now you can use String recipient = request.getParameter("recipient");

Nikhil Unni
  • 739
  • 5
  • 12
0

request.getParameter("recipient"); will look for recipient key in your data. But your key here is info not recipient (which is part of info). To access recipient, you have to first parse received JSON (request.getParameter("info")) using any JSON parsing library and then access recipient from the parsed JSON object.

In your ajax, pass your data in json format

$.ajax({

           url: 'page.jsp',
           type: 'POST',
           dataType: 'JSON',
           data: {
                 info:data
           },

           success: function (data) {
               alert("Successfully initiated email to queue");
           },
           error: function (request, error) {
               alert("Request: " + JSON.stringify(error));
           }
       });

In your servlet side, parse json like this :

JsonParser parser = new JsonParser();

String json = request.getParameter("info");

JsonElement jsonTree = parser.parse(json);
String recipientjsonTree.get("recipient");

JsonParser is part of GSON library.

Abhijeet
  • 4,069
  • 1
  • 22
  • 38