java網絡編程之作業I

1.編寫圖形界面的Application程序,包含一個TextField和一個Label,TextField接受用戶的輸入的主機名,Label把這個主機的IP地址顯示出來.

import java.net.*; import java.awt.event.*; import java.awt.*; import javax.swing.*; public class scannerip implements ActionListener{     String host;     InetAddress address;     String ip;     JButton buttonScan = new JButton("掃描");     JLabel hostnameLabel = new JLabel("主機名稱");     JTextField hostTextField = new JTextField();     JLabel hostipLabel = new JLabel("主機IP是");     public scannerip() {     JDialog p1=new JDialog();      p1.setTitle("主機掃描小程序");      Container dialogPane=p1.getContentPane();      dialogPane.setLayout(new  GridLayout(2,2));      dialogPane.add(hostnameLabel);      dialogPane.add(hostTextField);      dialogPane.add(buttonScan);      dialogPane.add(hostipLabel);     p1.setBounds(250,250,400,100);     p1.setVisible(true);      buttonScan.addActionListener(this);     }   public void buttonScan_actionPerformed(ActionEvent e){         out("checking scan parameters...");         if (validParameters()) {             out("scanning...");             try {                address = InetAddress.getByName(host);                 out("主機IP是"+"(" + ip + ").");             } catch (Exception ex) {                 if (ex instanceof SecurityException)                     out(ex.getMessage());                 else                     out("未找到主機IP地址.");             }         }     }         private boolean validParameters() {             try {                 host = hostTextField.getText();                 address = InetAddress.getByName(host);                 ip = address.getHostAddress();             } catch (Exception e) {                 out(e.getMessage());                 return false;             }             return true;         }         private void out(String msg) {             hostipLabel.setText(msg);         }         public void actionPerformed(ActionEvent e) {             buttonScan_actionPerformed(e);         }      public static void main(String[] args)      {       new scannerip();       } }

 老師給的答案:

import java.awt.*; import javax.swing.*; import java.awt.event.*; import java.net.*; public class Ex15_1 extends WindowAdapter implements ActionListener {  JTextField tf;  JButton button;  JLabel label;  public Ex15_1(){   JFrame f=new JFrame("顯示IP地址");   Container contentPane=f.getContentPane();   contentPane.setLayout(new GridLayout(2,2));   tf=new JTextField("localhost");   button=new JButton("確定");   label=new JLabel();   contentPane.add(tf);   contentPane.add(button);   contentPane.add(label);   button.addActionListener(this);   f.addWindowListener(this);   f.setSize(300,100);   f.show();  }  public void actionPerformed(ActionEvent e){   String str=tf.getText().trim();   try{    InetAddress address=InetAddress.getByName(str);    String IPname=address.getHostAddress();    label.setText(IPname);   }   catch(UnknownHostException ex){     System.out.println("找不到主機IP.");    }   }    public void WindowClosing(WindowEvent e){     System.exit(0);    }        public static void main(String[] args){             new Ex15_1();        }  }

2.編寫一個JSP程序,實現刷新頁面就顯示增加一次訪問次數.

程序1:<html> <head><title>網頁訪問次數統計</title></head> <% int totalCount=0; Object countChange=application.getAttribute("change"); if(countChange!=null) {   String countTemp=(String)countChange;   totalCount=Integer.parseInt(countTemp); } totalCount=totalCount+1; String countString=String.valueOf(totalCount); Object countState=(Object)countString; application.setAttribute("change",countState); %> <body> <div align="center"> <h1>你是第<%=totalCount%>次訪問本頁面了.</h1> </div> </body> </html>

程序II:<html> <head> <title>網頁訪問次數統計</title> </head> <body> <% Integer count=(Integer)session.getAttribute("COUNT"); if ( count==null ) { count = new Integer(1); session.setAttribute("COUNT", count); } else { count = new Integer(count.intValue() + 1); session.setAttribute("COUNT", count); } out.println("您已經是第"+count+"次訪問本頁面了."); %> </body> </html>

 

 

這是我們的作業題,首先聲明一點,我本人對JAVA也蠻喜歡的,不過JAVA不是我的主修,呵呵,我是學C#的,當然了,要是有需要,可能會入主JAVA 這一個,必竟,JAVA很強大,但又比C++好學,雖然有不足之處,上面的程序是我個人寫的,界面不怎麼樣,誰要是有興趣,改一個不錯.

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章