您的位置:首页 > 理论基础 > 计算机网络

java网络编程之作业I

2008-05-29 00:21 501 查看
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++好学,虽然有不足之处,上面的程序是我个人写的,界面不怎么样,谁要是有兴趣,改一个不错.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: