Top Banner
NetBeans IDE 5.0 Tutorial for Applets Fang Chu Kamolbhan Olapiriyakul
69

NetBeans IDE 5.0 Tutorial for Applets Fang Chu Kamolbhan Olapiriyakul.

Jan 15, 2016

Download

Documents

Audra Johnson
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: NetBeans IDE 5.0 Tutorial for Applets Fang Chu Kamolbhan Olapiriyakul.

NetBeans IDE 5.0Tutorial for Applets

Fang ChuKamolbhan Olapiriyakul

Page 2: NetBeans IDE 5.0 Tutorial for Applets Fang Chu Kamolbhan Olapiriyakul.

Deployment Options• There are at least three ways to put a NetBeans

created applet on the NJIT AFS system.• The first method is to create and test source code in

NetBeans, but then transfer it to AFS, compile it there, and set it up manually.

• The second method is to create a JAR file in NetBeans, copy it to AFS, and configure it there with Netbeans.

• The third method is to package a stand-alone JAR package in NetBeans and deploy it to AFS.

Page 3: NetBeans IDE 5.0 Tutorial for Applets Fang Chu Kamolbhan Olapiriyakul.

Contents of this presentation

• The detailed presentation, with code, uses the third option and is discussed last.

• The other two options are described briefly, along with explanatory material on Applets, AFS, and NetBeans, as an introduction to the final topic.

Page 4: NetBeans IDE 5.0 Tutorial for Applets Fang Chu Kamolbhan Olapiriyakul.

Setting Up Java Applets on AFSManually

Fang Chu

Page 5: NetBeans IDE 5.0 Tutorial for Applets Fang Chu Kamolbhan Olapiriyakul.

AFS and Java Applets• To create a homepage on AFS for Java Class, see

the instructions athttp://web.njit.edu • Your web pages will have an address like thishttp://web.njit.edu/~gblank• Use FTP software or AFS Clients to upload your

web pages to your directoryhttp://csd.njit.edu/softwarehttp://www.coreftp.com/download.html

Page 6: NetBeans IDE 5.0 Tutorial for Applets Fang Chu Kamolbhan Olapiriyakul.

AFS and Java Applets

• To run a Java applet on the web, you embed code similar to the following in an HTML file and then upload the file as a webpage.

• Applet Container Code Sample:<applet code=“ReadFile.class”

width=300 height=350>

</applet>

Page 7: NetBeans IDE 5.0 Tutorial for Applets Fang Chu Kamolbhan Olapiriyakul.

Running Java Applets on AFS

• Log on to AFS (afs1-afs32)• Change to your java file directory• Compile the java file • Make sure that java file, java class file,

text file and html applet container are in the same directory

• Link to the html applet container online to enable the java applet

Page 8: NetBeans IDE 5.0 Tutorial for Applets Fang Chu Kamolbhan Olapiriyakul.

Log on to AFS Click on

“Start” at bottom right

Click on “Run”

next to bottom

Page 9: NetBeans IDE 5.0 Tutorial for Applets Fang Chu Kamolbhan Olapiriyakul.

Log on AFS

Type in UCID and

Pass word

Page 10: NetBeans IDE 5.0 Tutorial for Applets Fang Chu Kamolbhan Olapiriyakul.

Change Directory

Current Directory

Page 11: NetBeans IDE 5.0 Tutorial for Applets Fang Chu Kamolbhan Olapiriyakul.

Change Directory

Change directory to the folder containing

“ReadFile.java”Using command

“cd”

Page 12: NetBeans IDE 5.0 Tutorial for Applets Fang Chu Kamolbhan Olapiriyakul.

Compile Java file

Compile java

fileUsing

command

“javac”No warning error

info“ReadF

ile” generat

ed!

Page 13: NetBeans IDE 5.0 Tutorial for Applets Fang Chu Kamolbhan Olapiriyakul.

Run the Java Applet

Page 14: NetBeans IDE 5.0 Tutorial for Applets Fang Chu Kamolbhan Olapiriyakul.

NetBeans Essentialsfor Java Applets

