The Artima Developer Community
Sponsored Link

Java Answers Forum
what's wrong with this piece of code?

6 replies on 1 page. Most recent reply: Mar 10, 2002 9:27 PM by Vikram

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
Pieter Provoost

Posts: 9
Nickname: pieter
Registered: Mar, 2002

what's wrong with this piece of code? Posted: Mar 10, 2002 4:16 PM
Reply to this message Reply
Advertisement
the URL does not exist, but still, when I click the button, I get the message "exists"... Or do I have to use another way to find out if this file exists?
import java.awt.*; 
import java.net.*; 
import java.applet.*; 
import java.awt.event.*; 
 
public class Applet2 extends Applet implements ActionListener  
{
	Button button;
	boolean exists;
	URL address;
	Object object;
	
	public void init(){
		button = new Button("click");
		add(button);
		exists = true;
	}
	
	public void paint(Graphics g){
		if(exists){ 
			g.drawString("exists",10,10); 
		}
		else{
			g.drawString("does not exist",10,10); 
		}
	} 
 
	public void actionPerformed(ActionEvent evt){
		if (evt.getSource() == button){
			try{
			String temp = "http://studwww.rug.ac.be/~pprovoos/pierkunde.html";
			address = new URL(temp);	
			object = address.getContent();
			}
			catch(Exception e){
			exists = false;
			}
			repaint();
		}	
	}
}


Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: what's wrong with this piece of code? Posted: Mar 10, 2002 4:44 PM
Reply to this message Reply
Don't you need to add an ActionListener to the button? I haven't seen an Applet that implents an ActionListener before, but I suppose you could add the applet instance to the button. I wouldn't do it this way, though, because it makes the action appear to be more associated with the Applet, rather than the button. Instead, I would do the usual thing of creating an anonymous inner class to implement the button's ActionListener. This is especially true in the case where you have more than one button, where your Applet's actionPerformed() method will become a big sloppy difficult-to-maintain mess of checking the event's source against controls.

What exception are you expecting? The URL constructor says it can throw a MalformedURLException whereas the getContent() method can throw an IOException, so you should probably have a catch for each of these.

Pieter Provoost

Posts: 9
Nickname: pieter
Registered: Mar, 2002

Re: what's wrong with this piece of code? Posted: Mar 10, 2002 5:05 PM
Reply to this message Reply
Indeed, I forgot the ActionListener, I'll give that a try. How can I catch for 2 exceptions at once? (sorry but this is all quite new for me)

Pieter Provoost

Posts: 9
Nickname: pieter
Registered: Mar, 2002

Re: what's wrong with this piece of code? Posted: Mar 10, 2002 5:15 PM
Reply to this message Reply
next try
import java.awt.*; 
import java.net.*; 
import java.applet.*; 
import java.awt.event.*; 
import java.io.*; 
 
public class Applet2 extends Applet implements ActionListener  
{
	Button button;
	boolean exists;
	URL address;
	Object object;
	
	public void init(){		
		button = new Button("click");		
		add(button);	
		button.addActionListener(this); 
		exists = true;	
	}		
	
	public void paint(Graphics g){		
		if(exists){ 			
			g.drawString("exists",10,10); 		
		}		
		else{			
			g.drawString("does not exist",10,10); 		
		}	
	} 	
	
	public void actionPerformed(ActionEvent evt){		
		if (evt.getSource() == button){			
			try{			
				String temp = "http://studwww.rug.ac.be/~pprovoos/dierkunde.html";		
				address = new URL(temp);			
				object = address.getContent();		
			}			
			catch(IOException ioe){			
				exists = false;		
			}			
			repaint();	
		}	
	}
}

Charles Bell

Posts: 519
Nickname: charles
Registered: Feb, 2002

