Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 05-26-2008, 10:35 PM
Member
 
Join Date: May 2008
Posts: 3
proud2bhaole is on a distinguished road
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;
}
}
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 05-27-2008, 12:35 AM
orchid's Avatar
Member
 
Join Date: Apr 2007
Location: Midwest
Posts: 60
orchid is on a distinguished road
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)
__________________

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 05-30-2008, 09:39 PM
Member
 
Join Date: May 2008
Posts: 3
proud2bhaole is on a distinguished road
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
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 05-30-2008, 10:51 PM
orchid's Avatar
Member
 
Join Date: Apr 2007
Location: Midwest
Posts: 60
orchid is on a distinguished road
Read this:
Understanding the Manifest
it should help. It looks like the name of your applet class is what you put in the manifest
__________________

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 05-30-2008, 11:45 PM
Member
 
Join Date: Jul 2007
Posts: 14
vglass is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 05-31-2008, 01:19 AM
Member
 
Join Date: May 2008
Posts: 3
proud2bhaole is on a distinguished road
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>
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Chat Applet in HTML Flynazn Java Applets 4 05-27-2008 10:26 AM
Applet - Displaying an HTML page with a selected resolution Java Tip Java Tips 0 03-10-2008 03:36 PM
How to view applet from html page. jwzumwalt Java Applets 2 11-24-2007 05:21 AM
How to pass an html variable as an applet value fred Java Applets 1 08-06-2007 04:28 AM
Create a Applet in the page HTML Daniel Java Applets 2 07-04-2007 08:52 AM


All times are GMT +3. The time now is 06:20 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org