インストラクターがIDEの使用を望まないため、これをテキストパッドで作成しました。Webサイトを入力して、で表示しようとしていJEditorPane
ます。
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
import javax.swing.*;
import java.util.*;
public class ViewRemoteFile extends JApplet
{
private JButton jbtView = new JButton("View");
private JTextField jtfURL = new JTextField(12);
private JEditorPane jtaFile = new JEditorPane();
private JLabel jlblStatus = new JLabel();
public void init()
{
JPanel p1 = new JPanel();
p1.add(new JLabel("URL"), BorderLayout.WEST);
p1.add(jtfURL, BorderLayout.CENTER);
p1.add(jbtView, BorderLayout.EAST);
add(new JScrollPane(jtaFile), BorderLayout.CENTER);
add(p1, BorderLayout.NORTH);
add(jlblStatus, BorderLayout.SOUTH);
jbtView.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
showFile();
}
});
}
private void showFile()
{
//Scanner input = null;
//URL url = null;
try
{
jtaFile.setPage(new URL(jtfURL.getText().trim()));
jlblStatus.setText("File loaded successfully");
}
catch(MalformedURLException ex)
{
jlblStatus.setText("URL " + jtfURL.getText().trim() + " not found.");
}
catch(IOException ex)
{
jlblStatus.setText(ex.getMessage());
}
/*finally
{
if(input != null)
input.close();
}*/
}
}
このコードを実行すると、サイトが表示されず、クラッシュしてコンソールに大量のエラーが表示されます。
http://www.yahoo.comの場合 :
at java.awt.EventDispatchThread.run(EventDispatchThread.java:90)
Caused by: java.security.AccessControlException: access denied ("java.net.Socket
Permission" "www.yahoo.com:80" "connect,resolve")
at java.security.AccessControlContext.checkPermission(AccessControlConte
xt.java:366)
at java.security.AccessController.checkPermission(AccessController.java:
555)
at java.lang.SecurityManager.checkPermission(SecurityManager.java:549)
at java.lang.SecurityManager.checkConnect(SecurityManager.java:1051)
at sun.net.www.http.HttpClient.openServer(HttpClient.java:466)
at sun.net.www.http.HttpClient.<init>(HttpClient.java:213)
at sun.net.www.http.HttpClient.New(HttpClient.java:300)
at sun.net.www.http.HttpClient.New(HttpClient.java:316)
at sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(HttpURLC
onnection.java:992)
at sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConne
ction.java:928)
at sun.net.www.protocol.http.HttpURLConnection.connect(HttpURLConnection
.java:846)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLCon
nection.java:1296)
at java.net.HttpURLConnection.getResponseCode(HttpURLConnection.java:468
)
... 40 more