• Create or import Java applet codes.• Debug, compile and run codes.• Embed codes into web applications.

Page 15: NetBeans IDE 5.0 Tutorial for Applets Fang Chu Kamolbhan Olapiriyakul.

Create/ Import Code Step1.Create a new project or

open an existing project

Step 2. Create a new java applet or open an existing

file.

Page 16: NetBeans IDE 5.0 Tutorial for Applets Fang Chu Kamolbhan Olapiriyakul.

Create a new project

Select General>>Application>>

Next>>…

Page 17: NetBeans IDE 5.0 Tutorial for Applets Fang Chu Kamolbhan Olapiriyakul.

Name the new project

After naming the project and

selecting its directoryClick on “finish”

Page 18: NetBeans IDE 5.0 Tutorial for Applets Fang Chu Kamolbhan Olapiriyakul.

Create a new applet file

Right-click on Source-package or

new project name>>Right-click on

“new”>>Left-click on

“Applet”; also name the package where the applet file and text file are saved.

Page 19: NetBeans IDE 5.0 Tutorial for Applets Fang Chu Kamolbhan Olapiriyakul.

Compile an applet

Right-click on the applet file and thenleft-click on“compile”to compile the java applet

Page 20: NetBeans IDE 5.0 Tutorial for Applets Fang Chu Kamolbhan Olapiriyakul.

Compiling Message

No Error,Can be built and executed

Page 21: NetBeans IDE 5.0 Tutorial for Applets Fang Chu Kamolbhan Olapiriyakul.

Run the Java applet

• Include data.txt file in the same folder with java code.

• “Build the project” by right-clicking on the project name node and then left-clicking it.

• “Run the file”, similar to “compile”.• If applet program is correct, text will be

displayed in the applet viewer. Otherwise, it may deliver a message in the bottom without initializing the applet.

Page 22: NetBeans IDE 5.0 Tutorial for Applets Fang Chu Kamolbhan Olapiriyakul.

Applet ViewerAfter the project

is built and executed, the text will be shown in

the applet viewer.

Page 23: NetBeans IDE 5.0 Tutorial for Applets Fang Chu Kamolbhan Olapiriyakul.

Web Applications

• Create a web project– Choose File>New Project. Under Categories, select

“Web”; under project, select “Web Application”; “next”

– Under Project Name, type in a given name and then redirect the directory to the right folder; “finish”.

Page 24: NetBeans IDE 5.0 Tutorial for Applets Fang Chu Kamolbhan Olapiriyakul.

Deploying JAR files on AFS

Fang Chu

Page 25: NetBeans IDE 5.0 Tutorial for Applets Fang Chu Kamolbhan Olapiriyakul.

Web Application

• When you want to include an applet JAR file in a web project, you can do so by adding the NetBeans IDE 5.x Java project that contains the JAR file, or by adding the JAR file itself.

• Note: if you adding the Java project to web project, you enable the IDE to build the applet whenever you build the web application. Therefore, when you modify the applet, the IDE builds a new version of the applet whenever the web project is built. On the other hand, if the applet JAR file is not in a NetBeans IDE 5.0 Java project, the applet source file is not rebuilt when you build the web project.

Page 26: NetBeans IDE 5.0 Tutorial for Applets Fang Chu Kamolbhan Olapiriyakul.

Web ApplicationsStep1: In the Projects window, right-click

the project node and select Properties

from the contextual menu.

If the applet JAR file is in a NetBeans IDE 5.x Java project, click Packaging, and then click Add Project. Browse to and select the folder that contains the NetBeans IDE 5.x Java project. Note that NetBeans IDE 5.x projects are marked by the NetBeans IDE project icon. If the applet JAR file is not in a NetBeans IDE 5.x project, click Packaging, and then click Add JAR/Folder. Browse to and select the folder that contains the JAR file.

Page 27: NetBeans IDE 5.0 Tutorial for Applets Fang Chu Kamolbhan Olapiriyakul.

Web Applications

