From 2c2bbd2c9509870b52b55e6e88f28ce88ba26897 Mon Sep 17 00:00:00 2001 From: xuxin <840198532@qq.com> Date: Sat, 20 Sep 2025 15:50:12 +0800 Subject: [PATCH] =?UTF-8?q?=E8=AF=B7=E6=B1=82-=E7=AE=80=E5=8D=95=E5=8F=82?= =?UTF-8?q?=E6=95=B0=E8=8E=B7=E5=8F=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- http-server-demo/pom.xml | 16 ++++ .../src/main/java/com/inmind/Server.java | 92 +++++++++++++++++++ .../src/main/resources/html/a.html | 39 ++++++++ .../main/resources/static/01. GET-POST.html | 23 +++++ springboot-web-req-resp/.gitignore | 33 +++++++ springboot-web-req-resp/pom.xml | 75 +++++++++++++++ .../SpringbootWebReqRespApplication.java | 13 +++ .../inmind/controller/RequestController.java | 33 +++++++ .../src/main/resources/static/index.html | 6 ++ .../SpringbootWebReqRespApplicationTests.java | 13 +++ 10 files changed, 343 insertions(+) create mode 100644 http-server-demo/pom.xml create mode 100644 http-server-demo/src/main/java/com/inmind/Server.java create mode 100644 http-server-demo/src/main/resources/html/a.html create mode 100644 springboot-web-quickstart/src/main/resources/static/01. GET-POST.html create mode 100644 springboot-web-req-resp/.gitignore create mode 100644 springboot-web-req-resp/pom.xml create mode 100644 springboot-web-req-resp/src/main/java/com/inmind/SpringbootWebReqRespApplication.java create mode 100644 springboot-web-req-resp/src/main/java/com/inmind/controller/RequestController.java create mode 100644 springboot-web-req-resp/src/main/resources/static/index.html create mode 100644 springboot-web-req-resp/src/test/java/com/inmind/SpringbootWebReqRespApplicationTests.java diff --git a/http-server-demo/pom.xml b/http-server-demo/pom.xml new file mode 100644 index 0000000..96bb233 --- /dev/null +++ b/http-server-demo/pom.xml @@ -0,0 +1,16 @@ + + + 4.0.0 + + com.itheima + http-server-demo + 1.0-SNAPSHOT + + + 11 + 11 + + + diff --git a/http-server-demo/src/main/java/com/inmind/Server.java b/http-server-demo/src/main/java/com/inmind/Server.java new file mode 100644 index 0000000..b8239e0 --- /dev/null +++ b/http-server-demo/src/main/java/com/inmind/Server.java @@ -0,0 +1,92 @@ +package com.inmind; + +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; + } + + public 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(); + } + } +} diff --git a/http-server-demo/src/main/resources/html/a.html b/http-server-demo/src/main/resources/html/a.html new file mode 100644 index 0000000..1e01e3d --- /dev/null +++ b/http-server-demo/src/main/resources/html/a.html @@ -0,0 +1,39 @@ + + + + + Title + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
序号品牌名称企业名称
010三只松鼠三只松鼠
009优衣库优衣库
008小米小米科技有限公司
+ + + + \ No newline at end of file diff --git a/springboot-web-quickstart/src/main/resources/static/01. GET-POST.html b/springboot-web-quickstart/src/main/resources/static/01. GET-POST.html new file mode 100644 index 0000000..627b312 --- /dev/null +++ b/springboot-web-quickstart/src/main/resources/static/01. GET-POST.html @@ -0,0 +1,23 @@ + + + + + 请求方式演示-GET-POST + + +
+ 姓 名:
+ 密 码:
+
+
+ +


