1

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:

What's servlet? How to use?

Community
  • 1
  • 1
R.E
  • 107
  • 1
  • 12
  • (1) Applets run in the browser. (2) You don't need an applet to run a server-side function. – user207421 Feb 06 '14 at 09:30
  • "Code" as an abstract entity can be run anywhere. If this one function is in a class on its own, then any JVM could load the class, and you could call that method (function). You would need to make the `.jar` available to whatever JVM you are running on the server. (Of course, the wisdom of organising your code this way is another thing entirely.) – Stewart Feb 06 '14 at 11:55

1 Answers1

1

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);
}

Credit 1 2

Community
  • 1
  • 1
Sully
  • 14,672
  • 5
  • 54
  • 79
  • Hi, My question is not this. I use my applet in HTML page in asp.net project already. And I use my function in applet in javascript. But I want to call my applet function in .cs file (Server side). – R.E Feb 06 '14 at 10:26