java : mozilla rhino js Test URL or Socket_JAVA_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > JAVA > java : mozilla rhino js Test URL or Socket

java : mozilla rhino js Test URL or Socket

 2018/9/8 10:00:20  belldeep  程序员俱乐部  我要评论(0)
  • 摘要:https://mozilla.github.io/rhino/下载rhino1_7R5.zip,解压后运行cmdcdD:\rhino\rhino1_7R5编写测试脚本test2.js//ImporttheSwingGUIcomponentsandafewotherclassesvarswingNames=newJavaImporter(javax.swing,javax.swing.event,javax.swing.border,java.awt,java.awt.event)
  • 标签:Java URL socket Mozilla JS
https://mozilla.github.io/rhino/ 下载 rhino1_7R5.zip ,解压后运行 cmd
cd D:\rhino\rhino1_7R5
编写测试脚本 test2.js
class="js">
// Import the Swing GUI components and a few other classes
var swingNames = new JavaImporter(javax.swing, javax.swing.event, javax.swing.border, java.awt,java.awt.event); 

importPackage(java.net);
importPackage(java.io);
importPackage(java.util);
importClass(java.lang.Thread);

with (swingNames) {
var font = new Font("宋体",Font.PLAIN,12);
var frame = new JFrame("Test URL or Socket "); 
//frame.setSize(600,400);
frame.setLocation(200,200);
var txtfield = new JTextField(30);               // txt entry field
txtfield.setFont(font);
txtfield.setText("http://www.baidu.com/");
var button1 = new JButton("send");            // Button to send message
var button2= new JButton("http"); 
var filechooser = new JFileChooser();            // A file selection dialog
var row = Box.createHorizontalBox();             // A box for field and button
var col = Box.createVerticalBox();               // For the row & progress bars
var padding = new EmptyBorder(3,3,3,3);      // Padding for rows
var texta = new JTextArea(10,30);
texta.setEditable(false);
texta.setLineWrap(true);
var sp = new JScrollPane(texta);
//sp.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
sp.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
// Put them all together and display the GUIm
row.add(txtfield);                               // Input field goes in the row
row.add(button1);                                 // Button goes in the row
row.add(button2); 
col.add(row);                                    // Row goes in the column
col.add(sp);
frame.add(col);                                  // Column goes in the frame
row.setBorder(padding);                      // Add some padding to the row
frame.pack();                                    // Set to minimum size
frame.visible = true;                            // Make the window visible
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

// KeyEvent: ENTER
txtfield.addActionListener(function() {
    var txt = txtfield.getText();
    texta.append(txt+"\n");
    txtfield.setText("");
});
// Test Socket
button1.addActionListener(function() {
    texta.setText(""); // Clear
    var txt = txtfield.getText();
    if (! txt.startsWith("http")){
        texta.append(txt+"\n");
        new java.lang.Thread(function(){ send("127.0.0.1",12345,txt);}).start();
        txtfield.setText("");
    } else {
        texta.append("this is http, can't use socket.\n");
    }
});
// Test URL http:
button2.addActionListener(function() {
    texta.setText(""); // Clear
    var txt = txtfield.text;
    if (txt.startsWith("http:") || txt.startsWith("https:")){ 
        try {
            var url = new URL(txt.trim());
            new java.lang.Thread(function() { connect(url);}).start();
        } catch(e){
            texta.append(e.message);
        }
    } else {
        texta.append("input is not http://");
    }
});
// 连接
function connect(url){
	var conn = url.openConnection();
    conn.setConnectTimeout(5000);
	conn.connect();
	var input = new Scanner(conn.getInputStream(),"UTF-8");
	var headers = conn.getHeaderFields();
    print(typeof headers);
    print(Array.isArray(headers.entrySet()));
    var str = headers.entrySet().toString();
    texta.append(str.replace("], ","],\n")+"\n");
/*
	while (input.hasNextLine()) {
		texta.append(input.nextLine()+"\n");
    }
*/
}
// Socket
function send(host,port,msg){
    try {
        var socket = new java.net.Socket(host,port);
        var message = msg.trim();
        var writer = new java.io.PrintWriter(socket.getOutputStream(),true);
        writer.println(message);
        try {
            var ins = new java.util.Scanner(socket.getInputStream());
            while( ins.hasNextLine()){
                texta.append(ins.nextLine()+"\n");
            }
        } finally {
            socket.close();
        }
    } catch(ex){
        texta.append(ex.message);
    }
}
}

运行 java -jar js.jar test2.js
或者 rhino.bat test2.js
上一篇: Java Class文件结构实例分析(上) 下一篇: 没有下一篇了!
发表评论
用户名: 匿名