




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
简单web服务器设计和实现设计内容及设计要求
WWW工作基于用户机/服务器计算模型,由Web浏览器(用户机)和Web服务器(服务器)组成,二者之间采取超文本传送协议(HTTP)进行通信,HTTP协议作用原理包含四个步骤:连接,请求,应答,关闭应答。设计内容Web服务器是web中关键部件,设计一个简单Web服务器,为用户提供简单信息服务。设计要求本试验要求完成一个简单web服务器设计和实现,能够经过HTTPGet命令取得一个简单HTML文件。设计目标经过web服务器设计和实现,能够达成以下目标:掌握网络编程知识和技能;掌握HTTP协议相关知识;熟悉网络软件开发过程,锻炼处理实际问题能力。总体设计HTTP协议作用原理包含四个步骤:连接:Web浏览器和Web服务器建立连接,打开一个称为socket(套接字)虚拟文件,此文件建立标志着连接建立成功。请求:Web浏览器经过socket向Web服务器提交请求。HTTP请求通常是GET或POST命令(POST用于FORM参数传输)。GET命令格式为:GET路径/文件名HTTP/1.0文件名指出所访问文件,HTTP/1.0指出Web浏览器使用HTTP版本。应答:Web浏览器提交请求后,经过HTTP协议传送给Web服务器。Web服务器接到后,进行事务处理,处理结果又经过HTTP传回给Web浏览器,从而在Web浏览器上显示出所请求页面。关闭连接:当应答结束后,Web浏览器和Web服务器必需断开,以确保其它Web浏览器能够和Web服务器建立连接。用户端采取是浏览器,整个系统需要设计是服务器服务器用例描述服务器实现功效能够描述为3个用例许可建立连接提供服务许可断开连接服务器活动图整个工作步骤以下具体设计及代码服务器对象静态关系服务器对象静态关系服务器类设计publicclassMain开启程序publicclassFrameextendsJFrameimplementsActionListener实现系统管理员查看服务器状态publicclassNet实现许可建立连接,提供服务,许可断开连接代码publicclassMain开启程序packageserver;//http://ylx-PC:1111/index.htmlpublicclassMain{//接口实现publicstaticvoidmain(String[]args){//开启程序newFrame();//建立窗体}}publicclassFrameextendsJFrameimplementsActionListener实现系统管理员查看服务器状态packageserver;importjava.awt.*;importjava.awt.event.*;importjava.util.Date;importjavax.swing.*;publicclassFrameextendsJFrameimplementsActionListener{ //建立窗体组件JPanelbtnPan=newJPanel(newFlowLayout(FlowLayout.CENTER)); privateJLabellistenPortLb=newJLabel("设置监听端口:"); privateJLabeltishi=newJLabel("Webserver信息:"); privateJLabeltian=newJLabel(""); privateJTextFieldportTf=newJTextField("8080",4);//设置默认端口号 JButtoncontrolBtn=newJButton("开始");//设置按钮名称 JButtonclearHintBtn=newJButton("清空"); privateJTextAreahintTa=newJTextArea("动态监测服务信息\n"); privateNetnet; publicFrame(){ initComponent();//初始化界面 net=newNet(this); } privatevoidinitComponent(){ //初始化全部界面组件 buildBtnPan(); buildHintPan(); setSize(600,450);//设置窗体大小 Toolkittool=Toolkit.getDefaultToolkit(); setLocation((tool.getScreenSize().width-this.getSize().width)/2, (tool.getScreenSize().height-this.getSize().height)/2 ); //设置窗体位置 this.addWindowListener(newWindowAdapter(){ publicvoidwindowClosing(WindowEventarg0){ Frame.this.setVisible(true); Frame.this.dispose(); } }); setTitle("一个简单Web服务器"); setDefaultCloseOperation(EXIT_ON_CLOSE); setVisible(true); } //构建按钮面板 publicvoidbuildBtnPan(){//实现窗体布局 JLabeltc=newJLabel(); BoxboxV1=Box.createHorizontalBox(); BoxboxV2=Box.createHorizontalBox(); Boxbox=Box.createVerticalBox(); boxV1.add(listenPortLb); boxV1.add(Box.createHorizontalStrut(10)); boxV1.add(portTf); boxV1.add(Box.createHorizontalStrut(10)); boxV1.add(controlBtn); boxV2.add(tishi); boxV2.add(Box.createHorizontalStrut(10)); boxV2.add(tian); boxV2.add(Box.createHorizontalStrut(10)); boxV2.add(clearHintBtn); box.add(boxV1); box.add(Box.createVerticalStrut(8)); box.add(boxV2); btnPan.add(box); //加入全部按键 add(btnPan,BorderLayout.NORTH);//将按键放置在北部面板 portTf.requestFocus(); portTf.selectAll(); controlBtn.addActionListener(this); clearHintBtn.addActionListener(this); } publicvoidbuildHintPan(){ hintTa.setEditable(false); JScrollPanescrollPane=newJScrollPane();//添加滑动条 scrollPane.setBorder(BorderFactory.createLoweredBevelBorder()); scrollPane.getViewport().add(hintTa); add(scrollPane,BorderLayout.CENTER);//置在中部面板 } publicvoidactionPerformed(ActionEventarg0){//各个出现问题监测 Objectsource=arg0.getSource(); //判定按钮提醒文字内容 if(source==clearHintBtn){ hintTa.setText(""); return; } Stringmsg=controlBtn.getText(); if(msg.equals("开始")){ controlBtn.setText("停止"); tian.setText("服务器开启接口"+portTf.getText()); portTf.setEditable(false); net.start(portTf.getText()); hintTa.setText(hintTa.getText()+"\n---------------------------"+ "-开启服务器"+newDate().toLocaleString()+"---------------------------------\n\n"); }else{ tian.setText("关闭服务器接口"+portTf.getText()); controlBtn.setText("开始"); portTf.setEditable(true); net.stop(); } } publicvoidaddHint(Strings){ hintTa.setText(hintTa.getText()+s); }}publicclassNet实现许可建立连接,提供服务,许可断开连接packageserver;importjava.io.*;import.*;importjava.util.*;importjavax.swing.*;publicclassNet{//实现许可建立连接,提供服务,许可断开连接 inti=0; privateFrameframe; privateServerSocketserverSocket; publicNet(Frameframe){//将WebServerFrame和WebServerNet连接起来 this.frame=frame; } //点击“开始”按钮出现结果 publicvoidstart(Stringport){ //对异常进行处理 try{ serverSocket=newServerSocket(Integer.parseInt(port)); newThread(){ //对于线程重写run() publicvoidrun(){ try{ while(true){ Socketsocket=serverSocket.accept();//许可和服务器连接 newHandlerThread(socket).start();//开启线程 } }catch(Exceptione){ JOptionPane.showMessageDialog(frame,e.getMessage()); } } }.start(); }catch(Exceptionex){ JOptionPane.showMessageDialog(frame,ex.getMessage()); } } publicvoidstop(){ try{ if(serverSocket!=null) serverSocket.close(); }catch(Exceptione){ }finally{ frame.addHint("\n---------------------------"+ "-关闭服务器"+newDate().toLocaleString()+"--------------------------------\n"); } } //增加新线程 classHandlerThreadextendsThread{ privateSocketsocket; privateStringhostName; publicHandlerThread(Socketsocket){ this.socket=socket; this.hostName=socket.getInetAddress().getHostAddress(); i++; frame.addHint(""+i+""+"主机"+hostName+"连接成功"); } //重写run() publicvoidrun(){ BufferedReaderin=null; //浏览器和服务器间交互输入流 PrintStreamout=null; //浏览器和服务器间交互输出流 BufferedInputStreamfin=null;//服务器端文件输入字符流 try{ in=newBufferedReader(newInputStreamReader(socket.getInputStream())); out=newPrintStream(socket.getOutputStream()); //从浏览器提交请求头部中取得想访问文件名称 Stringsrc=in.readLine().split("")[1]; //将浏览器想取得文件名称输出至提醒框 frame.addHint("取得服务"+"时间"+newDate().toLocaleString()+"\n"); //对浏览器想取得文件名称进行去除?后面内容处理 //示例:/index.html?a=b->/index.html intindex=src.indexOf("?"); if(index>=0)src.substring(0,index); //假如浏览器没指定访问页面,则返回index.html页面内容 if("/".equals(src))src="/index.html"; FilesrcFile=newFile("html"+src); //假如浏览器访问页面不存在,则返回404.html页面内容 if(!srcFile.exists()||!srcFile.canRead()) srcFile=newFile("html/404.html"); //输出响应头部信息 out.println("HTTP/1.1200OK"); out.println("服务器:IISWeb服务器V01"); out.println("最终修改时间:"+newDate(srcFile.lastModified())); out.println("文件总字节:"+getContentType(srcFile)); out.println("文件总长度:"+srcFile.length()); out.println("时间:"+newDate()); out.println(); //输出响应体部信息 fin=newBufferedInputStream(newFileInputStream(srcFile)); byte[]buffer=newbyte[1024*8]; inti=-1; while((i=fin.read(buffer))!=-1){ out.write(buffer,0,i); } }catch(Exceptione){ e.printStackTrace(); }finally{ try{ if(in!=null)in.close(); if(out!=null)out.close(); if(fin!=null)fin.close(); } catch(Exceptione){} } } } //文件接收 publicStringgetContentType(Filefile){ StringfileName=file.getName(); Stringtype=fileName.substring(fileName.indexOf(".")); Stringresult="空"; if(".gif".equals(type))result="image/gif"; elseif(".html".equals(type))result="text/html"; elseif(".htm".equals(type))result="text/html"; elseif(".txt".equals(type))result="text/plain"; elseif(".xml".equals(type))result="text/xml"; returnresult; }}调试及运行结果开启用exe
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 保证优先供货协议合同范例
- 产业城快递转让合同范本
- 股权抵押借款合同模板转让合同
- 上海废弃厂房租赁合同标准文本
- 标准规范的建筑工程合同范本实例
- 个人挂靠证件合同标准文本
- 与公司协议合同标准文本
- 个人自建包工合同标准文本
- 伴热带安装合同范例
- 生态修复工程苗木采购合同样本
- 1 热工测量基础知识
- 肺癌肿瘤标志物检测与临床应用
- Unit+4+Amazing+Art+Developing+ideas+-高中英语外研版(2019)必修第三册
- 物业公司章程模板
- 基于主成分-聚类分析的各地区火灾事故研究(附有SAS程序)
- 火龙罐技术课件
- 精神科症状学理论知识考核试题
- 石膏粉生产线设备及工艺介绍
- 河湖水系连通演变过程
- 财务审计投标方案(完整技术标)
- 电镀产品检验记录
评论
0/150
提交评论