Results 1 to 7 of 7
  1. #1
    proud2bhaole is offline Member
    Join Date
    May 2008
    Posts
    3
    Rep Power
    0

    Talking Applet to HTML Driving me bonkers

    Hi, I was wondering if you guys could help me with the following problem:

    I have an applet that I know runs in the Java Applet Viewer. The applet is a find and replace utility for a specific HTML file I used to have to manually update at work.

    I want to have it run via browser. Currently I have to open it up in Eclipse and run it as an applet.

    I cannot get it to work. I have tried loads of things:

    - downloaded SDK
    - tried HTMLconverter
    - tried compiling it thru eclipse. This gave me a single class file
    - tried compiling it thru cmd line (using javac.exe utility in SDK). This produced 5 separate class files
    - tried putting it in a .jar file and updating the main-class document
    - tried like a billion different ways of using the applet tag

    I finally am sending out my call for help. All I keep getting is a box with a red "x" where the applet is supposed to be (in the browser). I have been able to run other people's applets so i know it's possible.

    Thank you very much - any help is greatly appreciated!

    Here's the HTML I used:
    <html>
    <body>

    <applet
    code="WebStatConverter.class"
    width="500"
    height="500">
    </applet>

    </body>
    </html>

    And here's my Java source code:


    //Import Necessary Java Classes

    import javax.swing.*;
    import java.applet.*;
    import java.awt.*;
    import java.awt.event.*;

    import java.io.*;
    import java.util.Scanner;


    //Main Clas and Init() method
    public class WebStatConverter extends JApplet implements ActionListener{

    //Graphic Display elements
    Button okButton;
    Button openBrowse;
    Button saveBrowse;

    TextField openBrowsePath;
    TextField saveBrowsePath;
    TextField monthField;
    TextField yearField;

    //Class variables
    Convert converter = new Convert();
    LinkedList<String> list;
    JFileChooser browse = new JFileChooser();

    //User Entry Variables
    String openPath;
    String savePath;
    String month;
    String year;

    public void init(){
    setLayout(null);

    okButton = new Button("CONVERT!");
    openBrowse = new Button("OPEN");
    saveBrowse = new Button("SAVE");

    openBrowsePath = new TextField(openPath, 300);
    saveBrowsePath = new TextField(savePath, 300);
    monthField = new TextField(null,100);
    yearField = new TextField(null,100);

    openBrowse.setBounds(50, 50, 100, 30);
    saveBrowse.setBounds(50, 100, 100, 30);
    openBrowsePath.setBounds(200, 50, 300, 30);
    saveBrowsePath.setBounds(200, 100, 300, 30);

    monthField.setBounds(50, 200, 100, 30);
    yearField.setBounds(50, 300, 100, 30);
    okButton.setBounds(50, 350, 100, 30);


    add(okButton);
    add(openBrowse);
    add(saveBrowse);
    add(openBrowsePath);
    add(saveBrowsePath);
    add(monthField);
    add(yearField);

    okButton.addActionListener(this);
    openBrowse.addActionListener(this);
    saveBrowse.addActionListener(this);
    }

    public void actionPerformed(ActionEvent event){

    //Sets path for file to open
    if (event.getSource() == openBrowse){
    int openVar = browse.showOpenDialog(browse);

    if (openVar == JFileChooser.APPROVE_OPTION){
    openPath = browse.getSelectedFile().getPath();
    openBrowsePath.setText(openPath);
    repaint();
    }
    }

    //Sets path for file to save to
    else if (event.getSource() == saveBrowse){
    int saveVar = browse.showSaveDialog(browse);

    if (saveVar == JFileChooser.APPROVE_OPTION){
    savePath = browse.getSelectedFile().getPath();
    saveBrowsePath.setText(savePath);
    repaint();
    }
    }

    //Sets it all in motion
    else if (event.getSource() == okButton){
    month = monthField.getText();
    year = yearField.getText();

    System.out.println(openPath + "," + savePath + "," + month + "," + year);

    if(openPath != null && savePath != null && month != null && year != null){
    //read in file
    list = Convert.readFile(openPath);

    //Updates CSS path
    Convert.changeOne(list);

    //Replaces Header and updates 'Analyzes Request...' line
    Convert.changeTwo(list);

    //Updates Month and Year
    Convert.changeThree(month, year, list);

    //save to file
    Convert.saveFile(list, savePath);

    JOptionPane.showMessageDialog(null, "Conversion Complete");

    }
    else{
    JOptionPane.showMessageDialog
    (null, "You must enter in a value for all four entries.");
    }

    }
    }

    //Graphical Elements
    public void paint(Graphics g){
    Font font = new Font ("Arial", Font.BOLD, 16);
    g.setFont(font);
    g.drawString("Web Server Statistics HTML converter", 50, 30);
    font = new Font("Arial", Font.PLAIN, 12);
    g.setFont(font);
    g.drawString("Enter MONTH", 50, 190);
    g.drawString("Enter YEAR", 50, 290);
    }

    public void stop(){
    }

    }

    /******************* CONVERT CLASS ********************
    ************************************************** *****/

    class Convert{

    public Convert(){
    super();
    }

    public static LinkedList<String> readFile(String fileName){
    LinkedList<String> list = new LinkedList<String>();

    try{

    File file = new File(fileName);
    Scanner scan = new Scanner(file);

    while(scan.hasNextLine()){
    list.add(scan.nextLine());
    }//Try Block

    }
    catch (FileNotFoundException e){
    JOptionPane.showMessageDialog(null,"Sorry. Cannot find file.");
    System.exit(0);
    }//Catch

    return list;
    }

    public static void saveFile(LinkedList<String> newList, String fileName){
    try{
    FileWriter out = new FileWriter(fileName);
    out.write(newList.toString());
    out.close();
    }
    catch (Exception e){
    JOptionPane.showMessageDialog
    (null,"Sorry. Could not save File '" + fileName +"'");
    System.exit(0);
    }
    }

    //Adds "/.." to the css stylesheet href
    public static void changeOne(LinkedList<String> oldList){
    Node<String> node = oldList.head;

    while(node != null){
    if (node.getValue().equalsIgnoreCase
    ("<link href=\"../../../supportfiles/servercss.css\" rel=\"stylesheet\">")){
    node.setValue("<link href=\"../../../../supportfiles/servercss.css\" rel=\"stylesheet\">");
    break;
    }
    else
    node = node.getNext();
    }
    }

    //Provides Header information and updates "Analyzed request..." line
    public static void changeTwo(LinkedList<String> oldList){
    String analyzedRequest = "Analyzed Requests";
    String top = "<h1><a name=\"Top\">";
    String matched = "";
    Node<String> node = oldList.head;
    Node<String> nodeTwo = oldList.head;
    int positionOne = 1;
    int positionTwo = 1;

    //Stores updated "Analyzed request.." line
    try{
    while(positionOne <= oldList.size){

    try{

    if (node.getValue().substring(0,17).equalsIgnoreCase( analyzedRequest)){
    matched = node.getValue();
    oldList.remove(positionOne + 1);
    oldList.remove(positionOne);
    break;
    }
    else
    node = node.getNext();
    positionOne++;
    }
    catch(StringIndexOutOfBoundsException e){
    positionOne++;
    node = node.getNext();
    }

    }//End While Loop
    }//End Try Block

    catch (BadItemCountException e){
    System.out.println("No such item.");
    System.exit(0);
    }

    try{
    while(positionTwo <= oldList.size){
    try{
    if (nodeTwo.getValue().substring(0,18).equalsIgnoreCa se(top)){
    nodeTwo.setValue("<a NAME=\"Top\"> \n" +
    "<TABLE width=\"100%\" cellspacing=0 cellpadding=10 bgcolor=\"#b6da92\">\n" +
    "<tr><td><IMG src=\"../../images/header.gif\" alt=\"\"></td></tr>\n" +
    "<tr>\n<td>\n" + matched + "\n</td> \n</tr> \n</table>");
    oldList.remove(positionTwo + 2);
    break;
    }
    else
    nodeTwo = nodeTwo.getNext();
    positionTwo++;
    }//End try block
    catch (StringIndexOutOfBoundsException e){
    positionTwo++;
    nodeTwo = nodeTwo.getNext();
    }
    }//End while loop
    }
    catch (BadItemCountException e){
    System.out.println("No such item.");
    System.exit(0);
    }//End catch block
    }//End Method

    //Updates the Month and Year of the Report
    public static void changeThree(String month, String year, LinkedList<String> oldList){
    String menu = "<div align=right><span class=breadcrumb>";
    Node<String> node = oldList.head;
    int positionOne = 1;

    //Stores updated "Analyzed request.." line
    try{
    while(positionOne <= oldList.size){

    try{

    if (node.getValue().substring(0,40).equalsIgnoreCase( menu)){
    node.setValue
    ("<div align=right><span class=breadcrumb><a href=\"../../../../index.htm\">" +
    "UHM Main Page</a>&nbsp;|&nbsp; <a href=\"../server_"+ year + ".html\">" +
    "Web Statistics 2008</a>&nbsp;|&nbsp;" + month + " " + year + "</span></div>" +
    "\n<p class=month align=center>" + month + " " + year + "</p>");
    oldList.remove(positionOne + 2);
    break;
    }
    else
    node = node.getNext();
    positionOne++;
    }
    catch(StringIndexOutOfBoundsException e){
    positionOne++;
    node = node.getNext();
    }//End Catch Block

    }//End While Loop
    }//End Try Block

    catch (BadItemCountException e){
    System.out.println("No such item.");
    System.exit(0);
    }
    }
    }


    /********************Linked List and Node Classes*******************
    ************************************************** *****************/

    class LinkedList<T> {
    protected Node<T> head;
    protected int size;

    public LinkedList() {
    head = null; // start with an empty list
    size = 0;
    }

    /* accessor method */
    public int size() {
    return size;
    }

    public void add(T value) {
    head = addAtEnd(head, value);
    size++;
    }

    private Node<T> addAtEnd(Node<T> node, T value) {
    if (node == null) { // special case
    return new Node<T>(value, null);
    }

    else if (node.getNext() == null) { // other special case
    node.setNext(new Node<T>(value, null));
    }

    else {
    addAtEnd(node.getNext(), value);
    }

    return node;
    }

    /* @param position of item to be removed
    * @throws BadItemCountException if this is not a valid position
    * position is 1-based, so position = 1 removes the head
    */
    public void remove(int position) throws BadItemCountException {
    if ((position < 1) || (position > size)) {
    throw new BadItemCountException("invalid position " + position +
    ", only 1.." + size + " available");
    }

    if (position == 1) {
    head = head.getNext();
    }

    else {
    Node<T> node = head;
    for (int i = 2; i < position; i++) {
    node = node.getNext();
    }
    // set this node's "next" pointer to refer to the
    // node that is after the next

    node.setNext(node.getNext().getNext());
    }
    size--; // one less item
    }

    /* convert the list to a printable string
    * @return a string representing the stack
    */
    public String toString() {
    return toString(head);
    }

    private String toString(Node<T> node) {
    if (node == null) {
    return "";
    }

    else {
    return node.getValue() + "\n" + toString(node.getNext());
    }
    }
    }

    class BadItemCountException extends Exception{
    public BadItemCountException(String message){
    super(message);
    }
    }

    class Node<T> {
    private T item;
    private Node<T> nextNode;

    public Node(T value, Node<T> next) {
    item = value;
    nextNode = next;
    }

    /* accessor methods */
    public T getValue() {
    return item;
    }
    public Node<T> getNext() {
    return nextNode;
    }

    /* mutator methods */
    public void setNext(Node<T> newNext) {
    nextNode = newNext;
    }

    public void setValue(T value) {
    item = value;
    }
    }

  2. #2
    orchid's Avatar
    orchid is offline Member
    Join Date
    Apr 2007
    Location
    Midwest
    Posts
    60
    Rep Power
    0

    Default

    There is a security issue when I try to run it. Applets have restrictions on reading/writing local files so as not to considered mallicious. You probably need to deal with this via a signed applet or some such thing.

    java.security.AccessControlException: access denied (java.util.PropertyPermission user.dir read)
    at java.security.AccessControlContext.checkPermission (Unknown Source)
    at java.security.AccessController.checkPermission(Unk nown Source)
    at java.lang.SecurityManager.checkPermission(Unknown Source)
    at java.lang.SecurityManager.checkPropertyAccess(Unkn own Source)
    at java.lang.System.getProperty(Unknown Source)
    at java.io.Win32FileSystem.getUserPath(Unknown Source)
    at java.io.Win32FileSystem.resolve(Unknown Source)
    at java.io.File.getAbsolutePath(Unknown Source)
    at javax.swing.filechooser.WindowsFileSystemView.isFl oppyDrive(Unknown Source)
    at javax.swing.plaf.basic.BasicFileChooserUI$BasicFil eView.getIcon(Unknown Source)
    at javax.swing.JFileChooser.getIcon(Unknown Source)
    at javax.swing.plaf.metal.MetalFileChooserUI$Director yComboBoxRenderer.getListCellRendererComponent(Unk nown Source)
    at javax.swing.plaf.basic.BasicListUI.updateLayoutSta te(Unknown Source)
    at javax.swing.plaf.basic.BasicListUI.maybeUpdateLayo utState(Unknown Source)
    at javax.swing.plaf.basic.BasicListUI$Handler.valueCh anged(Unknown Source)
    at javax.swing.DefaultListSelectionModel.fireValueCha nged(Unknown Source)
    at javax.swing.DefaultListSelectionModel.fireValueCha nged(Unknown Source)
    at javax.swing.DefaultListSelectionModel.fireValueCha nged(Unknown Source)
    at javax.swing.DefaultListSelectionModel.changeSelect ion(Unknown Source)
    at javax.swing.DefaultListSelectionModel.changeSelect ion(Unknown Source)
    at javax.swing.DefaultListSelectionModel.setSelection Interval(Unknown Source)
    at javax.swing.JList.setSelectedIndex(Unknown Source)

  3. #3
    proud2bhaole is offline Member
    Join Date
    May 2008
    Posts
    3
    Rep Power
    0

    Default Applet Manifest file

    Hi

    I looked on the web on how to digitally sign my applet. I read that i need to put it in a jar file and sign the jar file. then ref it in the archive tag in the html code.

    I've created a keystore. The problem is with the .jar

    I've created the jar - but when i try to run it i get a "failed to load Main-class Manifest attribute from <jar location>"

    i probably need to put the main class location in the manifest file - but my applet has no main class? How do i get around this - or more importantly - do i need to get around this?

    Thanks for all the help,
    Kevin

  4. #4
    orchid's Avatar
    orchid is offline Member
    Join Date
    Apr 2007
    Location
    Midwest
    Posts
    60
    Rep Power
    0

    Default

    Read this:
    Understanding the Manifest
    it should help. It looks like the name of your applet class is what you put in the manifest

  5. #5
    vglass is offline Member
    Join Date
    Jul 2007
    Posts
    14
    Rep Power
    0

    Default

    To get the applet to load in your browser you don't need to specify anything in the manifest. Instead the browser looks at the "code" argument supplied via HTML applet parameter.

    e.g.

    param name="code" value="com.mydomain.MyClass.class"

    This is the way the browser knows what class to load.

  6. #6
    proud2bhaole is offline Member
    Join Date
    May 2008
    Posts
    3
    Rep Power
    0

    Default HTML code

    THanks for the replies guys!

    So i just jar the file and even though it can't run when i click on it - that's okay? I assume i still have to tie the keyStore to the jar right?

    So if i understand it correctly - this is what my html file would look like:
    param name="code" value="com.mydomain.MyClass.class"


    <body>

    <applet
    param name = "code"
    value = "WebStatConverter.class" (if i just wanna run it in a browser on the local machine)

    or

    value = "ww.myURL.com.WebStatConverter.class" (if i wanna run it from the root of my folder on the webserver)
    width="500"
    height="500">
    </applet>

    </body>

  7. #7
    dubdeeze3434 is offline Member
    Join Date
    Apr 2010
    Posts
    1
    Rep Power
    0

    Default

    how do i read in this file using a scanner class:
    <fruit>
    <type> Apple </type>
    <color> Green </color>
    </fruit>
    <fruit>
    <color> Yellow </color>
    <type> Banana </type>
    </fruit>

    its off topic but i really could use some help

Similar Threads

  1. Chat Applet in HTML
    By Flynazn in forum Java Applets
    Replies: 3
    Last Post: 05-27-2008, 10:26 AM
  2. Replies: 0
    Last Post: 03-10-2008, 03:36 PM
  3. How to view applet from html page.
    By jwzumwalt in forum Java Applets
    Replies: 2
    Last Post: 11-24-2007, 05:21 AM
  4. How to pass an html variable as an applet value
    By fred in forum Java Applets
    Replies: 1
    Last Post: 08-06-2007, 04:28 AM
  5. Create a Applet in the page HTML
    By Daniel in forum Java Applets
    Replies: 2
    Last Post: 07-04-2007, 08:52 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •