《网络编程技术》课程 实验指导书.doc_第1页
《网络编程技术》课程 实验指导书.doc_第2页
《网络编程技术》课程 实验指导书.doc_第3页
《网络编程技术》课程 实验指导书.doc_第4页
《网络编程技术》课程 实验指导书.doc_第5页
已阅读5页,还剩9页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

网络编程技术课程实验指导书上海大学通信与信息工程学院2002年9月目 录实验一 用户界面及多线程程序设计(2)实验二 网络通信程序设计(5)实验三 服务器端小程序设计(9)实验四 综合实例.(11)实验一 用户界面及多线程程序设计实验目的:1掌握java运行环境的配置2 理解事件响应机制3掌握java基本用户界面设计、多线程程序设计实验仪器:网络计算机,Jcreator pro 2.0 , JDK1.4,Windows2000实验项目及步骤:1 Java运行环境配置安软件提示安装Jcreator2.0和JDK1.4。在Windows2000环境中增加环境变量 .;c:jdk1.2libdt.jar;c:jdk1.2libtools.jar在Jcreator pro 2.0编辑下面源程序,验证环境配置是否正确。import java.awt.Graphics;public class HelloApplet extends java.applet.Appletpublic void init()resize(250,250);public void paint(Graphics g)g.drawString(Hello,50,100); 2 基本用户界面设计调试下面“计算器”程序,理解事件响应机制。import java.awt.*;import java.awt.event.*;import javax.swing.*;public class CalculatorTest extends JFrame implements ActionListenerJLabel label1=new JLabel(Please Input The First Num);JLabel label2=new JLabel(Please Input The Second Num);JLabel label3=new JLabel(The Result is);JTextField text1=new JTextField(15);JTextField text2=new JTextField(15);JTextField text3=new JTextField(15);JButton button1=new JButton(加);JButton button2=new JButton(减);JButton button3=new JButton(乘);JButton button4=new JButton(除);public CalculatorTest()super(calculatorTest);Container c=getContentPane();c.setLayout(new FlowLayout();c.add(label1);c.add(text1);c.add(label2);c.add(text2);c.add(button1);c.add(button2);c.add(button3);c.add(button4);c.add(label3);c.add(text3);button1.addActionListener(this);button2.addActionListener(this);button3.addActionListener(this);button4.addActionListener(this);addWindowListener(new WindowAdapter()public void windowClosing(WindowEvent e)System.exit(0););setSize(220,250);public void actionPerformed(ActionEvent e)float a1,a2,a3;String str1,str2;str1=text1.getText();a1=Float.valueOf(str1).floatValue();str2=text2.getText();a2=Float.valueOf(str2).floatValue();if(e.getSource()=button1)a3=a1+a2;text3.setText(str1+str2+=+String.valueOf(a3);if(e.getSource()=button2)a3=a1-a2;text3.setText(str1+-+str2+=+String.valueOf(a3);if(e.getSource()=button3)a3=a1*a2;text3.setText(str1+*+str2+=+String.valueOf(a3);if(e.getSource()=button4)a3=a1/a2;text3.setText(str1+/+str2+=+String.valueOf(a3);public static void main(String args)JFrame frame=new CalculatorTest();frame.show(); 3 多线程程序设计参考下面程序结构,利用多线程技术实现动态时钟。import java.applet.*;import java.awt.*;import java.util.*;public class RunnableDemo extends Applet implements RunnableThread clockThread;public void start()/启动多线程public void run()/多线程运行主体 public void paint(Graphics g)/重画功能实现 public void stop()/线程停止 实验二 网络通信程序设计实验目的:1掌握java的基本网络支持2 掌握Socket通信机制3结合多线程技术实现聊天室程序设计实验仪器:网络计算机,Jcreator pro 2.0 , JDK1.4,Windows2000实验项目及步骤:1 java的基本网络支持调试下面程序,观察运行结果,理解java网络资源获取的基本方法import .*;import java.io.*;class OpenStreamDemopublic static void main(String args)tryURL yahoo=new URL(http:/localhost:8080/);DataInputStream dis;String inputLine;dis=new DataInputStream(yahoo.openStream();while(inputLine=dis.readLine()!=null)System.out.println(inputLine);dis.close();catch(MalformedURLException me)System.out.println(MalformedURLException:+me);catch(IOException ioe)System.out.println(IOException:+ioe);2 基于Socket通信机制和多线程技术的聊天室程序设计下面为聊天室服务器端程序和客户机端程序,调试该程序,验证程序所具有的“聊天”功能,然后结合这段程序,通过增加线程的方法,使程序具有服务器端程序可以同时接纳2个以上客户登陆,并实现各客户机间的“聊天”功能。聊天室服务器端程序:import java.io.*;import java.awt.*;import java.awt.event.*;import .*;public class ChatServer extends Frame implements ActionListenerLabel label1=new Label(聊天);Panel panel=new Panel();TextField tf=new TextField(10);TextArea ta=new TextArea();ServerSocket server;Socket client;InputStream in;OutputStream out;public ChatServer()super(服务器);setSize(250,250);panel.add(label1);panel.add(tf);tf.addActionListener(this);add(North,panel);add(Center,ta);addWindowListener(new WindowAdapter()public void windowClosing(WindowEvent e)System.exit(0););show();tryserver=new ServerSocket(5000);client=server.accept();ta.append(已连接的客户机:+client.getInetAddress().getHostAddress()+nn);in=client.getInputStream();out=client.getOutputStream();catch(IOException ioe)while(true)trybyte buf=new byte256;in.read(buf);String str=new String(buf);ta.append(客户机说:+str);ta.append(n);catch(IOException e)public void actionPerformed(ActionEvent e)tryString str=tf.getText();byte buf=str.getBytes();tf.setText(null);out.write(buf);ta.append(我说:+str);ta.append(n);catch(IOException ioe)public static void main(String args)new ChatServer();聊天室客户机端程序:import java.io.*;import java.awt.*;import java.awt.event.*;import .*;public class ChatClient extends Frame implements ActionListenerLabel label1=new Label(聊天);Panel panel=new Panel();TextField tf=new TextField(10);TextArea ta=new TextArea();ServerSocket server;Socket client;InputStream in;OutputStream out;public ChatClient()super(客户机);setSize(250,250);panel.add(label1);panel.add(tf);tf.addActionListener(this);add(North,panel);add(Center,ta);addWindowListener(new WindowAdapter()public void windowClosing(WindowEvent e)System.exit(0););show();tryclient=new Socket(InetAddress.getLocalHost(),5000);ta.append(已连接的客户机:+client.getInetAddress().getHostAddress()+nn);in=client.getInputStream();out=client.getOutputStream();catch(IOException ioe)while(true)trybyte buf=new byte256;in.read(buf);String str=new String(buf);ta.append(服务器说:+str);ta.append(n);catch(IOException e)public void actionPerformed(ActionEvent e)tryString str=tf.getText();byte buf=str.getBytes();tf.setText(null);out.write(buf);ta.append(我说:+str);ta.append(n);catch(IOException ioe)public static void main(String args)new ChatClient();实验三 服务器端小程序设计实验目的:1虚拟服务器环境配置2 掌握基本的Servlet程序设计3完成基本的表单提交和处理程序设计实验仪器:网络计算机,Jcreator pro 2.0 , JDK1.4,Windows2000,JSWDK1.0.1实验项目及步骤:1 虚拟服务器环境配置根据JSWDK1.0.1的安装说明,配置虚拟服务器环境2 基本Servlet程序设计调试下面服务器端小应用程序,并根据配置的虚拟服务器环境发布这一Servlet程序,理解Servlet的运行与Applet运行过程的不同之处。import java.io.*;import javax.servlet.*;import javax.servlet.http.*;public class WelcomeServlet extends HttpServlet public void doGet( HttpServletRequest requset,HttpServletResponse response)throws IOException, ServletException response.setContentType(text/html);PrintWriter out = response.getWriter();out.println(+Hello +Hello, , welcome to Stars JSP site.+欢迎访问Star的主页+进入Star的主页+);3 表单提交和处理程序设计下面为一表单处理程序框架,根据这一框架完成程序设计,并完成相应的表单提交Html程序设计。import java.util.*;import javax.servlet.*;import javax.servlet.http.*;public class Survey extends HttpServlet public void doPost(HttpServletRequest req, HttpServletResponse res)throws ServletException, IOException/ 设置响应头部/得到响应的 PrintWriter以返回文本给客户端.try /打开一个文件写入Survey的结果./ 从客户端得到表单数据,存贮在文件中/关闭文件./ 返回感谢信息给客户端catch(IOException e) e.printStackTrace();toClient.println(A problem occured while recording your answers.+ Please try again.);/ 关闭writer; 响应完成.实验四 综合实例实验目的:1掌握JSP的基本程序设计2 掌握基于JDBC的数据库访问3完成简单网上书店程序设计实验仪器:网络计算机,Jcreator pro 2.0 , JDK1.4,Windows2000,JSWDK1.0.1实验项目及步骤:1 JSP的基本程序设计完成下面程序调试,体会JavaBean在程序设计中的运用修改前:产品 : 税率 : 修改后: 产品 : 税率 : /TaxRate.javaTaxRate.java:package tax;public class TaxRateString Product;double Rate;public TaxRate()this.Product = A001;this.Rate = 5;public void setProduct (String ProductName)this.Product = ProductName;public String getProduct()return(this.Product);public void setRate (double rateValue)this.Rate = rateValue;public double getRate()return (this.Rate);2 基于JDBC的数据库访问程序设计下面为基于JDBC的数据库访问程序,根据这一程序,利用Access建立相应的数据库文件,调试程序,观察运行结果。import java.sql.*; class testJDBC String sDBDriver = sun.jdbc.odbc.JdbcOdbcDriver; String sConnStr = jdbc:odbc:faq;/数据源名称faqConnection

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论