文章目錄
- Web后端開發簡介
- SpringBootWeb入門
- HTTP協議
- HTTP-概述
- HTTP-請求協議
- HTTP-響應協議
- HTTP-協議解析
- Web服務器-Tomcat
- 簡介
- 基本使用
- SpringBootWeb入門程序解析
Web后端開發簡介
SpringBootWeb入門
package com.wuxuan.javaweb_wushuang.controller;import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;//請求處理類
@RestController
public class hello {@RequestMapping("/hello")public String hello(){System.out.println("Hello World~");return "Hello World~";}}
package com.wuxuan.javaweb_wushuang.controller;import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;//請求處理類
@RestController
public class hello {@RequestMapping("/hello")public String hello(){System.out.println("Hello World~");return "Hello World~";}}
HTTP協議
HTTP-概述
HTTP-請求協議
HTTP-響應協議
HTTP-協議解析
HTTP的解析和處理十分繁瑣,如果要寫代碼,要寫大量代碼!非常麻煩,而Web服務器為我們解決了這個問題。Web服務器可以為我們解析和處理HTTP,使我們只需關注業務邏輯的實現。
package com.wuxuan.javabase_homework;import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.nio.charset.StandardCharsets;/** 自定義web服務器*/
public class Server {public static void main(String[] args) throws IOException {ServerSocket ss = new ServerSocket(8080); // 監聽指定端口System.out.println("server is running...");while (true){Socket sock = ss.accept();System.out.println("connected from " + sock.getRemoteSocketAddress());//開啟線程處理請求Thread t = new Handler(sock);t.start();}}
}class Handler extends Thread {Socket sock;public Handler(Socket sock) {this.sock = sock;}@Overridepublic void run() {try (InputStream input = this.sock.getInputStream(); OutputStream output = this.sock.getOutputStream()) {handle(input, output);} catch (Exception e) {try {this.sock.close();} catch (IOException ioe) {}System.out.println("client disconnected.");}}private void handle(InputStream input, OutputStream output) throws IOException {BufferedReader reader = new BufferedReader(new InputStreamReader(input, StandardCharsets.UTF_8));BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(output, StandardCharsets.UTF_8));// 讀取HTTP請求:boolean requestOk = false;String first = reader.readLine();if (first.startsWith("GET / HTTP/1.")) {requestOk = true;}for (;;) {String header = reader.readLine();if (header.isEmpty()) { // 讀取到空行時, HTTP Header讀取完畢break;}System.out.println(header);}System.out.println(requestOk ? "Response OK" : "Response Error");if (!requestOk) {// 發送錯誤響應:writer.write("HTTP/1.0 404 Not Found\r\n");writer.write("Content-Length: 0\r\n");writer.write("\r\n");writer.flush();} else { // 發送成功響應://讀取html文件,轉換為字符串InputStream is = Server.class.getClassLoader().getResourceAsStream("html/a.html");BufferedReader br = new BufferedReader(new InputStreamReader(is));StringBuilder data = new StringBuilder();String line = null;while ((line = br.readLine()) != null){data.append(line);}br.close();int length = data.toString().getBytes(StandardCharsets.UTF_8).length;writer.write("HTTP/1.1 200 OK\r\n");writer.write("Connection: keep-alive\r\n");writer.write("Content-Type: text/html\r\n");writer.write("Content-Length: " + length + "\r\n");writer.write("\r\n"); // 空行標識Header和Body的分隔writer.write(data.toString());writer.flush();}}
}
Web服務器-Tomcat
簡介
基本使用