• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
Sunyn-blogs
博客园    首页    新随笔    联系   管理    订阅  订阅
JavaWeb05-Web基础

Web基础

image

1.SpringBoot

Spring

  • 官网:spring.io

  • Spring发展到今天已经形成了一种开发生态圈,Spring提供了若干个子项目,每个项目用于完成特定的功能。

    image

    Spring Boot可以帮助我们非常快速的构建应用程序、简化开发、提高效率。

    image

1.1、入门程序

  • 需求:基于SpringBoot开发一个Web应用,浏览器发起请求/hello之后,给浏览器返回一个字符串“Hello Xxx”。

image

image

创建完Spring Boot项目后

image

这个为启动类,启动后才可以运行项目

创建如上图HelloController的请求类

package com.example.springbootwebquickstart;


import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController //表示当前是一个请求处理类
public class HelloController {

    @RequestMapping("/hello")
    public String hello(String name){
        System.out.println("name:" + name);
        return "Hello" + name + "~";
    }
}

然后访问

image

image

步骤:

  1. 创建SpringBoot工程,勾选Web开发依赖
  2. 定义请求处理类HelloController,定义请求处理方法
  3. 运行启动类,测试

image

1.2、HTTP协议

  • 概念:Hyper Text Transfer Protocol,超文本传输协议,规定了浏览器和服务器之间数据传输的规则。

    image

我们回到上一小节我们请求 Helloheima~

image

可以看到浏览器向客户端发出的请求和客户端的响应都是纯文本格式

  • 特点
    1. 基于TCP协议:面向连接,安全
    2. 基于请求-响应模型的:一次请求对应一次响应
    3. HTTP协议是无状态的协议:对于事务处理没有记忆能力。每次请求-响应都是独立的。
      • 缺点:多次请求间不能共享数据
      • 优点:速度快

HTTP-请求协议

请求数据格式

image

image

HTTP协议中请求数据分为哪几个部分?

  • 请求行(请求数据的第一行)
  • 请求头(key:value)
  • 请求体(POST方式 与请求头之间隔了一个空行)
请求数据获取
  • Web服务器(Tomcat)对HTTP协议的请求数据进行解析,并进行封装(HttpServletRequest),在调用Controller方法的时候传递给了该方法。这样,就使得程序员不必直接对协议进行操作,让Web开发更加便捷。

    image

@RequestMapping("/request")
public String request(HttpServletRequest request){
    // 1.获取请求参数name, age
    String name = request.getParameter("name"); // Tom
    // 2.获取请求路径uri 和 url
    String uri = request.getRequestURI(); // /request
    String url = request.getRequestURL().toString(); // http://localhost:8080/request
    // 3.获取请求头 User-Agent
    String userAg ent = request.getHeader("User-Agent"); // Mozilla/5.0 (Windows NT 10.0; Win64; x64)
    // 4.获取请求方式
    String method = request.getMethod(); // GET
    // 5.获取请求的查询字符串
    String queryString = request.getQueryString(); // name=Tom&age=10
    return "request success";
}

我们可以创建一个RequestController类来测试一下:

package com.example.springbootwebquickstart;

import jakarta.servlet.http.HttpServletRequest;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class RequestController {

    @RequestMapping("/request")
    public String request(HttpServletRequest request){
        //1.获取请求方式
        String method = request.getMethod();
        System.out.println("请求方式:" + method);

        //2.获取请求url地址
        String url = request.getRequestURL().toString();
        System.out.println("请求地址:" + url);

        //3.获取请求协议
        String protocol = request.getProtocol();
        System.out.println("请求协议:" + protocol);
        //4.获取请求参数- name,age
        String name = request.getParameter("name");
        String age = request.getParameter("age");
        System.out.println("请求参数:" + name + " " + age);
        //5.获取请求头 - Accept
        String Accept = request.getHeader("Accept");
        System.out.println("请求头:" + Accept);

        return "请求成功";
    }

}

请求代码可以ai生成

当我们在浏览器中输入网址 localhost:8080/request?name=itheima&age=18

image

image

image

HTTP-相应协议

响应数据格式

image

image

关于重定向:

image

浏览器会进行两次请求行为,但对于用户来说没有感知

比如百度地址为(https://www.baidu.com/) 如果我们输入(http://www.baidu.com/)就会发生重定向

image

需掌握的状态码:

image

常见状态码错误:

image

响应数据设置
  • Web服务器对HTTP协议的响应数据进行了封装(HttpServletResponse),并在调用Controller方法的时候传递给了该方法。这样,就使得程序员不必直接对协议进行操作,让Web开发更加便捷。

image

有两种方式:

image

方式一:

package com.example.springbootwebquickstart;

import jakarta.servlet.http.HttpServletResponse;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;

@RestController
public class ResponseController {
    /**
     *  方式一:HttpServletResponse 设置响应数据
     * @param response
     * @return
     */
    @RequestMapping("/response")
    public void response(HttpServletResponse  response) throws IOException {
        //1. 设置响应状态码
        response.setStatus(401); //通常不用设定
        //2.设置响应头
        response.setHeader("name","itheima");
        //3.设置响应体
        response.getWriter().print("<h1>hello response</h1>");
    }
}

image

方式二

package com.example.springbootwebquickstart;

import jakarta.servlet.http.HttpServletResponse;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;

@RestController
public class ResponseController {
    /**
     *  方式一:HttpServletResponse 设置响应数据
     * @param response
     * @return
     */
    @RequestMapping("/response")
    public void response(HttpServletResponse  response) throws IOException {
        //1. 设置响应状态码
        response.setStatus(401); //通常不用设定
        //2.设置响应头
        response.setHeader("name","itheima");
        //3.设置响应体
        response.getWriter().print("<h1>hello response</h1>");
    }

    /**
     * 方式二:使用ResponseEntity -Spring中提供的方式
     * @return
     */
    @RequestMapping("/response2")
    public ResponseEntity<String> response2(){

        return ResponseEntity
                .status(401) //响应状态码
                .header("name","javaweb-ai") //响应头
                .body("<h1>hello responseEntity</h1>"); //响应体
    }
}

image

注意:响应状态码和响应头如果没有特殊要求的话,通常不手动设定。服务器会根据请求处理的逻辑,自动设置响应状态码和响应头。

image

posted on 2025-11-12 17:04  齐天大圣951  阅读(0)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3