• To embed the applet in an HTML file, right-click the project node, and choose New > File/Folder from the contextual menu. Under Categories, select Web. Under File Types, select HTML. Click Next. Give your HTML file a name and click Finish.

• Embed the following tags anywhere between the file’s <body></body>tags.

<applet code=“FileRead.ReadFile” archive=“ReadFile.jar”> </applet>– FileRead.ReadFile is the full class name– ReadFile.jar is the JAR File that contains applet

Page 28: NetBeans IDE 5.0 Tutorial for Applets Fang Chu Kamolbhan Olapiriyakul.

Deploying Web Applications

• Right-click the HTML node in the Projects window and choose Run File from the contextual menu.

• The server deploys the HTML file in the IDE's default browser.

Page 29: NetBeans IDE 5.0 Tutorial for Applets Fang Chu Kamolbhan Olapiriyakul.

Success

Similar page will be displayed if

web applications run successfully.

Page 30: NetBeans IDE 5.0 Tutorial for Applets Fang Chu Kamolbhan Olapiriyakul.

NetBeans IDE 5.0Applet Deployment on AFS

Kamolbhan Olapiriyakul

Page 31: NetBeans IDE 5.0 Tutorial for Applets Fang Chu Kamolbhan Olapiriyakul.

Tutorial Outline

• Creating or importing an applet source file– Creating a java project, create or import an applet

source file

• Running and debugging an applet source file– Run and applet source file, debug it by modifying its

parameters in the src folder

• Embedding an applet in a web application– Create a web project, import NetBeans IDE 5.0 Java

project or a JAR file containing applet source files, add applet tags to a JSP file or HTML file, run the web project

Page 32: NetBeans IDE 5.0 Tutorial for Applets Fang Chu Kamolbhan Olapiriyakul.

Creating the Java project from scratch or from existing sources

• Java project (file.java) can be created, compile, run and debugged in NetBeans environment

• A java project can contain the following:– Java package– Java class and interface– Java applet– Html file– And more…

Page 33: NetBeans IDE 5.0 Tutorial for Applets Fang Chu Kamolbhan Olapiriyakul.

Creating the Java project from scratch or from existing sources

• On the NetBeans main menu, choose File > New Project. Under categories, select General. Then, click Next.

• Choose one of the following:– If you are creating a new applet, select Java Class

Library under Projects.– If you are importing an applet source, select Java Project

with Existing Source and specify the file’s location in the Source Packages Folder

Page 34: NetBeans IDE 5.0 Tutorial for Applets Fang Chu Kamolbhan Olapiriyakul.

• Under Project Name, type project name. Change the project location if you want.

• Click Finish. If you imported an applet source file, run and debug it.

Creating the Java project from scratch or from existing sources

Page 35: NetBeans IDE 5.0 Tutorial for Applets Fang Chu Kamolbhan Olapiriyakul.

Creating the applet source file

• Right-click to the project node in the Projects window and select New File/Folder

• Under Categories, select Java Classes. Under File Types, select Applet. Then click Next.

• Under Class Name, type file name. Under Package, type Package name.– For example:

File name ReadFilePackage FileRead

Page 36: NetBeans IDE 5.0 Tutorial for Applets Fang Chu Kamolbhan Olapiriyakul.

Sample source code: ReadFile Applet

• The ReadFile program reads a data text file from an applet

• the URL class is used for accessing web resources• In this case, it also provides a method to obtain

file access through an InputStream• A BufferedReader provides buffering to smooth out

the stream flow and to get the readLine() method for grabbing a whole line at once and returning it in a string

Page 37: NetBeans IDE 5.0 Tutorial for Applets Fang Chu Kamolbhan Olapiriyakul.

Sample source code: ReadFile Applet

package FileRead;Import java.applet.*;...public class ReadFile extends java.applet.Applet {

StringBuffer buffer; String filename="data.txt"; TextArea td;

... }

Page 38: NetBeans IDE 5.0 Tutorial for Applets Fang Chu Kamolbhan Olapiriyakul.

Sample source code: ReadFile Applet

