實現Servlet容器(Servlet容器根據web.xml文件調用對應的Servlet類) Servlet(狹義解釋是Servlet介面,但更常見的解釋是實現Servlet的所有類) 介紹:Servlet容器是用來管理Servlet, 過程:瀏覽器輸入URL地址,伺服器接收到這個地址,根據瀏覽器的 ...
實現Servlet容器(Servlet容器根據web.xml文件調用對應的Servlet類)
Servlet(狹義解釋是Servlet介面,但更常見的解釋是實現Servlet的所有類)
介紹:Servlet容器是用來管理Servlet,
過程:瀏覽器輸入URL地址,伺服器接收到這個地址,根據瀏覽器的訪問方式,響應消息發送給瀏覽器,併在瀏覽器上顯示。
Servlet的工作過程:
1.瀏覽器提交請求(通常是URL或超鏈接或表單提交)
2.伺服器接收到請求後,判斷請求的Servlet如果是第一次請求就實例化一個Servlet
3.調用Servlet的service()方法
4.伺服器把產生的響應內容發送給客戶端
5.瀏覽器根據自己的解析方式對響應內容進行解析
代碼實現:
編寫各類的功能:
HttpServletRequest解析URL,得到請求方式和地址等信息
HttpServletResponse封裝Http響應消息
HttpServlet抽象類,doService()方法根據請求的方式執行相應的子類方法
Service類有主函數,伺服器Socket設置埠號,接收客戶端的socket對象,這裡讀取一個屬性文件web.properties來模擬配置文件Web.xml,實現URL到類的映射
MyServlet和MyServlet2繼承自HttpServlet,相應的方法把html通過httpServletResponse發送給瀏覽器
1 package com.wj; 2 3 import java.io.BufferedReader; 4 import java.io.IOException; 5 import java.io.InputStreamReader; 6 import java.net.Socket; 7 8 /* 9 * 解析瀏覽器的請求,得到請求的方式和地址等信息 10 */ 11 public class HttpServletRequest { 12 13 private Socket socket; 14 private String[] items; 15 public HttpServletRequest(Socket socket) throws IOException { 16 this.socket = socket; 17 BufferedReader br=new BufferedReader(new InputStreamReader(socket.getInputStream())); 18 String cmd=br.readLine(); 19 items=cmd.split(" "); 20 21 } 22 public String getMethod(){ 23 return items[0]; 24 } 25 public String getURI(){ 26 return items[1]; 27 } 28 29 } 30 package com.wj; 31 32 import java.io.IOException; 33 import java.io.OutputStream; 34 import java.io.PrintWriter; 35 import java.net.Socket; 36 37 public class HttpServletResponse { 38 39 private Socket socket; 40 public HttpServletResponse(Socket socket){ 41 this.socket=socket; 42 } 43 public OutputStream getOutputStream() throws IOException{ 44 return socket.getOutputStream(); 45 } 46 public PrintWriter getWriter() throws IOException{ 47 return new PrintWriter(this.socket.getOutputStream()); 48 } 49 } 50 package com.wj; 51 52 import java.io.IOException; 53 54 55 public abstract class HttpServlet{ 56 57 58 public abstract void doPost(HttpServletRequest request,HttpServletResponse response) throws IOException; 59 60 public abstract void doGet(HttpServletRequest request,HttpServletResponse response)throws IOException; 61 62 public void doService(HttpServletRequest req, HttpServletResponse res) 63 throws IOException { 64 String me=req.getMethod(); 65 if(me.equalsIgnoreCase("get")){ 66 doGet(req, res); 67 }else{ 68 doPost(req, res); 69 } 70 } 71 } 72 package com.wj; 73 74 import java.io.BufferedReader; 75 import java.io.FileInputStream; 76 import java.io.IOException; 77 import java.net.ServerSocket; 78 import java.net.Socket; 79 import java.util.Properties; 80 81 public class Service { 82 public static void main(String[] args) throws IOException { 83 // TODO Auto-generated method stub 84 ServerSocket server = null; 85 BufferedReader br = null; 86 87 server = new ServerSocket(11111); 88 Socket socket = null; 89 while (true) {// loop 90 try { 91 socket = server.accept(); 92 HttpServletRequest request = new HttpServletRequest(socket); 93 HttpServletResponse response = new HttpServletResponse(socket); 94 Properties prop = new Properties(); 95 prop.load(new FileInputStream("src/web.properties")); //Java讀取配置文件信息 96 String className = prop.getProperty(request.getURI()); 97 HttpServlet servlet = (HttpServlet)Class.forName(className).newInstance(); 98 servlet.doService(request, response); 99 } catch (Exception ex) { 100 System.out.println(ex.getMessage()); 101 } finally { 102 if(socket != null){ 103 socket.close(); 104 } 105 } 106 } 107 } 108 109 } 110 package com.wj2; 111 112 import java.io.IOException; 113 import java.io.PrintWriter; 114 115 import com.wj.HttpServlet; 116 import com.wj.HttpServletRequest; 117 import com.wj.HttpServletResponse; 118 119 public class MyServlet extends HttpServlet{ 120 121 @Override 122 public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException { 123 // TODO Auto-generated method stub 124 String html = "<html><head><title>hello</title></head><body><h1>Hello</h1></body></html>"; 125 PrintWriter out=response.getWriter(); 126 out.print(html); 127 out.flush(); 128 out.close(); 129 130 } 131 132 @Override 133 public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException { 134 // TODO Auto-generated method stub 135 this.doGet(request, response); 136 } 137 138 139 } 140 package com.wj2; 141 142 import java.io.IOException; 143 import java.io.PrintWriter; 144 145 import com.wj.HttpServlet; 146 import com.wj.HttpServletRequest; 147 import com.wj.HttpServletResponse; 148 149 public class MyServlet2 extends HttpServlet{ 150 151 @Override 152 public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException { 153 // TODO Auto-generated method stub 154 String html = "<html><head><title>hello2</title></head><body><h1>Hello2</h1></body></html>"; 155 PrintWriter out=response.getWriter(); 156 out.println(html); 157 out.flush(); 158 out.close(); 159 160 } 161 162 @Override 163 public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException { 164 // TODO Auto-generated method stub 165 this.doGet(request, response); 166 } 167 168 169 }View Code
web.properties內容:(URL,映射到對應的Servlet類)
/testServlet/servlet/SvTest com.wj2.MyServlet
/testServlet/SvTest2 com.wj2.MyServlet2
Java工程目錄樹為:
瀏覽器可以和訪問Tomcat伺服器一樣,我們直接訪問:http://localhost:11111/testServlet/Servlet/SvTest
(ps:如果顯示localhost未發送任何數據的結果,可以換一個瀏覽器進行操作)