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,8017 gold badges43 silver badges67 bronze badges
asked Jul 15, 2011 at 16:43
2
  • Is all you want to do print out data? Commented Jul 15, 2011 at 16:46
  • Since when were HTML tags in uppercase? Commented Jun 26, 2021 at 1:20

8 Answers 8

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.

answered Jul 15, 2011 at 16:47
Sign up to request clarification or add additional context in comments.

Comments

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.

answered Jul 15, 2011 at 16:46

1 Comment

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()
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)

answered Jul 15, 2011 at 16:48

Comments

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.

answered Jul 15, 2011 at 16:48

Comments

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>
answered Mar 12, 2015 at 10:24

Comments

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

answered Jul 15, 2011 at 16:50

Comments

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();}
answered Jul 11, 2015 at 20:03

Comments

-1

Use JSP code

 <% 
 // Respond to the application with 1) result, 2) update, and 3) updateid values
 String result = "blablabla";
 out.print(result); 
 %>
answered Jul 15, 2011 at 16:51

1 Comment

This is not really answer to his question.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.