Calling Java inside JavaScript Function

后端 未结 8 958
时光取名叫无心
时光取名叫无心 2020-12-04 02:26

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


    

        
相关标签:
8条回答
  • 2020-12-04 02:36

    Use JSP code

        <% 
        // Respond to the application with 1) result, 2) update, and 3) updateid values
        String result = "blablabla";
    
        out.print(result); 
        %>
    
    0 讨论(0)
  • 2020-12-04 02:37

    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.

    0 讨论(0)
  • 2020-12-04 02:41

    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)

    0 讨论(0)
  • 2020-12-04 02:42

    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>
    
    0 讨论(0)
  • 2020-12-04 02:48

    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.

    0 讨论(0)
  • 2020-12-04 02:50

    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.

    0 讨论(0)
提交回复
热议问题