请求-简单参数获取
This commit is contained in:
16
http-server-demo/pom.xml
Normal file
16
http-server-demo/pom.xml
Normal file
@@ -0,0 +1,16 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<groupId>com.itheima</groupId>
|
||||
<artifactId>http-server-demo</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.source>11</maven.compiler.source>
|
||||
<maven.compiler.target>11</maven.compiler.target>
|
||||
</properties>
|
||||
|
||||
</project>
|
92
http-server-demo/src/main/java/com/inmind/Server.java
Normal file
92
http-server-demo/src/main/java/com/inmind/Server.java
Normal file
@@ -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();
|
||||
}
|
||||
}
|
||||
}
|
39
http-server-demo/src/main/resources/html/a.html
Normal file
39
http-server-demo/src/main/resources/html/a.html
Normal file
@@ -0,0 +1,39 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>Title</title>
|
||||
</head>
|
||||
<body>
|
||||
|
||||
<table border="1" cellspacing="0" width="500">
|
||||
<tr>
|
||||
<th>序号</th>
|
||||
<th>品牌名称</th>
|
||||
<th>企业名称</th>
|
||||
|
||||
</tr>
|
||||
<tr align="center">
|
||||
<td>010</td>
|
||||
<td>三只松鼠</td>
|
||||
<td>三只松鼠</td>
|
||||
</tr>
|
||||
|
||||
<tr align="center">
|
||||
<td>009</td>
|
||||
<td>优衣库</td>
|
||||
<td>优衣库</td>
|
||||
</tr>
|
||||
|
||||
<tr align="center">
|
||||
<td>008</td>
|
||||
<td>小米</td>
|
||||
<td>小米科技有限公司</td>
|
||||
</tr>
|
||||
|
||||
|
||||
</table>
|
||||
|
||||
|
||||
</body>
|
||||
</html>
|
@@ -0,0 +1,23 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>请求方式演示-GET-POST</title>
|
||||
</head>
|
||||
<body>
|
||||
<form action="" method="get">
|
||||
姓 名: <input type="text" name="name"> <br>
|
||||
密 码: <input type="password" name="password"><br>
|
||||
<input type="submit" value="提交表单GET"><br>
|
||||
</form>
|
||||
|
||||
<br><br><br>
|
||||
|
||||
<form action="" method="post">
|
||||
姓 名: <input type="text" name="name"> <br>
|
||||
密 码: <input type="password" name="password"><br>
|
||||
<input type="submit" value="提交表单POST"><br>
|
||||
</form>
|
||||
|
||||
</body>
|
||||
</html>
|
33
springboot-web-req-resp/.gitignore
vendored
Normal file
33
springboot-web-req-resp/.gitignore
vendored
Normal file
@@ -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/
|
75
springboot-web-req-resp/pom.xml
Normal file
75
springboot-web-req-resp/pom.xml
Normal file
@@ -0,0 +1,75 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>com.inmind</groupId>
|
||||
<artifactId>springboot-web-req-resp</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<name>springboot-web-req-resp</name>
|
||||
<description>springboot-web-req-resp</description>
|
||||
<properties>
|
||||
<java.version>11</java.version>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
|
||||
<spring-boot.version>2.7.6</spring-boot.version>
|
||||
</properties>
|
||||
<!--依赖管理-->
|
||||
<dependencies>
|
||||
<!--springboot-web起步依赖-->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!--springboot-单元测试起步依赖-->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
<dependencyManagement>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-dependencies</artifactId>
|
||||
<version>${spring-boot.version}</version>
|
||||
<type>pom</type>
|
||||
<scope>import</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</dependencyManagement>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>3.8.1</version>
|
||||
<configuration>
|
||||
<source>11</source>
|
||||
<target>11</target>
|
||||
<encoding>UTF-8</encoding>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
<version>${spring-boot.version}</version>
|
||||
<configuration>
|
||||
<mainClass>com.inmind.SpringbootWebReqRespApplication</mainClass>
|
||||
<skip>true</skip>
|
||||
</configuration>
|
||||
<executions>
|
||||
<execution>
|
||||
<id>repackage</id>
|
||||
<goals>
|
||||
<goal>repackage</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
</project>
|
@@ -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);
|
||||
}
|
||||
|
||||
}
|
@@ -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";
|
||||
}
|
||||
}
|
@@ -0,0 +1,6 @@
|
||||
<html>
|
||||
<body>
|
||||
<h1>hello word!!!</h1>
|
||||
<p>this is a html page</p>
|
||||
</body>
|
||||
</html>
|
@@ -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() {
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user