程序功能:在文本框中输入完整的url,按start,url会加到上面的列表中去,然后会开始扫描那个网页,找到有超文本链接的,就把它们都加到列表中,直到结束为止
问题 就是 Visit功能不能实现 :
网页中运行的时候会出现这样的提示呢:Errorjava.security.AccessControlException:access denied(java.net.SocketPermission (网址):80 connect,resolve)
import java.awt.*;
import java.awt.List;
import java.applet.*;
import java.net.*;
import java.io.*;
import java.util.*;
public class CrawLerApplet extends Applet implements Runnable
{
private TextField urlField=new TextField(30);
private List links=new List(10,false);
private URL u=null;
private Thread runner=null;
private int n=0;
public void init()
{
setLayout(new BorderLayout());
Panel p=new Panel();
p.setLayout(new FlowLayout());
p.add(urlField);
p.add(new Button( "Start "));
p.add(new Button( "Visit "));
add( "South ",p);
add( "Center ",links);
}
public boolean action(Event evt,Object arg)
{
if(arg.equals( "Visit "))
{
visit(links.getSelectedItem());
}
else if(arg.equals( "Start "))
{
String s=urlField.getText();
if(s.length()> 0)
{
add(s);
urlField.setText( " ");
if(runner==null)
{
runner=new Thread(this);
runner.setPriority(Thread.MIN_PRIORITY);
runner.start();
}
}
}
else return super.action(evt,arg);
return true;
}
public void visit(String s)
{
if(s==null) return;
try
{
AppletContext context=getAppletContext();
u=new URL(s);
context.showDocument(u, "Webpage ");
}
catch(Exception e)
{
showStatus( "Error "+e);
}
}
void add(String s)
{
int i;
for(i=0;i <links.countItems();i++)
if(links.getItem(i).equals(s)) return;
links.addItem(s);
repaint();
}
public void search(String s)
{
if(s==null) return;
try
{
AppletContext context=getAppletContext();
u=new URL(s);
showStatus( "Opening "+s);
InputStream input=u.openStream();
showStatus( "Opened "+s);
Searcher search=new Searcher(input);
String name;
while((name=search.nextURL())!=null)
add(name);
input.close();
showStatus( "Done ");
}
catch(Exception e)
{
showStatus( "Error "+e);
}
}
public void run()
{
while(n <links.countItems())
{
String s=links.getItem(n);
n++;
search(s);
runner.yield();
}
}
public void stop()
{
if((runner!=null)&&runner.isAlive())
runner.suspend();
}
public void start()
{
if(runner!=null&&runner.isAlive())
runner.resume();
}
public void destroy()
{
if(runner!=null&&runner.isAlive())
runner.resume();