Click here to Skip to main content
Click here to Skip to main content

Running a Java applet from an ASP.NET website

, 2 Jan 2013 CPOL
Rate this:
Please Sign up or sign in to vote.
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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

MAU787
Software Developer
India India
Experience in dot net specifically worked on bio metric devices.

Comments and Discussions

 
QuestionCalling .JAR in .NET through aspx page PinmemberManish_0071-Aug-14 1:00 
QuestionHelp me! Pinmemberwingchunpolice6-May-13 17:28 
AnswerRe: Help me! Pinmemberrevathivanga4-Sep-13 22:42 
GeneralMy vote of 5 PinmemberMAU7872-Jan-13 19:23 
excellent

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.141220.1 | Last Updated 2 Jan 2013
Article Copyright 2013 by MAU787
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid