Solved

How to call Javascript function in Java

Posted on 2012-04-10
4
685 Views
Last Modified: 2012-08-13
Developer wrote a code in Java .I want to write new functionality add button but thing is Previous developer wrote a code in Java Class..
Now I have  add a  button and call a function
function in java servlet
public chkpop()
{
..
//code I write ..
            out.write("<tr> <td><input type=button name=Excel value=excel onclick='javascript:test()' > </td></tr>");

}

not sure how to call Java function from onclick or should I Call Javascript function from Java class but then using Ajax I can all java function ...

but If i used above code where  calling  test() a java script function but Where I have to define the Javascript function  test(){} ?

test function should be define in JSP or Javascript file ..How I define in Java class where test() function lies in JSP or Javascript file ..

Or is there any way I can Call Java function on Onclick="<%test()%>"
0
Comment
Question by:harshalpotdar
4 Comments
 
LVL 20

Expert Comment

by:chaitu chaitu
ID: 37831108
test function should be define in JSP or Javascript file

you can define either in jsp(withing script blocks) file or in js file.
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 37831413
and onclick='javascript:test()'  should just be
onclick='test()'

unless you have VBScript as the first script block in the browser

onclick is not the event handler I would choose on an input FIELD - a button yes (except the submit button - use onsubmit of the form instead)
0
 
LVL 29

Expert Comment

by:Pravin Asar
ID: 37832108
0
 
LVL 11

Accepted Solution

by:
SANDY_SK earned 500 total points
ID: 37851975
hi harshalpotdar,

You cannot just call a server side java code on click of a button on the client side. But there is one way of doing it, that is using DWR (http://directwebremoting.org/dwr/index.html), Even in this case what he does internally is that he uses AJAX and gives you the convenience of calling server side java code directly from javascript.

However if you do not want to use that, what you could do is write the AJAX code in a javascript file(YourScriptFile.js) and then include it like this

public chkpop()
{
..

//Line added
out.write("<script language=\"javascript\" src=\""+request.getContextPath()+"\"/js/YourScriptFile.js\"></script>");

//code I write ..
out.write("<tr> <td><input type=button name=Excel value=excel onclick='javascript:test()' > </td></tr>");

}

Open in new window


OR

Another way is that (Though this is not a great way of doing things) you could write the fill javascript code using out.println, something like this


public chkpop()
{
..
out.println("<script>");
out.println("    function test() { ");
out.println("        alert('This is a test function'); ");
out.println("        //Your rest of the AJAX logic can come here; ");
out.println("    } ");
out.println("</script>");

//code I write ..
out.write("<tr> <td><input type=button name=Excel value=excel onclick='javascript:test()' > </td></tr>");

}

Open in new window

0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
JavaScript has plenty of pieces of code people often just copy/paste from somewhere but never quite fully understand. Self-Executing functions are just one good example that I'll try to demystify here.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

756 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question