当前位置:网站首页>Request和Response及其ServletContext总结
Request和Response及其ServletContext总结
2022-04-23 13:06:00 【ArchieSean】
博主名取自《小羊肖恩》中的小羊肖恩,名字为:肖恩,音译为Sean,自己取的姓:阿奇,为符合我们的阅读习惯,连起来组成为ArchieSean。博主目前大三在读,志在将博客打造成为个人线上笔记的技术栈,方便自己也方便他人。如博客中有任何错误,请各位指出,谢谢大家。
文章目录
Request和Response
原理
- request和response都是服务器提供的对象。
- request是用来获取请求消息的
- response是用来设置响应消息的。
Request
Request的体系结构
还有一个类是由服务器来提供的,它实现了HttpServletRequest接口。
Request的功能
比较常用的有:
- 通用的获取请求参数的方式
request.getParameter(String name);
request. getParameterValues(String name);
request.getParameterNames();
request. getParameterMap();
- 解决POST请求中文乱码
request.setCharacterEncoding("utf-8");
- 转发
request.getRequestDispatcher(String path).forward(request,response);
- 共享数据的使用
request.setAttribute(String key,Object obj);
request.getAttribute(String key);
request.removeAttribute(String key);
- 获取ServletContext对象
request.getServletContext();
Response
Response体系结构
同Request一样,服务器提供了一个类,实现了HttpServletResponse接口。
Response的功能
常用的有:
- 解决中文乱码问题
resp.setContentType("MIME类型;字符集");
- 获取流对象
- 获取字符输出流
- 获取字节输出流
- 重定向
resp.sendRedirect(String path);
ServletContext
概述
简单来说,ServletContext是web应用中最大的域对象。意思是说同一个服务器下的所有用户之间可以数据共享。
获取
该对象的获取有两种方式:
- 通过request来获取
request.getServletContext();
- 通过servlet来获取
this.getServletContext();
功能
- 数据共享(域对象的三个通用方法)
- 获取MIME类型
getMimeType(String file);
- 获取资源路径
getRealPath(String path);
博文中如有任何问题,请留言。
版权声明
本文为[ArchieSean]所创,转载请带上原文链接,感谢
https://blog.csdn.net/qq_41964942/article/details/106044861
边栏推荐
- 基于uniapp异步封装接口请求简介
- 31. Next arrangement
- FFmpeg常用命令
- Pyqt5 store opencv pictures into the built-in sqllite database and query
- [Technical Specification]: how to write technical documents?
- The first lesson is canvas, showing a small case
- Mysql8 installation
- CMSIS cm3 source code annotation
- CVPR 2022 & ntire 2022 | the first transformer for hyperspectral image reconstruction
- 这几种 VSCode 扩展是我最喜欢的
猜你喜欢
nodejs + mysql 实现简单注册功能(小demo)
Complete project data of UAV apriltag dynamic tracking landing based on openmv (LabVIEW + openmv + apriltag + punctual atom four axes)
mysql8安装
Date time type in database
CMSIS cm3 source code annotation
软件测试周刊(第68期):解决棘手问题的最上乘方法是:静观其变,顺水推舟。
R语言中dcast 和 melt的使用 简单易懂
nodeJs + websocket 循环小案例
World Book Day: I'd like to recommend these books
Mui + hbuilder + h5api simulate pop-up payment style
随机推荐
AUTOSAR from introduction to mastery 100 lectures (51) - AUTOSAR network management
Mui + hbuilder + h5api simulate pop-up payment style
CMSIS cm3 source code annotation
The first lesson is canvas, showing a small case
async void 导致程序崩溃
The accuracy and speed are perfectly balanced, and the latest image segmentation SOTA model is released!!!
Introducing vant components on demand
mysql支持ip访问
About the 'enum' enumeration type and structure.
There is no need to crack the markdown editing tool typora
Jupiter notebook installation
这几种 VSCode 扩展是我最喜欢的
office2021安装包下载与激活教程
Introduction to servlet listener & filter
Complete project data of UAV apriltag dynamic tracking landing based on openmv (LabVIEW + openmv + apriltag + punctual atom four axes)
Record some NPM related problems (messy records)
Golang realizes regular matching: the password contains at least one digit, letter and special character, and the length is 8-16
Design and manufacture of 51 single chip microcomputer solar charging treasure with low voltage alarm (complete code data)
Mysql8 installation
World Book Day: I'd like to recommend these books