Solved

How to call Javascript function in Java

Posted on 2012-04-10
4
695 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

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

Styling your websites can become very complex. Here I'll show how SASS can help you better organize, maintain and reuse your CSS code.
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

623 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