473,395 Members | 1,701 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

read a text file from an applet

jhardman
3,406 Expert 2GB
I am having a hard time opening a text file from an applet. I remembered after an attempt or two that there were obvious security issues here, so I switched the code to pull a text file off the internet, and there is obviously some naming or reference issue that I can't get my head around. Here is my code:
Expand|Select|Wrap|Line Numbers
  1. import java.awt.*;
  2. import java.awt.Font;
  3. import java.awt.Graphics;
  4. import java.awt.FontMetrics;
  5. import java.io.*;
  6. import java.net.*;
  7.  
  8.  
  9. public class solong17 extends java.applet.Applet {
  10.  
  11.     String letter = new String();
  12.  
  13.     public void paint(Graphics screen) {
  14.         //Color bk = new Color(0,0,0);
  15.         //Color tx = new Color(255,255,255);
  16.         Font f = new Font("Arial", Font.BOLD, 18);
  17.         FontMetrics fm = getFontMetrics(f);
  18.         setBackground(Color.black);
  19.         screen.setColor(Color.white);
  20.         screen.setFont(f);
  21.         String s = "So long, and thanks for all the fish.";
  22.         int x = (size().width - fm.stringWidth(s)) / 2;
  23.         int y = size().height / 2;
  24.         screen.drawString(s, x, y);
  25.     }
  26.  
  27.     TextArea lt;
  28.  
  29.  
  30.     public void init() {
  31.  
  32.         try {
  33.             URL page = new
  34.             URL("http://www.worksonline.org/works/default.htm");
  35.         }
  36.         catch (MalformedURLException e) {
  37.             System.out.println("Bad URL: http://www.worksonline.org" +
  38.                 "/works/default.htm");
  39.         }
  40.  
  41.         URLConnection conn = null;
  42.         InputStreamReader in;
  43.         BufferedReader data;
  44.         StringBuffer buf = new StringBuffer();
  45.         String line;
  46.         //System.out.println(page);
  47.  
  48.         try {
  49.             conn = this.page.openConnection(); //error is here
  50.             conn.connect();
  51.             in = new InputStreamReader(conn.getInputStream());
  52.             data = new BufferedReader(in);
  53.             /*FileReader file = new FileReader("solong16.java");
  54.             BufferedReader buff = new
  55.                 BufferedReader(file);
  56.             boolean eof = false;*/
  57.             while ((line = data.readLine()) != null) {
  58.                 buf.append(line + "\n");
  59.             }
  60.  
  61.             //buff.close();
  62.         } catch (IOException e) {
  63.             System.out.println("Error -- " + e.toString());
  64.         }
  65.  
  66.         letter = buf.toString();
  67.  
  68.         lt = new TextArea(letter, 5,50, 1);
  69.         Color bk = new Color(10,10,25);
  70.         lt.setBackground(bk);
  71.         lt.setForeground(Color.white);
  72.         lt.setEditable(false);
  73.         Font tf = new Font("Arial", Font.BOLD, 18);
  74.         lt.setFont(tf);
  75.         add(lt);
  76.     }
  77. }
The error (line 49) says "page" isn't found, so I think there is some naming or reference convention I'm missing.

I'm just trying this task to "get my feet wet" with java. I'm heavily entrenched in Visual Basic, and really wanted something completely different, and I'm having a hard time understanding what must be some real basics.

Jared
Jan 6 '08 #1
14 2927
r035198x
13,262 8TB
Remove the "this" on line 49 where you have this.page ...
Jan 7 '08 #2
jhardman
3,406 Expert 2GB
Remove the "this" on line 49 where you have this.page ...
Thanks for your attention, but that was one of the first things I tried and it doesn't change the error.
Expand|Select|Wrap|Line Numbers
  1. cannot find symbol
  2. symbol: variable page
With the "this." the carot indicating the error location points to the dot, without it, the carot points to the p, otherwise there is no difference in the error.

Jared
Jan 7 '08 #3
BigDaddyLH
1,216 Expert 1GB
Your error is here:

Expand|Select|Wrap|Line Numbers
  1. try {
  2.             URL page = new URL("...");
  3.      }
That declares page as a variable local to the try block, so it can't be used after the block. Declare it in a wider scope, for instance:

Expand|Select|Wrap|Line Numbers
  1. URL url = null;
  2. try {
  3.             page = new URL("...");
  4.      }
You shouldn't soldier on if that url is bad, though, right?
Jan 7 '08 #4
jhardman
3,406 Expert 2GB
Your error is here:
Thanks, that did the trick. I hadn't realized that the try block had any locale.
You shouldn't soldier on if that url is bad, though, right?
he he. So I tried it with a URL that existed and I got an access denied exception, so back to the drawing board, but at least that's one question down. Thanks a lot.