+ +
+ 姓 名:
+ 密 码:
+
+
+ + + diff --git a/springboot-web-req-resp/.gitignore b/springboot-web-req-resp/.gitignore new file mode 100644 index 0000000..549e00a --- /dev/null +++ b/springboot-web-req-resp/.gitignore @@ -0,0 +1,33 @@ +HELP.md +target/ +!.mvn/wrapper/maven-wrapper.jar +!**/src/main/**/target/ +!**/src/test/**/target/ + +### STS ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache + +### IntelliJ IDEA ### +.idea +*.iws +*.iml +*.ipr + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ +build/ +!**/src/main/**/build/ +!**/src/test/**/build/ + +### VS Code ### +.vscode/ diff --git a/springboot-web-req-resp/pom.xml b/springboot-web-req-resp/pom.xml new file mode 100644 index 0000000..17a8443 --- /dev/null +++ b/springboot-web-req-resp/pom.xml @@ -0,0 +1,75 @@ + + + 4.0.0 + com.inmind + springboot-web-req-resp + 0.0.1-SNAPSHOT + springboot-web-req-resp + springboot-web-req-resp + + 11 + UTF-8 + UTF-8 + 2.7.6 + + + + + + org.springframework.boot + spring-boot-starter-web + + + + + org.springframework.boot + spring-boot-starter-test + test + + + + + + org.springframework.boot + spring-boot-dependencies + ${spring-boot.version} + pom + import + + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.8.1 + + 11 + 11 + UTF-8 + + + + org.springframework.boot + spring-boot-maven-plugin + ${spring-boot.version} + + com.inmind.SpringbootWebReqRespApplication + true + + + + repackage + + repackage + + + + + + + + diff --git a/springboot-web-req-resp/src/main/java/com/inmind/SpringbootWebReqRespApplication.java b/springboot-web-req-resp/src/main/java/com/inmind/SpringbootWebReqRespApplication.java new file mode 100644 index 0000000..08b0910 --- /dev/null +++ b/springboot-web-req-resp/src/main/java/com/inmind/SpringbootWebReqRespApplication.java @@ -0,0 +1,13 @@ +package com.inmind; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class SpringbootWebReqRespApplication { + + public static void main(String[] args) { + SpringApplication.run(SpringbootWebReqRespApplication.class, args); + } + +} diff --git a/springboot-web-req-resp/src/main/java/com/inmind/controller/RequestController.java b/springboot-web-req-resp/src/main/java/com/inmind/controller/RequestController.java new file mode 100644 index 0000000..15945ef --- /dev/null +++ b/springboot-web-req-resp/src/main/java/com/inmind/controller/RequestController.java @@ -0,0 +1,33 @@ +package com.inmind.controller; + +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RestController; + +import javax.servlet.http.HttpServletRequest; + +@RestController//当前类是请求处理类 +public class RequestController { + + /*//原始方式,接收简单参数 + @RequestMapping("/simpleParam") + public String simpleParam(HttpServletRequest request){ + String name = request.getParameter("name"); + String ageStr = request.getParameter("age"); + int age = Integer.parseInt(ageStr); + System.out.println(name +" "+ age); + return "ok"; + }*/ + + //springboot,接收简单参数 + /* + @RequestParam + 1.当请求参数与方法形参名不一致时,那使用该注解进行映射(name属性) + 2.required属性:默认是true,当前参数成为必须参数,可以修改为flase,当前参数可选参数 + */ + @RequestMapping("/simpleParam") + public String simpleParam(@RequestParam(name = "name",required = false) String username, Integer age){ + System.out.println(username +" "+ age); + return "ok"; + } +} diff --git a/springboot-web-req-resp/src/main/resources/static/index.html b/springboot-web-req-resp/src/main/resources/static/index.html new file mode 100644 index 0000000..e2d94a2 --- /dev/null +++ b/springboot-web-req-resp/src/main/resources/static/index.html @@ -0,0 +1,6 @@ + + +

hello word!!!

+

this is a html page

+ + \ No newline at end of file diff --git a/springboot-web-req-resp/src/test/java/com/inmind/SpringbootWebReqRespApplicationTests.java b/springboot-web-req-resp/src/test/java/com/inmind/SpringbootWebReqRespApplicationTests.java new file mode 100644 index 0000000..4b82e01 --- /dev/null +++ b/springboot-web-req-resp/src/test/java/com/inmind/SpringbootWebReqRespApplicationTests.java @@ -0,0 +1,13 @@ +package com.inmind; + +import org.junit.jupiter.api.Test; +import org.springframework.boot.test.context.SpringBootTest; + +@SpringBootTest +class SpringbootWebReqRespApplicationTests { + + @Test + void contextLoads() { + } + +}