0

I have a JSON string in my javascript, I need to send this to a java class. Can I use a plain java class or is it necessary to use a servlet?

Also, How can I parse the JSON data?

My JSON data is of the following format:

 var details=   { "CustomerDetails": [{'name':'Amy','age':'23'},
{'name':'Amj','age':'25'},{'name':'Amg','age':'27'}]};

I send the json string like this:

  var jsonText = JSON.stringify(details);  

    $.post('ServletClass', jsonText);  

WEb.xml:

<servlet>
<servlet-name>ServletClass</servlet-name>
<display-name>ServletClass</display-name>
<servlet-class>java.files.ServletClass</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>ServletClass</servlet-name>
<url-pattern>/ServletClass</url-pattern>
</servlet-mapping>

On my servlet side: I try to do a system.out.println, but get nothing, is the transfer even happening properly

package XXXX;

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.IOException;
import java.files.JSONObject;
import java.files.JSONSerializer;

public class ServletClass extends HttpServlet {
  public void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {

  }

  public void doPost(HttpServletRequest request,HttpServletResponse response)throws ServletException, IOException {
      //          doGet(request, response);
      //JSONArray arrayObj = new JSONArray();
      JSONObject jsonObj = new JSONObject(jsonText);
      JSONArray the_
      json_array = jsonObj.getJSONArray("CustomerDetails");
      System.out.println("FINALLY HERE");



  }
}
Haran Murthy
  • 341
  • 2
  • 10
  • 30

2 Answers2

2

First of all, you JSON is incorrect. Corrected JSON:

{ "CustomerDetails": [{'name':'Amy','age':'23'},{'name':'Amj','age':'25'},{'name':'Amg','age':'27'}]}

You should use AJAX to send data to the java class if you want it to be asynchronous. You will require a servlet to accept this request, that will parse this JSON and you can follow with the necessary processing on it. Look into Jackson or GSON libraries to do JSON parsing.

pb2q
  • 58,613
  • 19
  • 146
  • 147
devang
  • 5,376
  • 7
  • 34
  • 49
  • is there an example you can guide me to... I am a newbie to js and JSON – Haran Murthy Jul 31 '12 at 20:00
  • 1
    For AJAX, you can use [jQuery library](http://api.jquery.com/jQuery.ajax/) to communicate with your servlet. For using GSON, you can refer examples [here](http://stackoverflow.com/questions/5490789/json-parsing-using-gson-for-java) and [here](http://www.javabeat.net/2012/04/parsing-json-using-java-and-gson-library/). [Here](http://veerasundar.com/blog/2008/12/implementing-ajax-in-java-web-application-using-jquery/) is an example of a client invoking AJAX call to a servlet. – devang Jul 31 '12 at 20:08
  • As you try stuff, post your code in case you have questions or blocked on anything. – devang Jul 31 '12 at 20:09
  • @HaranMurthy, I did not quite understand your question. – devang Jul 31 '12 at 20:20
  • sorry hit enter by mistake..... edited the main question. I am not sure why my servlets post method isn't showing the sysout – Haran Murthy Jul 31 '12 at 20:20
  • @HaranMurthy, could you post your servlet class code where you try to read the variable from the request. – devang Jul 31 '12 at 20:25
  • done... as you can see i do not see the print statement in the logs – Haran Murthy Jul 31 '12 at 20:30
  • In statement `JSONObject jsonObj = new JSONObject(jsonText);`, where is the `jsonText` coming from? – devang Jul 31 '12 at 20:38
  • @gotuskar- yup haven't completed that part yet, but my question is there is no call made to the post method in the servlet. how does that happen. – Haran Murthy Jul 31 '12 at 20:45
  • You code wont compile because the variable `jsonText` is not declared. Try putting just the `System.out.println()` statements and remove everything else to make sure your servlet is being called. See the logs for possible run time exceptions. – devang Jul 31 '12 at 20:47
  • No didnt work, I put the sysout in the first line by removing rest of the lines, the thing i dont get is how do i know the post method in the servlet is getting called and not the get – Haran Murthy Jul 31 '12 at 20:49
  • Because you are calling `$.post(...)`. Try hitting the URL from browser to invoke your servlet. – devang Jul 31 '12 at 20:50
  • Thanks for your patience in helping me, I tried to invoke it directly it threw a 404 error, does that mean my servlet mapping is incorrect? – Haran Murthy Jul 31 '12 at 21:14
  • I built the war and invoked the class file with the host post address and the full path – Haran Murthy Jul 31 '12 at 21:18
  • Thanks gotuskar... url was the issue, For some reason The url sent via ajax didn't generate properly. I updated the url and it works like a charm – Haran Murthy Aug 01 '12 at 13:51
0

The following change worked for me: fullpath=contextpath\servletname

$.post(fullpath, {jsonText:jsonText}, function(data) {
    alert(data);
});
Haran Murthy
  • 341
  • 2
  • 10
  • 30