黑马程序员_简易版IE浏览器 2.0
2015-11-04 19:14
471 查看
------- <a href="http://www.itheima.com" target="blank">android培训</a>、<a href="http://www.itheima.com" target="blank">java培训</a>、期待与您交流! ----------
package socketTest;
import java.io.*;
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.net.*;
public class HttpIEDemo2 {
/**
* 需求:
* 1:服务器是tomcat,里面有一个hello.html简单的页面
* 2:客户端自定义
* 要求自定义的客户端能访问服务器的资源,通过URL对象的方法,不用Socket的对象
* 两者对比
* 这里不会出现传输层的头部信息,还有一些确认信息等
* 简易版 IE 浏览器 2.0
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
MyIeWeb1 ie = new MyIeWeb1();
}
}
class MyIeWeb1 extends JFrame implements ActionListener
{
private TextField tf = null;
private TextArea ta = null;
private JButton jb1 = null;
private JPanel jp1 = null;
private JPanel jp2 = null;
//初始化相关的组件及注册简听
public MyIeWeb1()
{
tf = new TextField(50);
ta = new TextArea();
jb1 = new JButton("转到");
jb1.addActionListener(this);
jb1.setActionCommand("zd");
jp1 = new JPanel();
jp2 = new JPanel();
jp1.add(tf);
jp1.add(jb1);
jp2.add(ta);
this.setLayout(new BorderLayout());
this.add(jp1,BorderLayout.NORTH);
this.add(jp2,BorderLayout.CENTER);
this.setSize(700,300);
this.setLocation(400,300);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if (e.getActionCommand().equals("zd"))
{
//监听处理
showIEWeb();
}
}
public void showIEWeb()
{
// System.out.println("转到");
//每一次访问前要刷新
ta.setText("");
//得到url的值
String urlPath = tf.getText(); //http://192.168.1.154:8888/MyWebSit/hello.html
try {
//拋 MalformedURLException 异常
URL url = new URL(urlPath);
//拋IO异常
URLConnection conn = url.openConnection();
//封装了Socket对象中的getInputStream()方法
InputStream in = conn.getInputStream();
byte [] buf = new byte[1024];
int len = in.read(buf);
ta.setText(new String(buf,0,len));
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
package socketTest;
import java.io.*;
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.net.*;
public class HttpIEDemo2 {
/**
* 需求:
* 1:服务器是tomcat,里面有一个hello.html简单的页面
* 2:客户端自定义
* 要求自定义的客户端能访问服务器的资源,通过URL对象的方法,不用Socket的对象
* 两者对比
* 这里不会出现传输层的头部信息,还有一些确认信息等
* 简易版 IE 浏览器 2.0
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
MyIeWeb1 ie = new MyIeWeb1();
}
}
class MyIeWeb1 extends JFrame implements ActionListener
{
private TextField tf = null;
private TextArea ta = null;
private JButton jb1 = null;
private JPanel jp1 = null;
private JPanel jp2 = null;
//初始化相关的组件及注册简听
public MyIeWeb1()
{
tf = new TextField(50);
ta = new TextArea();
jb1 = new JButton("转到");
jb1.addActionListener(this);
jb1.setActionCommand("zd");
jp1 = new JPanel();
jp2 = new JPanel();
jp1.add(tf);
jp1.add(jb1);
jp2.add(ta);
this.setLayout(new BorderLayout());
this.add(jp1,BorderLayout.NORTH);
this.add(jp2,BorderLayout.CENTER);
this.setSize(700,300);
this.setLocation(400,300);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if (e.getActionCommand().equals("zd"))
{
//监听处理
showIEWeb();
}
}
public void showIEWeb()
{
// System.out.println("转到");
//每一次访问前要刷新
ta.setText("");
//得到url的值
String urlPath = tf.getText(); //http://192.168.1.154:8888/MyWebSit/hello.html
try {
//拋 MalformedURLException 异常
URL url = new URL(urlPath);
//拋IO异常
URLConnection conn = url.openConnection();
//封装了Socket对象中的getInputStream()方法
InputStream in = conn.getInputStream();
byte [] buf = new byte[1024];
int len = in.read(buf);
ta.setText(new String(buf,0,len));
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
相关文章推荐
- java对世界各个时区(TimeZone)的通用转换处理方法(转载)
- java-注解annotation
- java-模拟tomcat服务器
- java-用HttpURLConnection发送Http请求.
- java-WEB中的监听器Lisener
- Android IPC进程间通讯机制
- Android Manifest 用法
- Android学习笔记(二九):嵌入浏览器
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- 峰回路转,Firefox 浏览器即将重返 iOS 平台
- 介绍一款信息管理系统的开源框架---jeecg
- 聚类算法之kmeans算法java版本
- java实现 PageRank算法
- 峰回路转,Firefox 浏览器即将重返 iOS 平台
- PropertyChangeListener简单理解
- 插入排序
- 冒泡排序