整理《网络编程技术》课程+实验指导书.docx
- 文档编号:16550290
- 上传时间:2023-07-14
- 格式:DOCX
- 页数:17
- 大小:20.14KB
整理《网络编程技术》课程+实验指导书.docx
《整理《网络编程技术》课程+实验指导书.docx》由会员分享,可在线阅读,更多相关《整理《网络编程技术》课程+实验指导书.docx(17页珍藏版)》请在冰点文库上搜索。
整理《网络编程技术》课程+实验指导书
《网络编程技术》课程实验指导书
上海大学通信与信息工程学院
2002年9月
实验一用户界面及多线程程序设计………………………………………
(2)
实验二网络通信程序设计…………………………………………………(5)
实验三服务器端小程序设计………………………………………………(9)
实验四综合实例………………………………………………..…………(11)
实验一用户界面及多线程程序设计
实验目的:
1掌握java运行环境的配置
2理解事件响应机制
3掌握java基本用户界面设计、多线程程序设计
实验仪器:
网络计算机,Jcreatorpro2.0,JDK1.4,Windows2000
实验项目及步骤:
1Java运行环境配置
安软件提示安装Jcreator2.0和JDK1.4。
在Windows2000环境中增加环境变量.;c:
\jdk1.2\lib\dt.jar;c:
\jdk1.2\lib\tools.jar
在Jcreatorpro2.0编辑下面源程序,验证环境配置是否正确。
importjava.awt.Graphics;
publicclassHelloAppletextendsjava.applet.Applet{
publicvoidinit(){
resize(250,250);
}
publicvoidpaint(Graphicsg){
g.drawString("Hello",50,100);
}
}
2基本用户界面设计
调试下面“计算器”程序,理解事件响应机制。
importjava.awt.*;
importjava.awt.event.*;
importjavax.swing.*;
publicclassCalculatorTestextendsJFrameimplementsActionListener{
JLabellabel1=newJLabel("PleaseInputTheFirstNum");
JLabellabel2=newJLabel("PleaseInputTheSecondNum");
JLabellabel3=newJLabel("TheResultis");
JTextFieldtext1=newJTextField(15);
JTextFieldtext2=newJTextField(15);
JTextFieldtext3=newJTextField(15);
JButtonbutton1=newJButton("加");
JButtonbutton2=newJButton("减");
JButtonbutton3=newJButton("乘");
JButtonbutton4=newJButton("除");
publicCalculatorTest(){
super("calculatorTest");
Containerc=getContentPane();
c.setLayout(newFlowLayout());
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(
newWindowAdapter(){
publicvoidwindowClosing(WindowEvente){
System.exit(0);
}
}
);
setSize(220,250);
}
publicvoidactionPerformed(ActionEvente){
floata1,a2,a3;
Stringstr1,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));
}
}
publicstaticvoidmain(Stringargs[]){
JFrameframe=newCalculatorTest();
frame.show();
}
}
3多线程程序设计
参考下面程序结构,利用多线程技术实现动态时钟。
importjava.applet.*;
importjava.awt.*;
importjava.util.*;
publicclassRunnableDemoextendsAppletimplementsRunnable{
ThreadclockThread;
publicvoidstart(){
//启动多线程
}
publicvoidrun(){
//多线程运行主体
}
publicvoidpaint(Graphicsg){
//重画功能实现
}
publicvoidstop(){
//线程停止
}
}
实验二网络通信程序设计
实验目的:
1掌握java的基本网络支持
2掌握Socket通信机制
3结合多线程技术实现聊天室程序设计
实验仪器:
网络计算机,Jcreatorpro2.0,JDK1.4,Windows2000
实验项目及步骤:
1java的基本网络支持
调试下面程序,观察运行结果,理解java网络资源获取的基本方法
import.*;
importjava.io.*;
classOpenStreamDemo{
publicstaticvoidmain(Stringargs[]){
try{URLyahoo=newURL("http:
//localhost:
8080/");
DataInputStreamdis;
StringinputLine;
dis=newDataInputStream(yahoo.openStream());
while((inputLine=dis.readLine())!
=null){
System.out.println(inputLine);
}
dis.close();
}catch(MalformedURLExceptionme){
System.out.println("MalformedURLException:
"+me);
}catch(IOExceptionioe){
System.out.println("IOException:
"+ioe);
}
}
}
2基于Socket通信机制和多线程技术的聊天室程序设计
下面为聊天室服务器端程序和客户机端程序,调试该程序,验证程序所具有的“聊天”功能,然后结合这段程序,通过增加线程的方法,使程序具有服务器端程序可以同时接纳2个以上客户登陆,并实现各客户机间的“聊天”功能。
聊天室服务器端程序:
importjava.io.*;
importjava.awt.*;
importjava.awt.event.*;
import.*;
publicclassChatServerextendsFrameimplementsActionListener{
Labellabel1=newLabel("聊天");
Panelpanel=newPanel();
TextFieldtf=newTextField(10);
TextAreata=newTextArea();
ServerSocketserver;
Socketclient;
InputStreamin;
OutputStreamout;
publicChatServer(){
super("服务器");
setSize(250,250);
panel.add(label1);
panel.add(tf);
tf.addActionListener(this);
add("North",panel);
add("Center",ta);
addWindowListener(newWindowAdapter(){
publicvoidwindowClosing(WindowEvente){
System.exit(0);}});
show();
try{
server=newServerSocket(5000);
client=server.accept();
ta.append("已连接的客户机:
"+client.getInetAddress().getHostAddress()+"\n\n");
in=client.getInputStream();
out=client.getOutputStream();
}
catch(IOExceptionioe){}
while(true){
try{
byte[]buf=newbyte[256];
in.read(buf);
Stringstr=newString(buf);
ta.append("客户机说:
"+str);
ta.append("\n");
}catch(IOExceptione){}
}
}
publicvoidactionPerformed(ActionEvente){
try{
Stringstr=tf.getText();
byte[]buf=str.getBytes();
tf.setText(null);
out.write(buf);
ta.append("我说:
"+str);
ta.append("\n");
}catch(IOExceptionioe){}
}
publicstaticvoidmain(Stringargs[]){
newChatServer();
}
}
聊天室客户机端程序:
importjava.io.*;
importjava.awt.*;
importjava.awt.event.*;
import.*;
publicclassChatClientextendsFrameimplementsActionListener{
Labellabel1=newLabel("聊天");
Panelpanel=newPanel();
TextFieldtf=newTextField(10);
TextAreata=newTextArea();
ServerSocketserver;
Socketclient;
InputStreamin;
OutputStreamout;
publicChatClient(){
super("客户机");
setSize(250,250);
panel.add(label1);
panel.add(tf);
tf.addActionListener(this);
add("North",panel);
add("Center",ta);
addWindowListener(newWindowAdapter(){
publicvoidwindowClosing(WindowEvente){
System.exit(0);}});
show();
try{
client=newSocket(InetAddress.getLocalHost(),5000);
ta.append("已连接的客户机:
"+client.getInetAddress().getHostAddress()+"\n\n");
in=client.getInputStream();
out=client.getOutputStream();
}
catch(IOExceptionioe){}
while(true){
try{
byte[]buf=newbyte[256];
in.read(buf);
Stringstr=newString(buf);
ta.append("服务器说:
"+str);
ta.append("\n");
}catch(IOExceptione){}
}
}
publicvoidactionPerformed(ActionEvente){
try{
Stringstr=tf.getText();
byte[]buf=str.getBytes();
tf.setText(null);
out.write(buf);
ta.append("我说:
"+str);
ta.append("\n");
}catch(IOExceptionioe){}
}
publicstaticvoidmain(Stringargs[]){
newChatClient();
}
}
实验三服务器端小程序设计
实验目的:
1虚拟服务器环境配置
2掌握基本的Servlet程序设计
3完成基本的表单提交和处理程序设计
实验仪器:
网络计算机,Jcreatorpro2.0,JDK1.4,Windows2000,JSWDK1.0.1
实验项目及步骤:
1虚拟服务器环境配置
根据JSWDK1.0.1的安装说明,配置虚拟服务器环境
2基本Servlet程序设计
调试下面服务器端小应用程序,并根据配置的虚拟服务器环境发布这一Servlet程序,理解Servlet的运行与Applet运行过程的不同之处。
importjava.io.*;
importjavax.servlet.*;
importjavax.servlet.http.*;
publicclassWelcomeServletextendsHttpServlet{
publicvoiddoGet(HttpServletRequestrequset,
HttpServletResponseresponse)
throwsIOException,ServletException{
response.setContentType("text/html");
PrintWriterout=response.getWriter();
out.println(""+"
"+""+"
"+"Hello,,welcometoStar'sJSPsite.
"+"
欢迎访问Star的主页
"+"
//localhost:
8080/star\">进入Star的主页
"+//localhost:
8080/star\">进入Star的主页
""+
"");
}
}
3表单提交和处理程序设计
下面为一表单处理程序框架,根据这一框架完成程序设计,并完成相应的表单提交Html程序设计。
importjava.util.*;
importjavax.servlet.*;
importjavax.servlet.http.*;
publicclassSurveyextendsHttpServlet{
publicvoiddoPost(HttpServletRequestreq,HttpServletResponseres)
throwsServletException,IOException
{
//设置响应头部
//得到响应的PrintWriter以返回文本给客户端.
try{
//打开一个文件写入Survey的结果.
//从客户端得到表单数据,存贮在文件中
}
//关闭文件.
//返回感谢信息给客户端
}catch(IOExceptione){
e.printStackTrace();
toClient.println("Aproblemoccuredwhilerecordingyouranswers. "
+"Pleasetryagain.");
}
//关闭writer;响应完成.
}
}
实验四综合实例
实验目的:
1掌握JSP的基本程序设计
2掌握基于JDBC的数据库访问
3完成简单网上书店程序设计
实验仪器:
网络计算机,Jcreatorpro2.0,JDK1.4,Windows2000,JSWDK1.0.1
实验项目及步骤:
1JSP的基本程序设计
完成下面程序调试,体会JavaBean在程序设计中的运用
<%@pagelanguage="java"%>
useBeanid="taxbean"scope="application"class="tax.TaxRate"/> 修改前: 产品: <%=taxbean.getProduct()%> 税率: <%=taxbean.getRate()%> <%taxbean.setProduct("A002"); taxbean.setRate(20.5); %> 修改后: 产品: <%=taxbean.getProduct()%> 税率: <%=taxbean.getRate()%> //TaxRate.java <%@pageimport="java.util.*"%> <%=(newjava.util.Date()).toLocaleString()%> TaxRate.java: packagetax; publicclassTaxRate{ StringProduct; doubleRate; publicTaxRate(){ this.Product="A001"; this.Rate=5; } publicvoidsetProduct(StringProductName){ this.Product=ProductName;} publicStringgetProduct(){ return(this.Product); } publicvoidsetRate(doublerateValue){ this.Rate=rateValue; } publicdoublegetRate(){ return(this.Rate); } } 2基于JDBC的数据库访问程序设计 下面为基于JDBC的数据库访问程序,根据这一程序,利用Access建立相应的数据库文件,调试程序,观察运行结果。 importjava.sql.*; classtestJDBC{ StringsDBDriver="sun.jdbc.odbc.JdbcOdbcDriver"; StringsConnStr="jdbc: odbc: faq";//数据源名称faq Connectionconn=null;ResultSetrs=null; publictestJDBC(){ try{Class.forName(sDBDriver);} catch(java.lang.ClassNotFoundExceptione){ System.err.println("testJDBC(): "+e.getMessage()); } } publicResultSetexecuteQuery(StringtestJDBC){ rs=null; try{conn=DriverManager.getConnection(sConnStr); Statementstmt=conn.createStatement(); rs=stmt.executeQuery(testJDBC); }catch(SQLExceptionex){ (3)环境影响评价中应用环境标准的原则。 System.err.println("aq.executeQuery: "+ex.getMessage()); } (2)规划实施中所采取的预防或者减轻不良环境影响的对策和措
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 网络编程技术 整理 网络 编程 技术 课程 实验 指导书