/*
客户端:自定义
服务端:tomcat服务器
需求:
自定义一个客户端,向tomcat服务器发请求,要求浏览demo.html页面。
自定义客户端用到了图形化界面。
URL类封装了Socket类。
传输层--->应用层
*/
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
class MyIE2
{
private Frame f;
private Button but;
private TextField tf;
private TextArea ta;
MyIE2()
{
init();
}
public void init()
{
f=new Frame("IE浏览器");
f.setBounds(300,150,600,500);//坐标x y x y
f.setLayout(new FlowLayout());
tf=new TextField(50);
f.add(tf);
but=new Button("转到");
f.add(but);
ta=new TextArea(25,60);//25行,60列
f.add(ta);
myEvent();
f.setVisible(true);
}
private void myEvent()
{
tf.addKeyListener(new KeyAdapter()
{
public void keyPressed(KeyEvent e)
{
try
{
if(e.getKeyCode()==KeyEvent.VK_ENTER)
showDir();
}
catch (Exception ex)
{
}
}
});
but.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
try
{
showDir();
}
catch (Exception ex)
{
}
}
});
f.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
}
public void showDir()throws Exception
{
//ta.setText("");
String urlInfo = tf.getText();//http://192.168.1.101:8080/myweb/demo.html
URL url = new URL(urlInfo);
URLConnection conn = url.openConnection();
InputStream in = conn.getInputStream();
byte[] buf = new byte[1024];
int len=0;
while((len=in.read(buf))!=-1)
{
ta.setText(new String(buf,0,len));
}
}
public static void main(String[] args) throws Exception
{
new MyIE2();
}
}
day24/MyIE2.java
最新推荐文章于 2017-07-30 23:07:00 发布