Solved

How to call Javascript function in Java

Posted on 2012-04-10
4
672 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 28

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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.
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 tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

746 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now