Re: what's wrong with this piece of code? Posted: Mar 10, 2002 5:39 PM
Reply to this message Reply
There was more to it than I remembered this morning.
The server may return some html if the page does not exist. So in the case of the url you mentioned in the previous code, it will return several lines, one of which reads "The page cannot be found"
This probably will not work in all cases, but if you are dealing with just one server you can at least experiment and see what it gives back for missing pages.
I tried "http://www.gnu.org/"
which does really exist and the program will read all the line of html returned just fine

I tried "http://www.gnu.org/qqq.html"
which does not exist and I got an IOException
and I caught it in the catch block to indicate that the file was not there.

From what I remember, an almost fool proof way is to try and get an HTTPURLConnection and read the response code. If you get a 200 or HTTPURLConnection.HTTP_OK then everything is okay and the file exists.
You can get an HttpURLConnection with the following method.


/** Returns a HttpURL connection based upon the input url string.
*/
public HttpURLConnection getHttpURLConnection(String urlstring){
HttpURLConnection connection = null;
try{
if (debug) System.out.println("Connecting to " + urlstring);
URL url = new URL(urlstring);
connection = (HttpURLConnection)url.openConnection();
}catch(MalformedURLException murle){
System.out.println("MalformedURLException: " + murle.getMessage());
if (debug) System.out.println("Returning null connection");
return null;
}catch(IOException ioe){
System.out.println("IOException: " + ioe.getMessage());
return null;
}
if (connection == null) System.out.println("Returning null connection");
return connection;
}


Anyway, this is what I have so far.



import java.awt.*;
import java.io.*;
import java.net.*;
import java.applet.*;
import java.awt.event.*;

public class Applet2 extends Applet implements ActionListener
{
Button button;
boolean exists;
URL address;
Object object;

public void init(){
button = new Button("click");
add(button);
button.addActionListener(this);
exists = false;
}

public void paint(Graphics g){
if(exists){
g.drawString("exists",10,10);
}
else{
g.drawString("does not exist",10,10);
}
}

public void actionPerformed(ActionEvent evt){
if (evt.getSource() == button){
try{
//String temp = "http://studwww.rug.ac.be/~pprovoos/pierkunde.html";
//String temp = "http://www.gnu.org/";//this does really exist
String temp = "http://www.gnu.org/qqq.html";//this does not
address = new URL(temp);
//object = address.getContent();
String file = address.getFile();
if (file.length() > 0) {
try{
URLConnection connection = address.openConnection();
String type = connection.getContentType();
//System.out.println(type);
if (type.compareToIgnoreCase("text/html") == 0){
InputStream is = connection.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String lineread = "";
String html = "";
while ((lineread = br.readLine()) != null){
html = html + lineread;
//System.out.println(lineread);
}
String cantbefound = "The page cannot be found";
if (html.toLowerCase().indexOf(cantbefound.toLowerCase()) >= 0){
exists = false;
}else{
exists = true;
}
}

}catch(IOException ioe){
System.err.println("IOException: " + ioe.getMessage());
exists = false;

}
}

}
catch(Exception e){
exists = false;
}
repaint();
}
}
}
[/pre}

Pieter Provoost

Posts: 9
Nickname: pieter
Registered: Mar, 2002

Re: what's wrong with this piece of code? Posted: Mar 10, 2002 6:09 PM
Reply to this message Reply
It's 3 am so I will take a closer look at this tomorrow, thanks anyway. So suppose I work on the www.rug.ac.be server, I could just compare the requested file and error404stud-busy.html, the errorpage of this server?

Vikram

Posts: 2
Nickname: vikram
Registered: Mar, 2002

Re: what's wrong with this piece of code? Posted: Mar 10, 2002 9:27 PM
Reply to this message Reply
wouldnt it be simpler if he just checks the
the http response code is 404 or not

see HttpUrlConnection.getResponseCode()

--vikram

Flat View: This topic has 6 replies on 1 page
Topic: Roulette Previous Topic   Next Topic Topic: Run Time Error in my program.

Sponsored Links



Google
  Web Artima.com   

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