Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

How to call Javascript function in Java

Posted on 2012-04-10
4
Medium Priority
?
721 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 2000 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
The viewer will learn how to dynamically set the form action using jQuery.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses

879 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