Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Running a Java applet from an ASP.NET website

0.00/5 (No votes)
2 Jan 2013 3  
Integration of a Java applet in ASP.NET.

Introduction

This article helps you to understand how to use Java applet in your ASP.NET website.

Background

Many times you need to access local files on client machine from your website. In dot net you can use ActiveX control.

But ActiveX you can run only in IE, and for other browser you need to make some settings which is not safe.

Using the code

Make one jApplet in net beans as given below :

add following function code on button click :

public String readFile(String fn) { 
   String thisLine, ret = ""; 
 try { 
   FileInputStream fin =  new FileInputStream(fn); 
   BufferedReader myInput = new BufferedReader 
                     (new InputStreamReader(fin));
   while ((thisLine = myInput.readLine()) != null) {  
     ret += thisLine + "\n";
   } 
 } catch (Exception e) {
   ret = "Cannot load, exception!";
 } 
 return ret;
}

Then build this applet.

Then right click project-> properties-> Web start-> Under signing->Customize->select Self-sign by generated key-> clock OK

This will sign your jar file of your java applet project.

Then make one asp.net website add one folder name it as applet and copy jar files and class files of applet as shown below:

Then add below code in .aspx page

<applet code="javaapplication4/TestApplet.class" 
       archive="applet/TestApplet1.jar" width="325" height="325">  

you can also call applet using deploy java script:

<script src="http://www.java.com/js/deployJava.js" type="text/javascript"></script>
<pre><script type="text/javascript">
    var attributes = { code: 'javaapplication4.TestApplet.class',
        archive: 'applet/TestApplet1.jar', 
        width: 325, height: 325 
    };
    var parameters = { fontSize: 16 };
    var version = '1.6';
    deployJava.runApplet(attributes, parameters, version); 
</script>  

then run the website

It will give you following pop up for enabling Java applet

Check the check-boxes and click Run

Then type any local file name in text box and click on Load

It will allow you to read any file on client machine and it will show you content of that file.

You can test this by deploying website on IIS and run it from different machine.

If you want to send some parameter to applet from asp page you can use this link: http://www.roseindia.net/java/example/java/applet/appletParameter.shtml

For more information you can use this link: http://www.developer.com/java/other/article.php/3303561/Creating-a-Trusted-Applet-with-Local-File-System-Access-Rights.htm

Points of Interest

It will reduce your time and efforts of setting security of browser for ActiveX. It works with all browser which supports Java.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here