public void init() {

td = new TextArea(60, 20);

td.setEditable(false);

setLayout(new BorderLayout());

add(td, "Center");

//read the file

readFile();

}

Page 39: NetBeans IDE 5.0 Tutorial for Applets Fang Chu Kamolbhan Olapiriyakul.

Sample source code: ReadFile Applet

public void readFile(){

String line;

URL url = null;

try {

url = new URL (getCodeBase(), filename);

}

catch (MalformedURLException e ) {

System.out.println("Malformed URL ");

stop();

}

Page 40: NetBeans IDE 5.0 Tutorial for Applets Fang Chu Kamolbhan Olapiriyakul.

Sample source code: ReadFile Applet

try { InputStream in = url.openStream(); BufferedReader bufin = new BufferedReader(new

InputStreamReader(in)); buffer = new StringBuffer () ; while ((line = bufin.readLine()) != null){ buffer.append(line + "\n"); } in.close(); }catch (IOException e ) {} tdisplay.append(buffer.toString ()); }

Page 41: NetBeans IDE 5.0 Tutorial for Applets Fang Chu Kamolbhan Olapiriyakul.

Build and run applet source file

• After the applet source file has been created, you can build and run the applet source file on NetBeans IDE

• After the project is built and the applet is complied and run, an applet viewer is shown in small window frame

• If the applet viewer is not successfully loaded, this means that there is an error in your code

Page 42: NetBeans IDE 5.0 Tutorial for Applets Fang Chu Kamolbhan Olapiriyakul.

Build and run applet source file

• Right-click the project node and choose Build Project from the contextual menu

• The ReadFile.jar file is created in the dist folder

• Right-click the applet class node and choose Run File from the menu

Page 43: NetBeans IDE 5.0 Tutorial for Applets Fang Chu Kamolbhan Olapiriyakul.

• The ReadFile.html launcher file, with the applet embedded, is created in the build folder

• The applet is launched in the Applet viewer as shown in the left figure

Build and run applet source file

Page 44: NetBeans IDE 5.0 Tutorial for Applets Fang Chu Kamolbhan Olapiriyakul.

Debug the applet source file by modifying its parameters

• The ReadFile.html launcher file in build folder is overwritten each time when run or debug the applet

• Therefore, do not modify the launcher file in build folder

Page 45: NetBeans IDE 5.0 Tutorial for Applets Fang Chu Kamolbhan Olapiriyakul.

Debug the applet source file by modifying its parameters

• Instead, use the following procedures:– Open the Files window– Copy the ReadFile.html from build folder to the

package where the applet class lives in the src folder. Make sure that ReadFile.html launcher file has the same name as the applet class

– Edit the ReadFile.html launcher file as needed

When the project is built, the ReadFile.html launcher file is copied from the src folder to the build folder

Page 46: NetBeans IDE 5.0 Tutorial for Applets Fang Chu Kamolbhan Olapiriyakul.

Create the applet-embedded Web Application

• You can create a Web page that contains HTML source code and Java files by using the NetBeans IDE

• Java applets can be attached on the HTML source file

• The HTML page can be view on the default Web browser or the Web browser of your choice– To set up the default Web browser, go to Option on the

menu

Page 47: NetBeans IDE 5.0 Tutorial for Applets Fang Chu Kamolbhan Olapiriyakul.

Create the applet-embedded Web Application

– In the General tab, you will see the Web Browser drop-down box in the General section

– Choose the Web browser– Note: Java applet works well on Mozilla Firefox, instead

of Internet Explorer

Page 48: NetBeans IDE 5.0 Tutorial for Applets Fang Chu Kamolbhan Olapiriyakul.

Create the applet-embedded Web Application

• On the NetBeans main menu, choose File > New Project. Under categories, select Web. Under Projects, select Web Application. Then, click Next.

• Under Project Name, type project name. Change the Project Location to any folder you need. Then click Finish.

Page 49: NetBeans IDE 5.0 Tutorial for Applets Fang Chu Kamolbhan Olapiriyakul.

Add the applet JAR file to the web project

