当前位置: 首页 > news >正文

tomcat服务器以及接受请求参数的方式

1.javaee:意为java企业版,指java企业级开发的规范总和,包含13项技术规范

2.事实上服务器和客户端进行交互的过程中,有一个前端控制器在中间运作,这个控制器为DispatcherServlet,它负责将客户端请求的信息包装成HttpServletrequest对象,同时负责将服务器所传回的响应信息包装成HttpServletresponse对象.

3.获取请求参数的方式:

(1)原始方式:

package new_start.new_start4.controller;import jakarta.servlet.http.HttpServletRequest;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;@RestController
public class ControllerHello {@RequestMapping("/simpleParam")public String simpleParam(HttpServletRequest request){String name = request.getParameter("name");String age = request.getParameter("age");int a = Integer.parseInt(age);System.out.println(name + "+" + age);return "ok";}
}

注意点:①getParameter的括号中必须加括号,表示字符串,且必须与请求参数的参数名一致;

②获取的是个字符串,需要自行进行转换;

(2)简单参数:

package new_start.new_start4.controller;import jakarta.servlet.http.HttpServletRequest;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;@RestController
public class ControllerHello {@RequestMapping("/simpleParam")public String simpleParam(String name, Integer age){System.out.println(name + "+" + age);return "ok";}
}

第二种十分简洁,不需要进行类型转换;但要求形参名必须和请求参数名一致;

如果不一致,可以加上@RequestParam(name = “name”)注解:

package new_start.new_start4.controller;import jakarta.servlet.http.HttpServletRequest;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;@RestController
public class ControllerHello {@RequestMapping("/simpleParam")public String simpleParam(@RequestParam(name = "name") String username, Integer age){System.out.println( username + "+" + age);return "ok";}
}

同时该注解可以添加第二个参数:required;

默认为true,表示必须传入,否则报400错误,显示请求异常;

(3)实体对象参数:

这个需要创建一个实体类pojo,并用实体对象来接受请求参数,要求请求参数名必须对应实体类的属性名;同时有嵌套格式的话需要在请求时,进行加.的修饰:

例如:

package new_start.new_start4.controller;import jakarta.servlet.http.HttpServletRequest;
import new_start.new_start4.pojo.User;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;@RestController
public class ControllerHello {@RequestMapping("/pojo")public String pojo(User user){System.out.println(user);return "ok";}
}

(4)数组参数:

请求中只需要传值时使用同一个key值,同时这个key值等同于数组名即可;

package new_start.new_start4.controller;import jakarta.servlet.http.HttpServletRequest;
import new_start.new_start4.pojo.User;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;import java.util.Arrays;@RestController
public class ControllerHello {@RequestMapping("/hobby")public String hobby(String[] hobby){System.out.println(Arrays.toString(hobby));return "ok";}
}

同时还可以用集合,只不过要加上@RequestParam:

package new_start.new_start4.controller;import jakarta.servlet.http.HttpServletRequest;
import new_start.new_start4.pojo.User;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;import java.util.Arrays;
import java.util.List;@RestController
public class ControllerHello {@RequestMapping("/hobby")public String hobby(@RequestParam List<String> hobby){System.out.println(hobby);return "ok";}
}

(5)日期参数

(6)json参数:

通过json传递参数必须要用post请求方式,要把参数写在请求体中:同时用实体类接受,实体类的属性名和键名一致:但要注意形参名前要加@RequestBody

package new_start.new_start4.controller;import jakarta.servlet.http.HttpServletRequest;
import new_start.new_start4.pojo.User;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;import java.util.Arrays;
import java.util.List;@RestController
public class ControllerHello {@RequestMapping("/json")public String json(@RequestBody User user){System.out.println(user);return "ok";}
}(7)路径参数:注意要加@PathVariable,同时mapping路径映射要加{}来设定参数名
package new_start.new_start4.controller;import jakarta.servlet.http.HttpServletRequest;
import new_start.new_start4.pojo.User;
import org.springframework.web.bind.annotation.*;import java.util.Arrays;
import java.util.List;@RestController
public class ControllerHello {@RequestMapping("/path/{id}")public String json(@PathVariable Integer id){System.out.println(id);return "ok";}
}

http://www.xdnf.cn/news/744193.html

相关文章:

  • Java网络编程实战:TCP/UDP Socket通信详解与高并发服务器设计
  • uniapp uni-id Error: Invalid password secret
  • Linux531rsync定时同步 再回忆
  • 智能测试新范式:GenAI 与 Playwright MCP 如何重塑 QA 流程
  • 【Ubuntu】摸鱼技巧之虚拟机环境复制
  • C#WinForm程序时方法很多时Form.cs文件会很长,如何分别写入多个文件,partial class的作用体现出来了。
  • 矩阵快速幂算法快速上手
  • 极大似然估计例题——正态分布的极大似然估计
  • 尚硅谷redis7 99 springboot整合redis之连接集群
  • AppTrace 视角下 App 一键拉起:提升应用转化率的高效方案​
  • 使用Gemini, LangChain, Gradio打造一个书籍推荐系统 (第四部分)
  • 自动驾驶系列—Monocular 3D Lane Detection for Autonomous Driving
  • 【Web API系列】WebTransportSendStream接口深度解析:构建高性能实时数据传输的基石
  • Python实现P-PSO优化算法优化循环神经网络LSTM分类模型项目实战
  • 【技能拾遗】——家庭宽带单线复用布线与配置(移动2025版)
  • 【网络与信息安全】实验三 RSA加解密与签名验证
  • 澄清 STM32 NVIC 中断优先级
  • [网页五子棋][对战模块]实现游戏房间页面,服务器开发(创建落子请求/响应对象)
  • 中文NLP with fastai - Fastai Part4
  • 新视角!经济学顶刊QJE用文本分析探究新技术扩散
  • 简单cnn
  • go|channel源码分析
  • c# 如何中的 ? 与 ??
  • “粽”览全局:分布式系统架构与实践深度解析(端午特别版)
  • 《信号与系统》第 5 章 离散时间傅里叶变换
  • 2025年- H61-Lc169--74.搜索二维矩阵(二分查找)--Java版
  • Qt -下载Qt6与OpenCV
  • Python训练营打卡Day41
  • 5G-A:开启通信与行业变革的新时代
  • 2025年渗透测试面试题总结-匿名[校招]渗透测试(打击黑灰产)(题目+回答)