黑马程序员-Java之自定义图形化界面的浏览器访问本地的Tomcat服务器

-------------android培训java培训、期待与您交流----------------------


package com.itheima;

import java.awt.*;
import java.awt.event.*;
import java.net.*;
import java.io.*;

public class MyIEByGUI {
    private Frame fm;
    private TextField tf;
    private TextArea ta;
    private Button bt;
    MyIEByGUI(){
        try {
            init();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    private void init() throws Exception{
        fm = new Frame("my Window");
        fm.setBounds(100, 100, 600, 400);
        fm.setLayout(new FlowLayout());
        
        tf = new TextField(60);
        
        bt = new Button(" 转到");
        
        ta = new TextArea(25, 70);
        
        fm.add(tf);
        fm.add(bt);
        fm.add(ta);
        myEvent();
        fm.setVisible(true);
    }
    
    public void myEvent() throws Exception{//创建各个组件的监听事件
        tf.addKeyListener(new KeyAdapter(){
            public void keyPressed(KeyEvent e){
                if(e.getKeyCode()==KeyEvent.VK_ENTER)
                    try {
                        showDir();
                    } catch (Exception e1) {
                        // TODO Auto-generated catch block
                        e1.printStackTrace();
                    }
            }
        });
        bt.addActionListener(new ActionListener() {
            
            @Override
            public void actionPerformed(ActionEvent e) {
                // TODO Auto-generated method stub
                try {
                    showDir();
                } catch (Exception e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
            }
        });
        fm.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e){
                System.exit(0);;
            }
        });
    }
    public void showDir() throws Exception{//显示服务器发送回来的信息到文本域框中
        ta.setText("");
        String url = tf.getText();
        int index1 = url.indexOf("//")+2;
        int index2 = url.indexOf("/", index1);
        String str = url.substring(index1, index2);
        String[] arr = str.split(":");
        String host = arr[0];
        int port = Integer.parseInt(arr[1]);
        String path = url.substring(index2);
        //ta.setText(str+"---"+path);
        
        Socket s = new Socket(host,port);//创建客户端Socket对象
         PrintWriter out = new PrintWriter(s.getOutputStream(),true);//创建字符流PrintWriter对象关联字节输出流
         out.println("GET /myweb/demo.html HTTP/1.1");//发送信息(get方式访问服务器)
         out.println("Accept:*/*");//接收所有类型的文件
         out.println("Accept-Language:zh-cn");//接收简体中文
         out.println("Host:192.168.1.254:8080");//发送到的主机地址和端口
         out.println("Connection:Keep-Alive");//连接状态:保持存活
         out.println("");
         out.println("");//空行用于和主体消息隔开
         BufferedReader buf = new BufferedReader(new InputStreamReader(s.getInputStream

        ()));///创建缓冲字符流关联字节输入流
         String line = null;//定义字符串变量用于接收一行输入字符流
         while((line=buf.readLine())!=null){//读取到的字符输入流不为空则打印信息
          ta.append(line+"\r\t");
         }
         s.close();
        
    }
    public static void main(String[] args){
        new MyIEByGUI();
    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值