Docstoc

NetBeans IDE 5.0 Tutorial for Ap

Document Sample
NetBeans IDE 5.0 Tutorial for Ap Powered By Docstoc
					        NetBeans IDE 5.0
       Tutorial for Applets
                   Fang Chu
        Kamolbhan Olapiriyakul


NJIT
    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.
2                         8/12/2010
    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.



3                      8/12/2010
   Setting Up Java Applets on AFS
                         Manually

                         Fang Chu




NJIT
    AFS and Java Applets
 To create a homepage on AFS for Java Class,
  see the instructions at
http://web.njit.edu
 Your web pages will have an address like this

http://web.njit.edu/~gblank
 Use FTP software or AFS Clients to upload your
  web pages to your directory
http://csd.njit.edu/software
http://www.coreftp.com/download.html
5                    8/12/2010
    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>




6                           8/12/2010
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

7                         8/12/2010
    Log on to AFS
                                   Click on
                                  “Start” at
                                 bottom right
                                   Click on
                                    “Run”
                                next to bottom




8                   8/12/2010
    Log on AFS




             Type in UCID
                  and
               Pass word




9                 8/12/2010
 Change Directory




              Current
             Directory




10            8/12/2010
 Change Directory
                           Change directory to
                          the folder containing
                            “ReadFile.java”
                            Using command
                                  “cd”




11            8/12/2010
 Compile Java file
                            Compile
                            java file
                             Using
                           command
                            “javac”

                           No warning
                            error info
                           “ReadFile”
                           generated!



12             8/12/2010
 Run the Java Applet




13            8/12/2010
 NetBeans Essentials
 for Java Applets
    Create or import Java applet codes.
    Debug, compile and run codes.
    Embed codes into web applications.




14                   8/12/2010
 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.




15            8/12/2010
 Create a new project


                          Select General>>
                           Application>>
                             Next>>…




16            8/12/2010
 Name the new project
                           After naming
                          the project and
                            selecting its
                             directory
                              Click on
                              “finish”




17            8/12/2010
 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.


18             8/12/2010
 Compile an applet

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




19            8/12/2010
 Compiling Message

                           No Error,
                         Can be built
                         and executed




20           8/12/2010
 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.


21                        8/12/2010
 Applet Viewer
                              After the project
                                is built and
                             executed, the text
                              will be shown in
                             the applet viewer.




22               8/12/2010
 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‖.




23                           8/12/2010
       Deploying JAR files on AFS
                          Fang Chu




NJIT
 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.


25                          8/12/2010
 Web Applications
            Step1: 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.

26              8/12/2010
 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



27                        8/12/2010
 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.




28                    8/12/2010
 Success
                       Similar page will
                        be displayed if
                       web applications
                       run successfully.




29         8/12/2010
               NetBeans IDE 5.0
       Applet Deployment on AFS
               Kamolbhan Olapiriyakul




NJIT
 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

31                           8/12/2010
 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…


32                          8/12/2010
 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


33                           8/12/2010
 Creating the Java project from
 scratch or from existing sources

                                 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.
34                8/12/2010
 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 ReadFile
         Package FileRead


35                       8/12/2010
 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

36                       8/12/2010
 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;
     ...
 }


37                 8/12/2010
 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();
     }



38                   8/12/2010
 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();
      }
39                    8/12/2010
 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 ());
  }

40                 8/12/2010
 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


41                          8/12/2010
 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


42                 8/12/2010
 Build and run applet source file
                            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

43               8/12/2010
 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




44                       8/12/2010
 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
45                           8/12/2010
 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

46                          8/12/2010
 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




47                          8/12/2010
 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.



48                      8/12/2010
 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.


49                           8/12/2010
 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.


50                          8/12/2010
 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.




51                            8/12/2010
 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.


52                            8/12/2010
 Create and run the JSP file or
 HTML file
    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

53                          8/12/2010
 Output




54        8/12/2010
       Deploy the NetBeans
       Java Project on AFS
          Kamolbhan Olapiriyakul




NJIT
 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">


56                          8/12/2010
 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

57                           8/12/2010
 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


58                           8/12/2010
 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/




59                        8/12/2010
       Trouble Shooting Section
                               Fang Chu
                Kamolbhan Olapiriyakul
                   Also see the separate
        presentation on ―Applet Failures‖
NJIT
 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.

61                      8/12/2010
 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.


62                         8/12/2010
 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.

63                        8/12/2010
 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




64                     8/12/2010
 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/netbe
   ans_part1
   Quick-start tutorials for NetBeans users and
   professional developers’ forum where many
   topics are around applets.


65                    8/12/2010
 Reference
    www.cs.armstrong.edu/liang/intro5e/Suppl
     ementINetBeans.pdf
    http://www.javapassion.com/netbeans/ma
     sterindex.html
     Comprehensive NetBeans introductions with
     emphasis on Java programming and many
     samples on Java applets.



66                     8/12/2010
 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.



67                  8/12/2010
 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

68                     8/12/2010
 Reference
    NetBeans Web Application tutorial
     http://www.netbeans.org/kb/55/tutorial-
     webapps.html
    AFS Window Client Software
     http://web.njit.edu/all_topics/AFS/windows.client/
    Core FTP Software
     http://www.coreftp.com/download.html
    ReadFile source code
     http://www.particle.kth.se/~fmi/kurs/PhysicsSimu
     lation/Lectures/12B/fileReadByApplet.html
69                       8/12/2010

				
DOCUMENT INFO