The Artima Developer Community
Sponsored Link

Java Answers Forum
applet to application

6 replies on 1 page. Most recent reply: Jul 29, 2002 6:09 AM by Joey

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 6 replies on 1 page
Joey

Posts: 12
Nickname: joey
Registered: Jul, 2002

applet to application Posted: Jul 24, 2002 2:13 AM
Reply to this message Reply
Advertisement
If I turn my japplet in an application, how can I make it run in a browser (it's used to write some stuff to a txt file on a server, from where the Japplet(application) runs. (I turned the applet into an application to avoid using signing.


Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: applet to application Posted: Jul 24, 2002 12:01 PM
Reply to this message Reply
Applications don't run in the browser, by definition. That is what makes it an application, not an applet.

If all you want to do is write to a text file on the server (from where the applet is running), an applet can do that. What an applet can't do (by default) is read and write files on the client (or some other server, etc.).

Joey

Posts: 12
Nickname: joey
Registered: Jul, 2002

Re: applet to application Posted: Jul 25, 2002 2:01 AM
Reply to this message Reply
Now I have a tiny error in my code...
this is a piece of my code


private void read(){
try{
url = new URL( getDocumentBase()+ "gegevens.txt" );
BufferedReader in = new BufferedReader(new FileReader(url));
while((lijn = in.readLine())!=null){
lijst.add(lijn);
i++;
}


in.close();



}
catch( MalformedURLException err ){
System.exit(0);
}
catch(IOException err){
JOptionPane.showMessageDialog(null,""+err,"fout",JOptionPane.ERROR_MESSAGE);
System.exit(0);

}
catch(NullPointerException err){
JOptionPane.showMessageDialog(null,""+err,"fout",JOptionPane.ERROR_MESSAGE);
System.exit(0);
}
}

it's meant to read a file from the server where the applet is running.

Jay Kandy

Posts: 77
Nickname: jay
Registered: Mar, 2002

Re: applet to application Posted: Jul 25, 2002 2:53 PM
Reply to this message Reply
Hey Joey, I donno what your code does - hard to read the way it is on the page. But, heres a simple applet that can read a text file from its originating host. Also, since its got a main(), it can be run in a JFrame (its container) by explicitly "calling the applets init()". If its run as an application, getCodeBase() returns nothing, so the properties file is assumed to be in the class path. Anyways, if you go through the code you'll see my point:
import java.net.URL;
import java.net.MalformedURLException;
import java.awt.Graphics;
import java.awt.Color;
import java.applet.Applet;
import java.io.InputStream;
import java.io.IOException;
import java.util.Properties;
import javax.swing.JApplet;
import javax.swing.JFrame;
 
public class ReadApplet extends JApplet
{
	public static final String PROPERTIES_FILE = "app.prp";
	public static final String NAME_KEY = "Name";
	public static final String GREETING_KEY = "Greeting";
 
	private Properties props;
	private StringBuffer buffer;
 
	public ReadApplet()
        {
	}
 
	public void explicitInit()
   	{
		try
		{
			InputStream is = getClass().getResourceAsStream(PROPERTIES_FILE);
			props = new Properties();
			buffer = new StringBuffer();
			props.load(is);
 
			repaint();
		}
		catch(MalformedURLException murle)
		{
			handleException(murle);
		}
		catch(IOException ioe)
		{
			handleException(ioe);
		}
	}
 
	public void init()
   	{
		try
		{
			URL filePath = new URL(getCodeBase() + PROPERTIES_FILE);
			props = new Properties();
			buffer = new StringBuffer();
			props.load(filePath.openStream());
 
			repaint();
		}
		catch(MalformedURLException murle)
		{
			handleException(murle);
		}
		catch(IOException ioe)
		{
			handleException(ioe);
		}
	}
 
	public void paint(Graphics g)
	{
		g.setColor(Color.black);
 
		buffer.append(props.getProperty(GREETING_KEY));
		buffer.append(", ");
		buffer.append(props.getProperty(NAME_KEY));
 
		g.drawString(buffer.toString(), 100, 10);
	}
 
	public void handleException(Exception e)
	{
		e.printStackTrace();
	}
 
	public static void main(String s[])
	{
		JFrame frame = new JFrame();
		ReadApplet applet = new ReadApplet();
		applet.explicitInit();
		frame.getContentPane().add(applet);
		frame.setSize(200, 200);
		frame.setVisible(true);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}
}


Create a file called app.prp. Its contents like:

Name=Kandy
Greeting=Hello


1. Compile as usual, and run with a .html in a browser
2. Compile and run at prompt: java ReadApplet

Joey

Posts: 12
Nickname: joey
Registered: Jul, 2002

Re: applet to application Posted: Jul 26, 2002 12:48 AM
Reply to this message Reply
Thnx!
What the method read does:
it reads a file, gegevens.txt, in a list until there are no more items in gegevens.txt
gegevens.txt:
Joey|Bergen|4412|joey@mailhost.be
Mat|Ball|5532|matt @mailhost.be
...

firstname|lastname|idnr|mailaddress

when someone gives in his mailadres he wil be unsubscribed from the list, the file gegevens.txt
what should I change in your code to read until the last line is read in the list. (I use the list to easily delete a line)

Thanx for your time!
With your code, now I learned to use properties. (I'm just a student IT)

Jay Kandy

Posts: 77
Nickname: jay
Registered: Mar, 2002

Re: applet to application Posted: Jul 26, 2002 12:17 PM
Reply to this message Reply
Glad you learnt! Heres how you read a file like that. I posted the whole code coz i dint want to confuse you with just one method. After this, if you like, I want you to download javadocs if you dont have already, and read about ObjectInputStream and ObjectOutputStream and see how that can be used with this. Good Luck.
import java.net.URL;
import java.net.MalformedURLException;
import java.awt.Graphics;
import java.awt.Color;
import java.applet.Applet;
import java.io.InputStream;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.StringTokenizer;
import java.util.Iterator;
import javax.swing.JApplet;
import javax.swing.JFrame;
 
public class ReadApplet extends JApplet
{
	public static final String PROPERTIES_FILE = "app.prp";
	private ArrayList list;
 
	public ReadApplet(){}
 
	public void explicitInit()
   	{
		try
		{
			InputStream is = getClass().getResourceAsStream(PROPERTIES_FILE);
			BufferedReader br = new BufferedReader(new InputStreamReader(is));
			doInit(br);
		}
		catch(Exception e)
		{
			handleException(e);
		}
	}
 
	public void init()
	{
		try
		{
			URL filePath = new URL(getCodeBase() + PROPERTIES_FILE);
			BufferedReader br = new BufferedReader(new InputStreamReader(filePath.openStream()));
			doInit(br);
		}
		catch(Exception e)
		{
			handleException(e);
		}
	}
 
	public void doInit(BufferedReader reader) throws Exception
   	{
		list = new ArrayList();
 
		String buffer;
		String firstName = null;
		String lastName = null;
		int id = 0;
		String email = null;
		while( (buffer = reader.readLine()) != null )
		{
			StringTokenizer tokenizer = new StringTokenizer(buffer, "|");
			while(tokenizer.hasMoreTokens())
			{
				firstName = tokenizer.nextToken();
				lastName = tokenizer.nextToken();
				id = Integer.parseInt(tokenizer.nextToken());
				email = tokenizer.nextToken();
 
				Subscriber aSubscriber = new Subscriber(firstName, lastName, id, email);
				list.add(aSubscriber);
			}
		}
 
		repaint();
	}
 
	public void paint(Graphics g)
	{
		Iterator iterator = list.iterator();
		int i = 1;
		while(iterator.hasNext())
		{
			Subscriber aSubscriber = (Subscriber)iterator.next();
			g.drawString(aSubscriber.toString(), 100, 20 * i);
			i++;
		}
	}
 
	public void handleException(Exception e)
	{
		e.printStackTrace();
	}
 
	public static void main(String s[])
	{
		JFrame frame = new JFrame();
		ReadApplet applet = new ReadApplet();
		applet.explicitInit();
		frame.getContentPane().add(applet);
		frame.setSize(500, 200);
		frame.setVisible(true);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}
}
 
class Subscriber
{
	private String firstName;
	private String lastName;
	private int id;
	private String email;
 
	public Subscriber(String fn, String ln, int id, String e)
	{
		this.firstName = fn;
		this.lastName = ln;
		this.id = id;
		this.email = e;
	}
 
	public String toString()
	{
		StringBuffer buffer = new StringBuffer("[ ");
		buffer.append(firstName);
		buffer.append(" " );
		buffer.append(lastName);
		buffer.append(", ID# ");
		buffer.append(id);
		buffer.append(", ");
		buffer.append(email);
		buffer.append(" ]");
 
		return buffer.toString();
	}
}

Joey

Posts: 12
Nickname: joey
Registered: Jul, 2002

Re: applet to application Posted: Jul 29, 2002 6:09 AM
Reply to this message Reply
thank you a lot!! It was realy getting on my nerves, now all is fine.

Flat View: This topic has 6 replies on 1 page
Topic: JSP call EJB Compoment! Previous Topic   Next Topic Topic: Some suggestions on posting topics

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use