轉載地址:https://blog.csdn.net/u014795347/article/details/52328221?locationNum=2&fps=1html
如下代碼純屬本人複製,並且沒複製全,而且沒有進行手打,實在是不走心,在此貼上原文連接:java
https://blog.csdn.net/u014795347/article/details/52328221?locationNum=2&fps=1tomcat
並再次對原做者表示感謝,其中url切割的正則有些許問題,之後會從新書寫補充:多線程
說下思路:socket
什麼是容器?:我理解的就是對象實例化的載體,對象在容器中進行類加載與實例化,咱們就可以訪問內存中的對象啦~ide
實現方式?this
簡單來講就是:socket監聽+xml讀取標籤+反射實例化對象url
代碼以下:spa
package com.test.catalina.run; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.net.ServerSocket; import java.net.Socket; /** * 這裏只實現簡單的參數獲取與servlet類加載 * 更深層次能夠添加多線程,封裝renquest,reponse,解析配置文件等等 * @author Administrator這個地方原先是原做者的名字 * */ public class CatalinaContainer { private int port=8090; private String address="localhost"; private ServerSocket serverSocket; public static void main(String[] args) { try { new CatalinaContainer ().start(); } catch (IOException | InstantiationException e) { e.printStackTrace(); } } //開啓 public void start() throws IOException, InstantiationException{ serverSocket = new ServerSocket(port); while(true){ Socket socket = serverSocket.accept(); BufferedReader bReader = new BufferedReader(new InputStreamReader(socket.getInputStream())); String line = bReader.readLine(); if (!(null==line)) { ClintRequestBean requestBean = new ClintRequestBean(line); System.out.println("客戶端請求:"+requestBean.toReadString()); System.out.println("請求參數[路徑]:"+requestBean.getRequestParm().get("path")); System.out.println("請求參數[參數表]:"+requestBean.getRequestParm().get("attrs")); ClassLoader classLoader = ClassLoader.getSystemClassLoader(); try { classLoader.loadClass("cn.wwyxxmiemie.littletomcat.exclass.ExClass"); System.out.println("動態加載ExClass類--成功"); } catch (ClassNotFoundException e) { e.printStackTrace(); System.out.println("動態加載ExClass類--失敗"); } Class<?> exClass = null; try { exClass = Class.forName("cn.wwyxxmiemie.littletomcat.exclass.ExClass"); System.out.println("動態初始化ExClass類--成功"); } catch (ClassNotFoundException e) { e.printStackTrace(); System.out.println("動態初始化ExClass類--失敗"); } Method method; try { method = exClass.getMethod("test", null); System.out.println("獲得ExClass對象的"+method.getName()+"方法"); try { System.out.println("執行ExClass對象的"+method.getName()+"方法"); method.invoke(exClass.newInstance(), null); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } } catch (NoSuchMethodException e) { e.printStackTrace(); } catch (SecurityException e) { e.printStackTrace(); } } bReader.close(); socket.close(); } } }
package com.test.catalina.run; import java.util.HashMap; import java.util.Map; public class ClintRequestBean { //以一個請求舉例:http://127.0.0.1/www/qqq/eee private String protocol;//協議類型(eg:http) private String protocolVersion;//協議版本(eg:1.1) private String data;//請求數據(eg:/www/qqq/eee) private String method;//請求方法:(eg:GET) /** * 客戶端請求實體構造方法 * @param protocol 協議類型 (eg:http) * @param protocolVersion 協議版本 (eg:1.1) * @param data 請求數據 (eg:/www/qqq/eee)【必須以‘/’分隔】 * @param method 請求方法 (eg:GET) */ public ClintRequestBean(String protocol, String protocolVersion, String data, String method) { super(); this.protocol = protocol; this.protocolVersion = protocolVersion; this.data = data; this.method = method; } /** * 客戶端請求實體構造方法 * @param request 請求連接,通常針對一條完整的http連接 */ public ClintRequestBean(String request){ super(); String [] requestString = request.split(" "); this.method = requestString[0]; this.data = requestString[1]; String [] proAndVer = requestString[2].split("/"); this.protocol = proAndVer[0]; this.protocolVersion = proAndVer[1]; } /** * 轉化爲可讀String用於分析請求 * @return */ public String toReadString(){ return "ClintRequestBean [protocol=" + protocol + ", protocolVersion=" + protocolVersion + ", data=" + data + ", method=" + method + "]"; } /** * 獲得請求的參數 * @return map[請求路徑|參數map] */ public Map<String, Object> getRequestParm(){ Map<String,Object> map = new HashMap<>(); String [] parms =null; if(data.contains("\\?")){ parms = data.split("\\?"); map.put("path", parms[0]); } Map<String, String> attrs = new HashMap<>(); String[] kvs =null; if(data.contains("&")){ kvs =parms[1].split("&"); for (String string : kvs) { String [] kv = string.split("="); attrs.put(kv[0], kv[1]); } map.put("attrs", attrs); } return map; } public String getProtocol() { return protocol; } public void setProtocol(String protocol) { this.protocol = protocol; } public String getProtocolVersion() { return protocolVersion; } public void setProtocolVersion(String protocolVersion) { this.protocolVersion = protocolVersion; } public String getData() { return data; } public void setData(String data) { this.data = data; } public String getMethod() { return method; } public void setMethod(String method) { this.method = method; } @Override public String toString() { return this.method+" "+this.data+" "+this.protocol+"/"+this.protocolVersion; } }