-2

I want to call JavaScript function from a servlet using jQuery.

This is my JavaScript:

function showerrodailog(){

ShowDialogBox('Alert','No record found.','Ok','', 'GoToAssetList',null);
}

function ShowDialogBox(title, content, btn1text, btn2text, functionText,     parameterList) {

            var btn1css;
            var btn2css;

            if (btn1text == '') {
                btn1css = "hidecss";
            } else {
                btn1css = "showcss";
            }

            if (btn2text == '') {
                btn2css = "hidecss";
            } else {
                btn2css = "showcss";
            }
            $("#lblMessage").html(content);

            $("#dialog").dialog({
                resizable: false,
                title: title,
                modal: true,
                width: '400px',
                height: 'auto',
                bgiframe: false,
                hide: { effect: 'scale', duration: 400 },

                buttons: [
                                {
                                    text: btn1text,
                                    "class": btn1css,
                                    click: function () {

                                        $("#dialog").dialog('close');

                                    }
                                },
                                {
                                    text: btn2text,
                                    "class": btn2css,
                                    click: function () {
                                        $("#dialog").dialog('close');
                                    }
                                }
                            ]
            });
        }

I want to call this function from a servlet. How can I do that I tried from Google and find this code, however, I called that it says error $ not defined.

I used this for popup alert message.

/* ScriptEngineManager manager = new ScriptEngineManager();
                 ScriptEngine javascriptEngine = manager.getEngineByExtension("js");

                 // Get script from JS File
                 FileInputStream fileInputStream = new FileInputStream("F:\\workspace\\Userlogin\\WebContent\\js\\val.js");
                 if (fileInputStream != null) {
                     BufferedReader reader = new BufferedReader(new InputStreamReader(fileInputStream));

                     javascriptEngine.eval(reader);
                     Invocable invocableEngine = (Invocable)javascriptEngine;

                     // Invoke javascript function named "sayHello" with parameter "Atul"
                     Object object = invocableEngine.invokeFunction("sayHello", "Atul");
                     System.out.println("Result: " + object);*/

                // }
Jonathan Eustace
  • 2,469
  • 12
  • 31
  • 54
  • 1
    Where do you intend your JavaScript to run? Servlets execute serverside, and JavaScript (normally) executes in your browser. You can trigger your JavaScript to load in your page based on some event, like page load/document ready? Since you're using jQuery, take a look at: https://learn.jquery.com/using-jquery-core/document-ready/ – Kevin Hooke Jun 22 '15 at 05:16
  • 1
    You have an odd requirement here. Why do you need that the servlet executes and updates the view? – Luiggi Mendoza Jun 22 '15 at 05:22

1 Answers1

0

Your attempt is wrong, due to the following reasons:
A. JQuery and other JS libraries work on DOM that you have on client side, Your Servlet runs at server side.
B. Your Script does not include the JQuery library.

Maybe your Servlet should write on its output stream as response to the client an HTML + Script embedded in it, and this script includes the JQuery stuff and uses it?

Yair Zaslavsky
  • 4,091
  • 4
  • 20
  • 27