7

Please tell me if we can call java inside javascript function ?

<HTML><HEAD></HEAD><BODY>
    <SCRIPT>
        function getScreenDimension() {
            <% System.out.println("Hiiiiiiiii"); %>
        }
    </SCRIPT>
    <FORM>
        <INPUT type="button" value="call Java method direct"  onClick = "getScreenDimension()">
    </FORM>
</BODY></HTML>
Thilina Sampath
  • 3,615
  • 6
  • 39
  • 65

8 Answers8

5

While the answer of "No" is technically correct based on the phrasing of the question. You may want to read up on AJAX. It is a way for javascript to make a request to your backend code (in this case Java).

Javascript is client side, meaning it is run by the user's browser. Java is running on your server. In order for the client side javascript to interact with the backend Java, you need to make a request to the server.

James Montagne
  • 77,516
  • 14
  • 110
  • 130
2

My question to you would be, "What are you trying to do, and what do you expect to see?".

You have to realize that there are two different execution contexts. The first is the JSP itself whose code is executed by the JVM on the server-side, and the second is the Javascript that is executed by the browser. So when the code goes to the browser, you'll see: So the System.out.println will cause Hiiiiiiiii to be printed to the server logs, but you won't see anything on the browser. In fact, the Javascript code on the browser will look like this:

function getScreenDimension() {




}

Which is not valid Javascript code. The code in the JSP is run before the Javascript gets run on the browser. So to "run" Java code, you need to make a request to your server either by posting the form or with an AJAX call. This will cause Java code in the appropriate servlet or controller, to run.

UPDATE

After glancing at your code, it appears that you want to call a Java method directly. This is not possible with your current code. You might want to read up on AJAX. This will point you in the right direction.

Vivin Paliath
  • 94,126
  • 40
  • 223
  • 295
  • No, he actually won't see that because System.out.println() will output to logs file. I think what you mean is if he had out.println() – Amir Raminfar Jul 15 '11 at 16:51
2

JSP runs on the server. It generates a document which the server sends to the browser. That is the end of the involvement of JSP in the process. The browser then parses the document and runs any JS.

You can include JSP in a script element, it just has to output valid JavaScript.

You cannot have JSP that runs in response to JavaScript, other then when JavaScript causes the browser to issue a new HTTP request (either setting location.href, submitting a form, adding an image, or using Ajax, etc)

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
1

Yes, you can. Use JSP expressions <%= %>. Example:

<aui:script use="aui-datepicker">
    AUI().use('aui-datepicker', function(A) {
        new A.DatePickerSelect({
            calendar : {
                dates : [ '<%="1/1/1970" %>' ],
            }
        }).render('#myDatePicker');
    }); 
</aui:script>
LovaBill
  • 5,107
  • 1
  • 24
  • 32
1

I think you have lack of understanding of what is going on here. Anything in middle of <% %> is executed when the page is first requested. Anything in javascript is executed when the browser calls it. What you have will never happen and there is no way to make it happen. However, you can use AJAX to do something like this but that's a different question.

Amir Raminfar
  • 33,777
  • 7
  • 93
  • 123
0

You can. In JSF you can use PrimeFaces' component p:remoteCommand, which would contain action method. Call that remoteCommand by its name from JS and the java method will get executed.

JSF Page

    <p:remoteCommand name='rmt' action="#{bean.doWork()}"/> 

In JavaScript

    function callJava {rmt();}
max3d
  • 1,437
  • 15
  • 16
0

Yes, you can call Java from Javascript, both if you mean the Java-VM on the server and on the client; i assume you mean the client (the VM in the browser); take a look here:

http://www.apl.jhu.edu/~hall/java/Java-from-JavaScript.html

Bernd Elkemann
  • 23,242
  • 4
  • 37
  • 66
-1

Use JSP code

    <% 
    // Respond to the application with 1) result, 2) update, and 3) updateid values
    String result = "blablabla";

    out.print(result); 
    %>
Londeren
  • 3,202
  • 25
  • 26