Jared
Jan 7 '08 #5
BigDaddyLH
1,216 Expert 1GB
he he. So I tried it with a URL that existed and I got an access denied exception, so back to the drawing board, but at least that's one question down. Thanks a lot.
An untrusted applet can only open connections back on the server from which it was downloaded. This is for security. Otherwise, an applet downloaded through a firewall could try to hack into machines hidden behind the firewall.

I'm not a fan of applets. Another option would we to write this as a normal application. Does it have to be an applet?
Jan 7 '08 #6
jhardman
3,406 Expert 2GB
An untrusted applet can only open connections back on the server from which it was downloaded. This is for security. Otherwise, an applet downloaded through a firewall could try to hack into machines hidden behind the firewall.
I figured the problem was something like that. The applet is just hosted on my computer, I just browsed to the HTML file that displays it, I didn't use a server at all. Any hint about getting my own applet to be trusted? Or should I just put it up on the same domain as the file I was trying to download?
I'm not a fan of applets. Another option would we to write this as a normal application. Does it have to be an applet?
Not at all. I was trying this project to "get my feet wet" and I thought to myself, "Opening, displaying (and next step: manipulating) a text file would be a good exercize." I settled on putting it in an applet because it seemed to me that formatting and positioning are simpler there. I'm just a noob when it comes to java, and some of that stuff is still kind of confusing. In ASP I use VBScript to write the code, and HTML/CSS to do layout and formatting, and I kind of like that. Writing layout in Java seems daunting. If I were to write this as a stand-alone app, I'm not even sure what I should put it in. That's the kind of thing that makes me want to not even bother starting.

Jared
Jan 7 '08 #7
BigDaddyLH
1,216 Expert 1GB
I find the opposite: it's easier to develop a GUI as a standalone application rather than as an applet. The main reason is that you don't have to fight with the browser.

Here for example, is a standalone application that displays the contents of a URL:
Expand|Select|Wrap|Line Numbers
  1. import java.io.*;
  2. import java.net.*;
  3. import javax.swing.*;
  4.  
  5. public class TextAreaExample implements Runnable {
  6.     public void run() {
  7.         JTextArea textArea= new JTextArea(20, 40);
  8.         try {
  9.             URL url = new URL("http://java.sun.com");
  10.             InputStream in = url.openStream();
  11.             textArea.read(new InputStreamReader(in), url);
  12.         } catch (IOException e) {
  13.             throw new RuntimeException(e);
  14.         }
  15.         JFrame f = new JFrame("TextAreaExample");
  16.         f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  17.         f.getContentPane().add(new JScrollPane(textArea));
  18.         f.pack();
  19.         f.setLocationRelativeTo(null);
  20.         f.setVisible(true);
  21.     }
  22.  
  23.     public static void main(String[] args) throws IOException {
  24.         SwingUtilities.invokeLater(new TextAreaExample());
  25.     }
  26. }
If some of these methods are unfamiliar, why not take the Swing tutorial?

Sun's Swing Tutorial
Jan 7 '08 #8
jhardman
3,406 Expert 2GB
If some of these methods are unfamiliar, why not take the Swing tutorial?

Sun's Swing Tutorial
Thanks, I'll take your advice.

Jared
Jan 7 '08 #9
r035198x
13,262 8TB
Thanks, I'll take your advice.

Jared
Judging from your background, I'd suggest also that you consider going the JSP way.
Jan 8 '08 #10
jhardman
3,406 Expert 2GB
Judging from your background, I'd suggest also that you consider going the JSP way.
I've considered, but it looks like there is more of a future in PHP or leaving scripting behind entirely. Do you do anything in JSP?

Jared
Jan 8 '08 #11
Dököll
2,364 Expert 2GB
I find the opposite: it's easier to develop a GUI as a standalone application rather than as an applet. The main reason is that you don't have to fight with the browser.

Here for example, is a standalone application that displays the contents of a URL:
Expand|Select|Wrap|Line Numbers
  1. import java.io.*;
  2. import java.net.*;
  3. import javax.swing.*;
  4.  
  5. public class TextAreaExample implements Runnable {
  6.     public void run() {
  7.         JTextArea textArea= new JTextArea(20, 40);
  8.         try {
  9.             URL url = new URL("http://java.sun.com");
  10.             InputStream in = url.openStream();
  11.             textArea.read(new InputStreamReader(in), url);
  12.         } catch (IOException e) {
  13.             throw new RuntimeException(e);
  14.         }
  15.         JFrame f = new JFrame("TextAreaExample");
  16.         f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  17.         f.getContentPane().add(new JScrollPane(textArea));
  18.         f.pack();
  19.         f.setLocationRelativeTo(null);
  20.         f.setVisible(true);
  21.     }
  22.  
  23.     public static void main(String[] args) throws IOException {
  24.         SwingUtilities.invokeLater(new TextAreaExample());
  25.     }
  26. }
