65.9K
CodeProject is changing. Read more.
Home

Running a Java applet from an ASP.NET website

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.40/5 (4 votes)

Jan 2, 2013

CPOL

1 min read

viewsIcon

49218

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.