• You can either add the NetBeans IDE Java project that contains the JAR files, or add the JAR file itself– Note: if you add the Java project to web project, you

enable the IDE to build the applet whenever you build the web application. Therefore, when you modify the applet, the IDE builds a new version of the applet whenever the web project is built. On the other hand, if the applet JAR file is not in a NetBeans IDE 5.0 Java project, the applet source file is not rebuilt when you build the web project.

Page 50: NetBeans IDE 5.0 Tutorial for Applets Fang Chu Kamolbhan Olapiriyakul.

Add the applet JAR file to the web project

• In the Projects window, right-click the project node and select Properties from the contextual menu

• Choose one of the following:– If the applet JAR file is in a NetBeans, click

Packaging, and then Add project. Browse to and select the folder that contains java project.

– If the applet JAR file is not in a NetBeans, click Packaging, and then click Add JAR/Folder. Browse to and select the folder that contains JAR file.

Page 51: NetBeans IDE 5.0 Tutorial for Applets Fang Chu Kamolbhan Olapiriyakul.

Add the applet JAR file to the web project

• Optionally, you can type a location for the applet in the table’s Path in WAR column. By default, the applet JAR file will be copied to build/web folder. Click OK– Note: When you build the project, the applet’s JAR file is

packaged in the project’s WAR file in the dist folder and also in build/web folder.

Page 52: NetBeans IDE 5.0 Tutorial for Applets Fang Chu Kamolbhan Olapiriyakul.

Create and run the JSP file or HTML file

• Choose one of the following:– Embed the applet in a JSP file,

double-click default index.jsp file in the Projects window. This file is created by the IDE.

– Embed the applet in a HTML file, right-click the project node, and choose New > File/Folder from the menu. Under Categories, select Web > HTML. Click Next and Finish.

Page 53: NetBeans IDE 5.0 Tutorial for Applets Fang Chu Kamolbhan Olapiriyakul.

• Embed the applet in the file by adding the following applet tag anywhere between the file’s <body></body> tags:<applet code=“FileRead.ReadFile” archive=“ReadFile.jar”> </applet>– FileRead.ReadFile is the full class name– ReadFile.jar is the JAR File that contains applet

• Right-click the JSP or HTML node in the Projects window and choose Run File from the menu

Create and run the JSP file or HTML file

Page 54: NetBeans IDE 5.0 Tutorial for Applets Fang Chu Kamolbhan Olapiriyakul.

Output

Page 55: NetBeans IDE 5.0 Tutorial for Applets Fang Chu Kamolbhan Olapiriyakul.

Deploy the NetBeans Java Project on AFS

Kamolbhan Olapiriyakul

Page 56: NetBeans IDE 5.0 Tutorial for Applets Fang Chu Kamolbhan Olapiriyakul.

Create the links to Java projects on AFS

• Create a Webpage for course• The course main page should be named as

index.html– For example: CS602/index.html

• Creates the underlying Web pages that link with the course main page– Sample code for the link command is:

<a href="/~ab12/CS602/index.html">

Page 57: NetBeans IDE 5.0 Tutorial for Applets Fang Chu Kamolbhan Olapiriyakul.

Transfer Java project and files to AFS Clients

• To transfer Java projects to AFS Clients, you need to have File Transfer Protocol (FTP) or AFS Clients software. The FTP and AFS Clients allows you to transfer or exchange files between your computer and AFS servers.– You can download AFS Clients at

http://csd.njit.edu/software– See also the installation and setup instruction at

http://web.njit.edu/all_topics/AFS/windows.client/– You can also download the Core FTP at

http://www.coreftp.com/download.html

Page 58: NetBeans IDE 5.0 Tutorial for Applets Fang Chu Kamolbhan Olapiriyakul.

Transfer file to AFS Clients

– The path for your AFS personal space should be \afs\cad\u\first_letter_UCID\second_letter_UCID\UCID

– For example, the personal space for UCID “ab12” can be found at afs\cad\u\a\b\ab12

• Transfer the project folder from your computer to your AFS space. – For example, the ReadFile project are transferred to

