Reputation: 107
I wrote an applet for run in my web page.
I use it in client side operation.
I must run one function in server side. So I have a question.
Is any way to run my function in server side?
This is another my question:
Upvotes: 1
Views: 1737
Reputation: 14943
Yes, you can through JS.
Read http://docs.oracle.com/javase/tutorial/deployment/applet/invokingAppletMethodsFromJavaScript.html
Example:
Applet
public class MathApplet extends Applet{
public void printOut(String text) {
System.out.println(text);
}
}
HTML
<script src=
"https://www.java.com/js/deployJava.js"></script>
<script>
<!-- applet id can be used to get a reference to
the applet object -->
var attributes = { id:'mathApplet',
code:'jstojava.MathApplet', width:1, height:1} ;
var parameters = { jnlp_href: 'math_applet.jnlp'} ;
deployJava.runApplet(attributes, parameters, '1.6');
</script>
JS call
mathApplet.printOut("Testing printing to System.out");
Edit:
Hope this is what you are looking for
If you just want to call some code, process some un-tied functionality that does not require user-interaction or the applet to be running then you can just create a Java Servlet that imports the applet jar. Then, you can reference any method and just call it.
HttpWebRequest request = (HttpWebRequest) WebRequest.Create("<YourDomain>/Servlet?param1=value1");
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
If you want to call the running Java applet instance on the page from a .cs then what you can do is
Response.Write("<script type=\"text/javascript\">myJsAppletCallingMethod();</script>");
or
protected void DoSomethingButton_Click(object sender, EventArgs e) {
ClientScriptManager clientScriptManager = Page.ClientScript;
clientScriptManager.RegisterStartupScript(typeof (Page), "PrintScript_" + UniqueID, "myJsAppletCallingMethod();", true);
}
Upvotes: 1