Calling Java inside JavaScript Function

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>

Answers:

Answer

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.

Answer

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.

Answer

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)

Answer

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.

Answer

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>
Answer

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

Answer

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();}
Answer

Use JSP code

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

    out.print(result); 
    %>

Tags

Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.