personal homepage folder on AFS or public_html folder

Page 59: NetBeans IDE 5.0 Tutorial for Applets Fang Chu Kamolbhan Olapiriyakul.

Transfer file to AFS Clients

– Create sub-folder if necessary. For instance, sub-folder CS602 is created to store project files

– The path of project, for instance, is http://web.njit.edu/~ab12/CS602/ReadFile/

Page 60: NetBeans IDE 5.0 Tutorial for Applets Fang Chu Kamolbhan Olapiriyakul.

Trouble Shooting Section

Fang ChuKamolbhan Olapiriyakul

Also see the separate presentation on “Applet Failures”

Page 61: NetBeans IDE 5.0 Tutorial for Applets Fang Chu Kamolbhan Olapiriyakul.

Frequently Asked Questions

• There is an “x” in the applet viewer. Was it initialized?—Both applet structure and code needs to be double checked.

• The applet runs in NetBeans, but it is not working with AFS.—Check links to see if it is directed to the folder where source and class files are stored. Or transfer codes from NetBeans to server account to compile java file via AFS to see if it is working.

Page 62: NetBeans IDE 5.0 Tutorial for Applets Fang Chu Kamolbhan Olapiriyakul.

FAQ• Applet works with Firefox, but not IE.– IE may

require a manifest file with a blank line for a JAR file. See details in the tutorial on JAR files. A Java applet may not work in a browser due to browser configuration, security settings, lack of the Java Runtime Edition, or another incompatibility. Netbeans allows you to specify a browser. Try both and compare the code, especially the deployment descriptor and HTML code.

Page 63: NetBeans IDE 5.0 Tutorial for Applets Fang Chu Kamolbhan Olapiriyakul.

FAQ

• Cannot embed applet in the HTML file– Check the code between <applet> </applet> tag– Different IDE may have slightly different

standard of code for applet command

• Input is accepted in NetBeans but not AFS.– Check the I/O program structure in the code to see if there is a missing request/accept module.

Page 64: NetBeans IDE 5.0 Tutorial for Applets Fang Chu Kamolbhan Olapiriyakul.

FAQ

• The following messages have been found:“The applet is not initialized”“Loading Java applet failed”– Try to run applet on NetBeans first– Check the path and syntax of the code

Page 65: NetBeans IDE 5.0 Tutorial for Applets Fang Chu Kamolbhan Olapiriyakul.

Reference

www.netbeans.org NetBeans quick-start tutorials, open source

downloads and FAQ sessions and a particular chapter highlighting applets.

java.sun.com/developer/onlineTraining/tools/netbeans_part1

Quick-start tutorials for NetBeans users and professional developers’ forum where many topics are around applets.

Page 67: NetBeans IDE 5.0 Tutorial for Applets Fang Chu Kamolbhan Olapiriyakul.

Reference

• http://docs.rinet.ru/J21/ch8.htm• http://math.hws.edu/javamath/ Comprehensive Java Applets samples

can be found along with detailed documentation, which can be practiced with NetBeans.

Page 68: NetBeans IDE 5.0 Tutorial for Applets Fang Chu Kamolbhan Olapiriyakul.

Reference

• NetBeans IDE 5.0 Quick start guide for J2EE Application

http://www.netbeans.org/kb/50/quickstart-j2ee.html

• NetBeans IDE 5.0 Software download. http://www.netbeans.org/kb/55/tutorial-webapps.html

• NetBeans tutorial for Applet http://www.netbeans.org/kb/50/tutorial-applets.html

Page 69: NetBeans IDE 5.0 Tutorial for Applets Fang Chu Kamolbhan Olapiriyakul.

Reference• NetBeans Web Application tutorial

http://www.netbeans.org/kb/55/tutorial-webapps.html

• AFS Window Client Softwarehttp://web.njit.edu/all_topics/AFS/windows.client/

• Core FTP Softwarehttp://www.coreftp.com/download.html

• ReadFile source code

http://www.particle.kth.se/~fmi/kurs/PhysicsSimulation/Lectures/12B/fileReadByApplet.html