简体   繁体   中英

Can I use java applet in server side code?

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM