java入门视频教学 博客园_网络编程4--毕向东java基础教程视频学习笔记

Day24

06 自定义浏览器-Tomcat服务端

07 自定义图形界面浏览器-Tomcat服务端

08 URL-URLConnection

09 小知识点

10 域名解析

06 自定义浏览器-Tomcat服务端

客户端:自定义

服务端:Tomcat

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

1 import java.net.*;2 import java.io.*;3 classMyIE4 {5 public static void main(String[] args)throwsException6 {7 Socket s=new Socket("127.0.0.1",8080);8 PrintWriter out=new PrintWriter(s,getOutputStream(),true);9

10 out.prinln("GET /myWeb/Demo.html HTTP/1.1");11 out.prinln("Accept:*/*");12 out.println("Accept-Language:zh-cn");13 out.println("Host:127.0.0.1:11000");14 out.println("Connection:closed");15

16 out.println();17 out.println();18

19 BufferedReader bufr=

20 new BufferedReader(newInputStreamReader(s.getInputStreamReader));21 String line=null;22 while((line=bufr.readLine())!=null)23 {24 System.out.println(line);25 }26 s.close();27 }28 }

View Code

在Tomcat服务端开启的情况下,运行此程序控制台便能显示服务端发给客户端的信息。

07 自定义图形界面浏览器-Tomcat服务端

实现图形化界面的浏览器代码如下,程序的关键是对输入的网址进行分割,获得ip地址,端口号和访问路径。

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

1 import java.awt.*;2 import java.awt.event.*;3 import java.io.*;4 import java.net.*;5 classMyIEByGUI6 {7 privateFrame f;8 privateTextField tf;9 privateButton but;10 privateTextArea ta;11

12 privateDialog d;13 privateLabel lab;14 privateButton okBut;15

16

17 MyIEByGUI()18 {19 init();20 }21 public voidinit()22 {23 f = new Frame("my window");24 f.setBounds(300,100,600,500);25 f.setLayout(newFlowLayout());26

27 tf = new TextField(60);28

29 but = new Button("转到");30

31 ta = new TextArea(25,70);32

33

34 d = new Dialog(f,"提示信息-self",true);35 d.setBounds(400,200,240,150);36 d.setLayout(newFlowLayout());37 lab = newLabel();38 okBut = new Button("确定");39

40 d.add(lab);41 d.add(okBut);42

43

44

45 f.add(tf);46 f.add(but);47 f.add(ta);48

49

50 myEvent();51 f.setVisible(true);52 }53 private voidmyEvent()54 {55

56 okBut.addActionListener(newActionListener()57 {58 public voidactionPerformed(ActionEvent e)59 {60 d.setVisible(false);61 }62 });63 d.addWindowListener(newWindowAdapter()64 {65 public voidwindowClosing(WindowEvent e)66 {67 d.setVisible(false);68 }69 });70

71 tf.addKeyListener(newKeyAdapter()72 {73 public voidkeyPressed(KeyEvent e)74 {75 try

76 {77 if(e.getKeyCode()==KeyEvent.VK_ENTER)78 showDir();79 }80 catch(Exception ex)81 {82 }83

84 }85 });86

87

88 but.addActionListener(newActionListener()89 {90 public voidactionPerformed(ActionEvent e)91 {92 try

93 {94 showDir();95 }96 catch(Exception ex)97 {98 }99

100

101 }102 });103

104 f.addWindowListener(newWindowAdapter()105 {106 public voidwindowClosing(WindowEvent e)107 {108 System.exit(0);109 }110 });111 }112

113 private void showDir()throwsException114 {115

116 ta.setText("");117 String url = tf.getText();//http://192.168.1.254:8080/myweb/demo.html

118

119 int index1 = url.indexOf("//")+2;120

121 int index2 = url.indexOf("/",index1);122

123

124

125 String str =url.substring(index1,index2);126 String[] arr = str.split(":");127 String host = arr[0];128 int port = Integer.parseInt(arr[1]);129

130 String path =url.substring(index2);131 //ta.setText(str+"...."+path);

132

133

134 Socket s = newSocket(host,port);135

136 PrintWriter out = new PrintWriter(s.getOutputStream(),true);137

138 out.println("GET "+path+" HTTP/1.1");139 out.println("Accept: */*");140 out.println("Accept-Language: zh-cn");141 out.println("Host: 127.0.0.1:11000");142 out.println("Connection: closed");143

144 out.println();145 out.println();146

147 BufferedReader bufr = new BufferedReader(newInputStreamReader(s.getInputStream()));148

149 String line = null;150

151 while((line=bufr.readLine())!=null)152 {153 ta.append(line+"\r\n");154 }155

156 s.close();157

158 }159

160 public static voidmain(String[] args)161 {162 newMyIEByGUI();163 }164 }

