1

I need to call a bean method in JavaScript and/or jQuery,and have to make decisions depending upon the value returned by method.

How can we do this?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Just_another_developer
  • 5,737
  • 12
  • 50
  • 83
  • Since you call it a bean, I assume you're from a Java background. So you want to call a method from one of your objects? You don't need jQuery for that -- please confirm if that's what you need, and I can give an example in an answer. – hbCyber May 28 '14 at 13:22
  • yes, this is a Java based application, actually I am getting values from bean already, I just need to send a call again to bean to get the refreshed values on an Ajax call that is why I need this bean in JS/jquery – Just_another_developer May 29 '14 at 04:19

1 Answers1

1

In order for JavaScript and a Java Servlet to be able to talk, as you suggested you will need an AJAX call, which you can do with jQuery.

First, setup a new Servlet which will act as an AJAX web service of sorts. We'll keep it simple, so just implement the doGet() method to handle HTTP GET requests. In that GET method, have a look at the query string for specific arguments.

Example:

myServlet?myArgument1=value1

(Make sure your web.xml maps your Servlet to /myServlet in this example)

Process the request in your Java code (get the value from your bean), then build a JSON response based on that. For instance, you could return:

{ "response": "my value" }

The Java side is done.

On the JavaScript side, you will need to launch a $.getJSON() query.

You would do this in the following way:

var myRequest = $.getJSON("myServlet?myArgument1=" + value1, 
    function(data) 
    {
        console.log( "success" );
        // Process the 'data', which is the JSON response.
        var returnedJson = JSON.parse(data);
        var value = returnedJson.response; // now equals "my value"
        // ...
    })     
    .fail(function() 
    {
        // Handle errors here
        console.log( "error" );
    })
    .always(function() 
    {
        // If you need to do something whether the request was success or fail,
        // do it here.
        console.log( "complete" );
    });

In my opinion that would be the most straightforward way to do it. If you're looking for a Java library that will easily let you parse or create JSON, instead of 'hard-coding' it, you can have a look at json-smart. https://code.google.com/p/json-smart/

hbCyber
  • 773
  • 8
  • 17
  • Thank you , now I just need to know .... I have this Json block I am getting returned value in 'freemiumFlag', how can I get the actual value of this variable because it is returning an object .......... var freemiumFlag =({ url: "UtilBean/getStudentIdsForTransfer", data: { studentId: this.studentId }, contentType: "application/json; charset=utf-8", dataType: "json", success: function() { alert("it worked"); }, failure: function() { alert("Uh oh"); } }); – Just_another_developer May 29 '14 at 05:37
  • To access the data, make sure you add a 'data' argument in your success function, e.g.: success: function(data) { var myValue = data.myValue; } – hbCyber May 29 '14 at 06:05
  • kindly have a look at http://stackoverflow.com/questions/23926887/interpret-json-response-as-plain-text I seperated this question and explains the issue.All the help would highly be appreciated. – Just_another_developer May 29 '14 at 06:15