If some of these methods are unfamiliar, why not take the Swing tutorial?

Sun's Swing Tutorial
Hey, BDLH!

Hiya Jared...Just getting my feet wet as well, good luck:-)

BDLH, would you say even though a GUI as standalone app would effortlessly be converted to an Applet or otherwise work as an applet when packaged?

This could have come out weird, not sure the proper lingo as of yet, just trying to see what the real advantages are to building as either or.

Dököll
Jan 9 '08 #12
BigDaddyLH
1,216 Expert 1GB
Hey, BDLH!

BDLH, would you say even though a GUI as standalone app would effortlessly be converted to an Applet or otherwise work as an applet when packaged?
If you design an application with this in mind, it will be easy to wrap an applet around it. Of course, an untrusted applet can't do many things an application can do -- local file I/O, connecting to a general URL, using the system clip board, etc...

My main beef is that people are often told to start with applets by out-of-date textbooks and tutorials. I'll repeat what I wrote before: it's easier to develop and debug code if it's not an applet. If you really must write an applet, do the conversion as a last step.
Jan 9 '08 #13
Dököll
2,364 Expert 2GB
If you design an application with this in mind, it will be easy to wrap an applet around it. Of course, an untrusted applet can't do many things an application can do -- local file I/O, connecting to a general URL, using the system clip board, etc...

My main beef is that people are often told to start with applets by out-of-date textbooks and tutorials. I'll repeat what I wrote before: it's easier to develop and debug code if it's not an applet. If you really must write an applet, do the conversion as a last step.
Super appreciate it, BigDaddyLH!

I will keep that in mind. I actually wanted to be sure in the case I wanted persons to work on same project having each specific class in the code assigned to each specific member of the group. Take class Room, as one example, that's handled by member or person # 1, class Eat @ member # 2, and so on...

Sort of what one can do in any other language, like VB for instance, where one can assign modules @ 1 person each, still having the main code...

I think not going againts the browser therefore will help save time and effort between the group.

Thanks for replying on this, BigDaddyLH:-).

In a bit!

Dököll
Jan 27 '08 #14
BigDaddyLH
1,216 Expert 1GB
Super appreciate it, BigDaddyLH!

I will keep that in mind. I actually wanted to be sure in the case I wanted persons to work on same project having each specific class in the code assigned to each specific member of the group.
If you are working on a group project for a course, I hope your instructor started by emphasizing unit testing, first.

http://en.wikipedia.org/wiki/Unit_testing

You need to be able to test your code in isolation before combining it with anyone else's. Since you've programmed in .NET you may already be familiar with NUnit, which is the /NET rewrite of the very popular Java Unit testing framework, JUnit:

http://junit.org/
Jan 27 '08 #15

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Doug van Vianen | last post by:
Hi, I am working on an Applet which provides some mouse practice for new computer users in our local seniors' computer club. The applet contains several cards, in a card layout, which are...
14
by: Michael Levin | last post by:
I've got the following problem. I'm a biologist and I have a device at work which monitors my frog habitat. The device has a bunch of sensors, and runs an embedded html server with some java...
5
by: Doug | last post by:
I would appreciate a response on this. I have clients who will be placing text files information about in a network directory for example: http://www.protegen.com/clients/ Is it possible to...
2
by: Thomas Magma | last post by:
Hello, I have a case where an applet .class file may or may not be present. In the case when it is not present I get a big gray box where the applet is suppose to be. This is understandable, but...
1
by: Roberto Gallo | last post by:
Hi, I have an applet running inside an html document and I need to read and to set some of html form's textfield and textarea input from my applet. How can I do that? Do I need a javascript? ...
2
by: ying | last post by:
I need develop an asp.net script which will run on web server. The script need read documents from client's machine(know the filename, need check if the file exist or not), convert the file to...
3
by: Jimbo | last post by:
I have a website & applet produced under Windows-XP. It works fine. I took it to a machine running Windows 98 & an earlier version of Java to test what happens when I open the web page using IE6 &...
2
by: dimitryous | last post by:
Hello, I just would like to read a specific (/Downloads/) directory of the client machine in order to get the latest file: My code look for text files like "quotes.txt, quotes-1.txt, ......
1
by: sandeepk84 | last post by:
Hi all... I am having an applet program for playing an audio file. It runs normally... but when it's run from browser, its showing the filepermission error. java.security.AccessControlException:...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.