View Code

在Tomcat服务端开启的情况下,在窗口上方的输入网址区输入:http://127.0.0.1:8080/myWeb/Demo.html,再按Enter或者转到,运行显示如下:

601cc8adf13af8d033d62ebd63683fa9.png

08 URL-URLConnection

在07中为了获得有用信息,分割字符串很是麻烦。我们可以使用java.net包中的URL类。

类 URL 代表一个统一资源定位符,它是指向互联网“资源”的指针。

URL 可选择指定一个“端口”,它是用于建立到远程主机 TCP 连接的端口号。

如果未指定该端口号,则使用协议默认的端口。例如,http 协议的默认端口为 80。

构造方法:

URL(String spec):

URL(String protocl,String host;int port,String file)

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

1 import java.net.*;2 classURLDemo3 {4 public static void main(String[] args)throwsMalformedURLException5 {6 URL url=new URL("http://127.0.0.1:8080/myWeb/Demo.html?name=hahaha&name=20");7

8 System.out.println("getFile(): "+url.getFile());9 System.out.println("getHost(): "+url.getHost());10 System.out.println("getPath(): "+url.getPath());11 System.out.println("getPort(): "+url.getPort());12 System.out.println("getProtocol(): "+url.getProtocol());13 System.out.println("getQuery(): "+url.getQuery());14 }15 }16 /*

17 String getFile()18 获取此 URL 的文件名。19 String getHost()20 获取此 URL 的主机名(如果适用)。21 String getPath()22 获取此 URL 的路径部分。23 int getPort()24 获取此 URL 的端口号。25 String getProtocol()26 获取此 URL 的协议名称。27 String getQuery()28 获取此 URL 的查询部分。29

30 运行结果:31

32 D:\abc>java URLDemo33 getFile(): /myWeb/Demo.html?name=hahaha&name=2034 getHost(): 127.0.0.135 getPath(): /myWeb/Demo.html36 getPort(): 808037 getProtocol(): http38 getQuery(): name=hahaha&name=2039

40

41

42 */

View Code

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

1 import java.io.*;2 import java.net.*;3 classURLConnectionDemo4 {5 public static void main(String[] args) throwsException6 {7 URL url=new URL("http://127.0.0.1:8080/myWeb/Demo.html");8

9 URLConnection conn=url.openConnection();10 System.out.println(conn);11

12 InputStream in=conn.getInputStream();13 byte[] buf=new byte[1024];14 int len =in.read(buf);15 System.out.println(new String(buf,0,len));16 }17 }

View Code

09 小知识点

1.Socket类中有一个没有参数的构造方法,这个构造方法一般和connect方法一起使用。

void connect(SocketAddress endpoint)

connect方法的参数类型SocketAddress类是一个抽象类,它的直接子类InetSocketAddress和InetAddress类的区别就是:

InetAddress类中只封装ip地址,而InetSocketAddress封装了ip地址和端口号。

2.ServerSocket类有一个构造方法:ServerSocket(int port,int backlog),

其中backlog代表对连接请求的最大队列长度,即此服务器最多能连接的用户个数。

10 域名解析

1.因为ip地址不容易记忆,上网时常用主机名。

主机名翻译成ip地址,则需要域名解析(DNS)。比如上网时输入www.sina.com.cn,浏览器就先向公网请求DNS服务,

把主机名翻译成相应的ip地址,再把这个地址发回给浏览器。

2.http://127.0.0.1:8080/

http://localhost:8080/

上面这两个地址是等价的,其实127和localhost的映射关系就在本机。

在C:\Windows\System32\drivers\etc路径下的host文件,文件中有:

# localhost name resolution is handled within DNS itself.

#127.0.0.1 localhost

#::1 localhost

也可以修改文件让127.0.0.1对应别的名字。

上网时,先在本机找映射关系,找不到再用DNS。

可以利用这个特点避免恶意网站的侵扰。在此文件中把一些恶意网站同127.0.0.1建立映射关系就可以了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值