当前位置:文档之家› 课程设计 简单web服务器的设计与实现 uml设计

课程设计 简单web服务器的设计与实现 uml设计

简单web服务器的设计与实现一、设计内容及设计要求WWW的工作基于客户机/服务器计算模型,由Web 浏览器(客户机)和Web服务器(服务器)构成,两者之间采用超文本传送协议(HTTP)进行通信,HTTP协议的作用原理包括四个步骤:连接,请求,应答,关闭应答。

设计内容Web服务器是web中的关键部件,设计一个简单的Web服务器,为用户提供简单的信息服务。

设计要求本实验要求完成一个简单的web服务器的设计与实现,能够通过HTTP Get 命令获得一个简单的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个用例允许建立连接提供服务允许断开连接服务器的活动图整个工作流程如下三、详细设计及代码服务器对象的静态关系服务器对象的静态关系服务器类的设计public class Main 启动程序public class Frame extends JFrame implements ActionListener 实现系统管理员查看服务器状态public class Net 实现允许建立连接,提供服务,允许断开连接代码public class Main 启动程序package server;// http://ylx-PC:1111/index.htmlpublic class Main {//接口的实现public static void main(String[] args) { //启动程序new Frame(); //建立窗体}}public class Frame extends JFrame implements ActionListener 实现系统管理员查看服务器状态package server;import java.awt.*;import java.awt.event.*;import java.util.Date;import javax.swing.*;public class Frame extends JFrame implements ActionListener {// 建立窗体组件JPanel btnPan = new JPanel(newFlowLayout(FlowLayout.CENTER));private JLabel listenPortLb = new JLabel("设置监听端口: ");private JLabel tishi = new JLabel("Webserver信息:");private JLabel tian = new JLabel("");private JTextField portTf = new JTextField("8080", 4);//设置默认端口号JButton controlBtn = new JButton("开始");//设置按钮名称JButton clearHintBtn = new JButton("清空");private JTextArea hintTa= new JTextArea("动态监测服务信息\n");private Net net;public Frame() {initComponent(); //初始化界面net = new Net(this);}private void initComponent() { //初始化所有界面组件buildBtnPan();buildHintPan();setSize(600, 450);//设置窗体大小Toolkit tool = Toolkit.getDefaultToolkit();setLocation((tool.getScreenSize().width -this.getSize().width) / 2,(tool.getScreenSize().height -this.getSize().height) / 2); //设置窗体位置this.addWindowListener(new WindowAdapter(){public void windowClosing(WindowEvent arg0) {Frame.this.setVisible(true);Frame.this.dispose();}});setTitle("一个简单的 Web服务器");setDefaultCloseOperation(EXIT_ON_CLOSE);setVisible(true);}//构建按钮面板public void buildBtnPan() { //实现窗体布局JLabel tc= new JLabel();Box boxV1=Box.createHorizontalBox();Box boxV2=Box.createHorizontalBox();Box box=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);}public void buildHintPan() {hintTa.setEditable(false);JScrollPane scrollPane = new JScrollPane(); //添加滑动条scrollPane.setBorder(BorderFactory.createLoweredBevelBorde r());scrollPane.getViewport().add(hintTa);add(scrollPane, BorderLayout.CENTER); //置在中部面板}public void actionPerformed(ActionEvent arg0) { //各个出现的问题监测Object source = arg0.getSource();//判断按钮提示文字内容if (source == clearHintBtn) {hintTa.setText("");return;}String msg = 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();}}public void addHint(String s) {hintTa.setText(hintTa.getText() + s);}}public class Net 实现允许建立连接,提供服务,允许断开连接package server;import java.io.*;import .*;import java.util.*;import javax.swing.*;public class Net { //实现允许建立连接,提供服务,允许断开连接int i=0;private Frame frame;private ServerSocket serverSocket;public Net(Frame frame) {//将WebServerFrame 和WebServerNet连接起来this.frame=frame;}//点击“开始”按钮出现的结果public void start(String port) {//对异常进行处理try {serverSocket=new ServerSocket(Integer.parseInt(port));new Thread(){//对于线程重写run()public void run() {try{while(true) {Socket socket=serverSocket.accept();//允许与服务器连接new HandlerThread(socket).start();//开启线程}}catch (Exception e) {JOptionPane.showMessageDialog(frame, e.getMessage());}}}.start();} catch (Exception ex) {JOptionPane.showMessageDialog(frame, ex.getMessage());}}public void stop() {try{if(serverSocket!=null)serverSocket.close();}catch(Exception e) {}finally{frame.addHint("\n---------------------------" +"-关闭服务器 "+newDate().toLocaleString()+"--------------------------------\n");}}//增加新的线程class HandlerThread extends Thread {private Socket socket;private String hostName;public HandlerThread(Socket socket) {this.socket=socket;this.hostName=socket.getInetAddress().getHostAddress();i++;frame.addHint(" "+i+" "+"主机 "+hostName+" 连接成功");}//重写 run()public void run() {BufferedReader in=null; //浏览器和服务器间交互的输入流PrintStream out=null; //浏览器和服务器间交互的输出流BufferedInputStream fin=null; //服务器端文件的输入字符流try{in=new BufferedReader(newInputStreamReader(socket.getInputStream()));out=new PrintStream(socket.getOutputStream());//从浏览器提交的请求头部中获得想访问的文件名称String src=in.readLine().split(" ")[1];//将浏览器想获得的文件名称输出至提示框frame.addHint("获得服务 "+" 时间"+newDate().toLocaleString()+"\n");//对浏览器想获得的文件名称进行去除?后面内容处理//示例: /index.html?a=b -> /index.htmlint index=src.indexOf("?");if(index>=0)src.substring(0,index);//如果浏览器没指定访问的页面,则返回 index.html 页面内容if("/".equals(src)) src="/index.html";File srcFile=new File("html"+src);//如果浏览器访问的页面不存在,则返回 404.html 页面内容if(!srcFile.exists()||!srcFile.canRead())srcFile=new File("html/404.html");//输出响应头部信息out.println("HTTP/1.1 200 OK");out.println("服务器: IIS Web服务器 V01");out.println("最后修改时间: "+newDate(stModified()));out.println("文件总字节:"+getContentType(srcFile));out.println("文件总长度: "+srcFile.length());out.println("时间: "+new Date());out.println();//输出响应体部信息fin=new BufferedInputStream(newFileInputStream(srcFile));byte[] buffer=new byte[1024*8];int i=-1;while((i=fin.read(buffer))!=-1) {out.write(buffer,0,i);}}catch(Exception e) {e.printStackTrace();}finally{try{if(in!=null)in.close();if(out!=null)out.close();if(fin!=null)fin.close();}catch(Exception e){}}}}//文件的接收public String getContentType(File file) {String fileName=file.getName();String type=fileName.substring(fileName.indexOf("."));String result="空";if(".gif".equals(type)) result="image/gif";else if(".html".equals(type)) result="text/html";else if(".htm".equals(type)) result="text/html";else if(".txt".equals(type)) result="text/plain";else if(".xml".equals(type)) result="text/xml";return result;}}四、调试及运行结果启动用exe4j生成的myserver.exe应用程序初始化如图设置端口8080,单击开始开启服务器打开浏览器地址栏输入http://ylx-pc:8080/得到结果如图服务器端结果单击停止,暂停服务器服务五、总结通过此次实验,我基本了解了Web服务器的设计和工作原理,掌握了一些JAVA网络编程方法,更加深了我对书本知识的理解:Web服务器与客户端的通信使用HTTP协议(超文本传输协议),所以也叫做HTTP服务器。

相关主题