java web 動態伺服器

來源:互聯網
上載者:User

 

 

 

 

 

 

寫了一個java web 動態伺服器,主要通過內部類來實現,動態類使用了外部類,採用了

 

classforname 執行個體化,動態類的構造方法不能帶參數, 效果都出來了,分享給有需要的

 

朋友。判斷做的不夠多,

 

寫得不夠好,感謝指出缺點。以下是代碼:

 

靜態檔案index.html:

 

<html>                     <head>                              <title>簡單的web伺服器</title>                     </head>                     <body>                               <div align="center">簡單的web靜態伺服器</div>                      </body>               </html>

 

 

 

主要核心代碼:

 

import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.PrintWriter; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.net.MalformedURLException; import java.net.ServerSocket; import java.net.Socket; import java.net.URL; import java.net.URLClassLoader; /**  * web處理類  */ class WebProcess{  private Request request;  private Response response;  public WebProcess(Request request, Response response) {   // TODO Auto-generated constructor stub   this.request=request;   this.response=response;   this.process();  }  private void process() {   // TODO Auto-generated method stub   String uri=request.getUri();   System.out.println("uri---------------------------"+uri);   String uriSuffix=uri.substring(uri.indexOf('.')+1);   System.out.println("uriSuffix------------"+uriSuffix);   if("htmlxmlhtm".indexOf(uriSuffix)>-1){    System.out.println("靜態檔案");    File fileToSend=new File(uri);    if(fileToSend.exists()){     System.out.println("存在檔案");     try {      FileInputStream fis=new FileInputStream(fileToSend);      byte[] data=new byte[fis.available()];      fis.read(data);      //System.out.println(data.length);      response.out(new String(data));     } catch (FileNotFoundException e) {      // TODO Auto-generated catch block      e.printStackTrace();     } catch (IOException e) {      // TODO Auto-generated catch block      e.printStackTrace();     }    }else{     System.out.println("不存在檔案");     response.out("您要訪問的網頁不存在");    }   }else{    System.out.println("動態檔案");    //Visit v=new Visit(response,request);    File fileToSend1=new File(uri);    System.out.println("fileToSend----------------"+fileToSend1);   /* if(fileToSend1.exists()){*/     //System.out.println("類檔案存在");     try {      //擷取需要的的類的路徑      URL url=new URL("file:\\D:\\Users\\Administrator\\Workspaces\\MyEclipse 8.6\\2013-08-21\\src");      String classname=uri;      System.out.println("url-----------------"+url);      ClassLoader cl=new URLClassLoader(new URL[]{url});      Class d=cl.loadClass(classname);      System.out.println(d);      //System.out.println("cl-----------------------"+cl);      Object c=Class.forName(uri).newInstance();      Method method=c.getClass().getMethod("process", Response.class,Request.class);      method.invoke(c,response,request);     } catch (MalformedURLException e) {      // TODO Auto-generated catch block      e.printStackTrace();     } catch (ClassNotFoundException e) {      // TODO Auto-generated catch block      e.printStackTrace();     } catch (InstantiationException e) {      // TODO Auto-generated catch block      e.printStackTrace();     } catch (IllegalAccessException e) {      // TODO Auto-generated catch block      e.printStackTrace();     } catch (SecurityException e) {      // TODO Auto-generated catch block      e.printStackTrace();     } catch (NoSuchMethodException e) {      // TODO Auto-generated catch block      e.printStackTrace();     } catch (IllegalArgumentException e) {      // TODO Auto-generated catch block      e.printStackTrace();     } catch (InvocationTargetException e) {      // TODO Auto-generated catch block      e.printStackTrace();     }    /*}else{     System.out.println("類檔案不存在");    }*/       }  }   } /**  * 響應類  */ class Response{  private PrintWriter writer;  public Response(OutputStream outputStream) {   // TODO Auto-generated constructor stub   this.writer=new PrintWriter(outputStream,true);   //this.out(data);  }  public void out(String data) {   // TODO Auto-generated method stub   System.out.println("out()-----------------start");   writer.println(data);     }   } /**  * 請求類  */ class Request{  private InputStream input;  public Request(InputStream inputStream) {   // TODO Auto-generated constructor stub   this.input=inputStream;   //this.getUri();  }  public String getUri(){   String resource=null;   System.out.println("getUri()-------------------start--");   BufferedReader in=new BufferedReader(new InputStreamReader(input));   try {    String line=in.readLine();    resource=line.substring(line.indexOf('/')+1, line.lastIndexOf('/')-5);    System.out.println("resource-----------"+resource);       } catch (IOException e) {    // TODO Auto-generated catch block    e.printStackTrace();   }   return resource;  }   } /**  * @author Administrator  *採用了線程  */ public class ServerWeb implements Runnable {  private static final int port = 8085;  ServerSocket serversocket;  /**   * @param args   */  public ServerWeb(){   new Thread(this).start();      //把當前類設定為一個線程  }    @Override  public void run() {   // TODO Auto-generated method stub   System.out.println("run()---------------------start-----");   try {    serversocket=new ServerSocket(port);    System.out.println("serversocket--------------------"+serversocket.getLocalPort());    while(true){     Socket socket=serversocket.accept();     System.out.println("socket----------------------"+socket.getLocalPort());     Request request=new Request(socket.getInputStream());     Response response=new Response(socket.getOutputStream());     WebProcess webprocess=new WebProcess(request,response);     socket.close();    }   } catch (IOException e) {    // TODO Auto-generated catch block    e.printStackTrace();   }  }        public static void main(String[] args) {   // TODO Auto-generated method stub   new ServerWeb();  }   }

  

獨立動態類的代碼:

import java.util.Date; public class Visit {  public Visit(/*Response response, Request request*/) {   // TODO Auto-generated constructor stub   /*this.request=request;   this.response=response;*/   //this.process();  }    public void process(Response response, Request request){   response.out("動態網頁"+new Date().toString());   System.out.println("process------------start");  } }

 

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.