java网络编程之作业I

本文提供两个Java编程实例:一是利用Swing创建一个图形界面应用程序,该程序能够接收用户输入的主机名并显示其对应的IP地址;二是通过JSP实现页面访问计数功能,每次页面刷新时更新并显示访问次数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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++好学,虽然有不足之处,上面的程序是我个人写的,界面不怎么样,谁要是有兴趣,改一个不错.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值