文章目录
- 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
简介
基本使用