
Java语言程序设计第七讲 Java网络基础 AVA
Java语言程序设计第七讲 Java网络基础

教学内容 ■使用URL ■读取URL中的资源 ■显示URL资源中的html文件 ■处理超链接 ■InetAdress类 ·套接字 ■UDP数据报
教学内容 ◼ 使用URL ◼ 读取URL中的资源 ◼ 显示URL资源中的html文件 ◼ 处理超链接 ◼ InetAdress类 ◼ 套接字 ◼ UDP数 据 报

使用URL JAVA ■URL类是对统一资源定位符(Uniform Resource Locator)的抽象,使用URL创建对 象的应用程序称作客户端程序,一个URL对象 存放着一个具体的资源的引用,表明客户要访 问这个URL中的资源,利用URL对象可以获取 URL中的资源。 ■URL对象通常包含最基本的三部分信息:协议、 地址、资源
使用URL ◼ URL类是对统一资源定位符(Uniform Resource Locator)的抽象,使用URL创建对 象的应用程序称作客户端程序,一个URL对象 存放着一个具体的资源的引用,表明客户要访 问这个URL中的资源,利用URL对象可以获取 URL中的资源 。 ◼ URL对象通常包含最基本的三部分信息:协议、 地址、资源

URL的构造方法 JAVA public URL(String spec)throws MalformedURLException try url=new URL("http://www.open.edu.cn"); catch(MalformedURLException e) { System.out.printIn ("Bad URL:"+url);
URL的构造方法 ◼ public URL(String spec) throws MalformedURLException try { url=new URL("http://www.open.edu.cn"); } catch(MalformedURLException e) { System.out.println ("Bad URL:"+url); }

读取URL中的资源 JAVA ·URL对象调用InputStream openStream)方法 可以返回一个输入流,该输入流指向URL对象 所包含的资源。通过该输入流可以将服务器上 的资源信息读入到客户端。 ·下面是应用举例
读取URL中的资源 ◼ URL对象调用InputStream openStream() 方法 可以返回一个输入流,该输入流指向URL对象 所包含的资源。通过该输入流可以将服务器上 的资源信息读入到客户端。 ◼ 下面是应用举例

import java.awt.*;import java.awt.event.*; import java.net.*; import java.io.*; public class Example11 1 JAVA public static void main(String args[]) new NetWin(); class NetWin extends Frame implements ActionListener,Runnable {Buttonbutton;URL url;TextField text; TextArea area; byte b[]=new byte[118]; Thread thread; NetWin() text=new TextField(20); area=new TextArea(12,12); button=-new Button("确定"): button.addActionListener(this); thread=new Thread(this); Panel p=new Panel(); p.add(new Label(输入网址:"): p.add(text);p.add(button); add(area,BorderLayout.CENTER); add(p,BorderLayout.NORTH); setBounds(60,60,360,300); setVisible(true); validate();
import java.awt.*; import java.awt.event.*; import java.net.*; import java.io.*; public class Example11_1 { public static void main(String args[]) { new NetWin(); } } class NetWin extends Frame implements ActionListener,Runnable { Button button; URL url; TextField text; TextArea area; byte b[]=new byte[118]; Thread thread; NetWin() { text=new TextField(20); area=new TextArea(12,12); button=new Button("确定"); button.addActionListener(this); thread=new Thread(this); Panel p=new Panel(); p.add(new Label("输入网址:")); p.add(text); p.add(button); add(area,BorderLayout.CENTER); add(p,BorderLayout.NORTH); setBounds(60,60,360,300); setVisible(true); validate();

addWindowListener(new WindowAdapter() public void windowClosing(WindowEvent e) System.exit(0); } JAVA public void actionPerformed(ActionEvent e) if(!(thread.isAlive()) 运行结果 thread=new Thread(this); try{thread.start(); 图 日回☒ catch(Exception ee) 输入网址:http:/www.open.ed如.em 确定 {tet.setText("我正在读取"+ur);} iahref="http://www.chinadisedu. public void run() try{int n=-1; area.setText(null); url=new URL(text.getText().trim(); InputStream in=url.openStream(): while((n=in.read(b))!=-1) { String s=new String(b,0,n); area.append(s); } catch(MalformedURLException e1) text.setText("+e1); return; catch(IOException e1) text.setText("+e1); return; }}
addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); } public void actionPerformed(ActionEvent e) { if(!(thread.isAlive())) thread=new Thread(this); try{ thread.start(); } catch(Exception ee) { text.setText("我正在读取"+url); } } public void run() { try { int n=-1; area.setText(null); url=new URL(text.getText().trim()); InputStream in=url.openStream(); while((n=in.read(b))!=-1) { String s=new String(b,0,n); area.append(s); } } catch(MalformedURLException e1) { text.setText(""+e1); return; } catch(IOException e1) { text.setText(""+e1); return; } } } 运行结果

显示URL资源中的html文件 JAVA ·Javax.swing包中 日回☒ 的JEditorPane容 确定 器可以解释执行 C黑龙江广播电视大学 html文件,也就是 HEILONGJIANG RADIO&TELEVISION UNIVERSITY 拉况高高支安数 说, 如果你把html 文件读入到 JEditorPane,该 html文件就会被解 电大部箱 释执行,显示在 JEditorPane容器 中,这样程序就看 黑花近疗播电视大学信息化建设规利 到了网页的运行效
显示URL资源中的html文件 ◼ Javax.swing包中 的JEditorPane容 器可以解释执行 html文件,也就是 说,如果你把html 文件读入到 JEditorPane,该 html文件就会被解 释执行,显示在 JEditorPane容器 中,这样程序就看 到了网页的运行效 果

处理超链接 JAVA ■当JEditorPane对象调用setEditable方法将编 辑属性设为falsel时,不仅可以显示网页的运行 效果,而且用户如果单击网页中超链接还可以 使得JEditorPane对象触发HyperlinkEvent事件。 程序可以通过处理HyperlinkEvent事件,来显 示新的URL资源
处理超链接 ◼ 当JEditorPane对象调用setEditable方法将编 辑属性设为false时,不仅可以显示网页的运行 效果,而且用户如果单击网页中超链接还可以 使得JEditorPane对象触发HyperlinkEvent事件。 程序可以通过处理HyperlinkEvent事件,来显 示新的URL资源

InetAdress类 JAVA java.net包中的InetAddress类对象含有一个 Internet主机地址的域名和IP地址: Ww.sina.com.cn/202.108.35.210: ■域名容易记忆,当你在连接网络时输入一个主 机的域名后,域名服务器(DNS)负责将域名 转化成P地址,这样我们才能和主机建立连接。 ■下面是应用举例
InetAdress类 ◼ java.net包中的InetAddress类对象含有一个 Internet主机地址的域名和IP地址: www.sina.com.cn/202.108.35.210。 ◼ 域名容易记忆,当你在连接网络时输入一个主 机的域名后,域名服务器(DNS)负责将域名 转化成IP地址,这样我们才能和主机建立连接。 ◼ 下面是应用举例