0

I have a JQuery code that opens a Modal Popup. I would like to execute this JQuery code (function) from a method in the Servlet.

I want to achieve one of these:

  1. Call the JQuery method that is defined in a JSP page from the Servlet.
  2. Execute the JQuery code inside a method in the Servlet.

Is this possible? I couldn't find anything about this.

Swag
  • 2,090
  • 9
  • 33
  • 63

2 Answers2

0

This is not possible.

JQuery code is javascript code, and it's executed inside the browser.

The Servlet code is java code, and it's executed on the server side.

Danix
  • 1,947
  • 1
  • 13
  • 18
  • I wouldn't say that. Javascript is a complete language and can be executed either at server side or client side. – Hirak May 18 '14 at 15:16
  • @Hirak, you are right, but in this case, we want to execute some JQuery code. JQuery usually manipulates the DOM, but you don't have any DOM on the server side. So it doesn't really make sense to execute the js code in the Servlet container. – Danix May 19 '14 at 09:29
-1

I am not sure why you would do that.... but if I understand the question correctly, here is one way to do it:

  1. Parse the jsp in your servlet to get hold of the jquery javascript code. Load the jquery code in a stringreader.
  2. Parse the javascript function using the embedded scriptengine..... code follows:
ScriptEngineManager factory = new ScriptEngineManager();
 ScriptEngine engine = factory.getEngineByName("JavaScript");
 engine.eval("<jquery.js code in string form>");
 Invocable inv = (Invocable) engine;
 inv.invokeFunction("someFUnction", param1, param2);
  1. Regarding the modal popup: If the function returns the html code for popup, then that can be captured in your servlet. The invokeFunction return an Object containing whatever the function outputs. You need to process the object as per need. However, if the function, draws the modal popup in line (using document.innerhtml), then you cannot get hold of the popup... IMHO.
Hirak
  • 3,601
  • 1
  • 22
  • 33