0

I wanted to use data from javascript/jquery in a java class.

I get data from another server as a postresult to my servlet. I want to use this data. This data is a JSON String. The Response looks like this:

[{"id":"1","bool_m":"0","name":"Test 1"},{"id":"2","bool_m":"1","name":"Test 2"},{"id":"3","bool_m":"0","name":"Test 3"}]

How can i archive this? If i create a new java-object and pass "data" into the constructor it wont know "data".

this is what i got(this code runs as a .jsp on a liferay-server (which uses tomcat)):

<script type="text/javascript">

    function getScenarioList() {
        $.post("url",
                {
                    action : "get_scenario_list"
                })
                .done(function(data) {
                    alert("Data Loaded: "+ data);
                    <%
                        Testclass one = new Testclass(data);
                    %>
                });
    }
</script>
Wandang
  • 912
  • 2
  • 8
  • 37

2 Answers2

2

You can do this by jackson . Please make a google search with JACKSON .

Human Being
  • 8,269
  • 28
  • 93
  • 136
  • there is data in my datavariable (coming from the post) how can i use that instead? – Wandang Jul 10 '13 at 08:29
  • i am sorry. in your code you are using alert(response). my goal is to use response in my own javaclass to modify it and give it back to javascript. it seems to me like you are not using a java class at all. – Wandang Jul 10 '13 at 08:41
  • Can you post the response that you got ? – Human Being Jul 10 '13 at 08:45
  • It is really not clear to me, what you want to achieve. If you want to modify the transmitted data, why don't you write such code within the servlet that receives the JSON? A servlet is meant to receive a request and return an appropriate response to it. – snrlx Jul 10 '13 at 08:50
  • i am new to javascript and jquery. therefore i wanted to handle as much as possible in java. – Wandang Jul 10 '13 at 08:53
  • thx for your effort, i had many false assumptions in my question. a friend of mine gave me a lecture and now i how this works (no need for java). sry for wasting your time. – Wandang Jul 10 '13 at 09:35
0

You cannot use Java in AJAX request.

JSP rendered on server-side like Servlet and at finish of rendering it is nothing from Java-code, only html (and javascript). And ajax runs on client-side. Also you don't have "Java" access at ajax (at client-side).

The right way is to create this Java class at server-side. You have the "url", what is behind this url? Another jsp or servlet or maybe Liferay-Action?

Mark
  • 17,887
  • 13
  • 66
  • 93