User-Profile-Image
hankin
  • 5
  • Java
  • Kotlin
  • Spring
  • Web
  • SQL
  • MegaData
  • More
  • Experience
  • Enamiĝu al vi
  • 分类
    • Zuul
    • Zookeeper
    • XML
    • WebSocket
    • Web Notes
    • Web
    • Vue
    • Thymeleaf
    • SQL Server
    • SQL Notes
    • SQL
    • SpringSecurity
    • SpringMVC
    • SpringJPA
    • SpringCloud
    • SpringBoot
    • Spring Notes
    • Spring
    • Servlet
    • Ribbon
    • Redis
    • RabbitMQ
    • Python
    • PostgreSQL
    • OAuth2
    • NOSQL
    • Netty
    • MySQL
    • MyBatis
    • More
    • MinIO
    • MegaData
    • Maven
    • LoadBalancer
    • Kotlin Notes
    • Kotlin
    • Kafka
    • jQuery
    • JavaScript
    • Java Notes
    • Java
    • Hystrix
    • Git
    • Gateway
    • Freemarker
    • Feign
    • Eureka
    • ElasticSearch
    • Docker
    • Consul
    • Ajax
    • ActiveMQ
  • 页面
    • 归档
    • 摘要
    • 杂图
    • 问题随笔
  • 友链
    • Spring Cloud Alibaba
    • Spring Cloud Alibaba - 指南
    • Spring Cloud
    • Nacos
    • Docker
    • ElasticSearch
    • Kotlin中文版
    • Kotlin易百
    • KotlinWeb3
    • KotlinNhooo
    • 前端开源搜索
    • Ktorm ORM
    • Ktorm-KSP
    • Ebean ORM
    • Maven
    • 江南一点雨
    • 江南国际站
    • 设计模式
    • 熊猫大佬
    • java学习
    • kotlin函数查询
    • Istio 服务网格
    • istio
    • Ktor 异步 Web 框架
    • PostGis
    • kuangstudy
    • 源码地图
    • it教程吧
    • Arthas-JVM调优
    • Electron
    • bugstack虫洞栈
    • github大佬宝典
    • Sa-Token
    • 前端技术胖
    • bennyhuo-Kt大佬
    • Rickiyang博客
    • 李大辉大佬博客
    • KOIN
    • SQLDelight
    • Exposed-Kt-ORM
    • Javalin—Web 框架
    • http4k—HTTP包
    • 爱威尔大佬
    • 小土豆
    • 小胖哥安全框架
    • 负雪明烛刷题
    • Kotlin-FP-Arrow
    • Lua参考手册
    • 美团文章
    • Java 全栈知识体系
    • 尼恩架构师学习
    • 现代 JavaScript 教程
    • GO相关文档
    • Go学习导航
    • GoCN社区
    • GO极客兔兔-案例
    • 讯飞星火GPT
    • Hollis博客
    • PostgreSQL德哥
    • 优质博客推荐
    • 半兽人大佬
    • 系列教程
    • PostgreSQL文章
    • 云原生资料库
    • 并发博客大佬
Help?

Please contact us on our email for need any support

Support
    首页   ›   Java   ›   正文
Java

HttpServletRequest常用的方法

2020-03-14 01:34:56
1064  0 0
参考目录 隐藏
1) HttpServletRequest
2) ServerHttpRequest

阅读完需:约 2 分钟

HttpServletRequest

HttpServletRequest接口最常用的方法就是获得请求中的参数,这些参数一般是客户端表单中的数据。同时,HttpServletRequest接口可以获取由客户端传送的名称,也可以获取产生请求并且接收请求的服务器端主机名及IP地址,还可以获取客户端正在使用的通信协议等信息。

[1]、request.getRequestURL:http://user.cbice.com/cbice/BoardAction.do--------客户请求的url,不包括参数数据。 
[2]、request.getRequestURI:/cbice/BoardAction.do--------将URL的域名和尾随的参数截取掉,剩下的那部分就是URI 。 
[3]、request.getContextPath:/cbice-----即斜杆加工程名。 
[4]、request.getRealPath("/WEB-INF"):D:\jboss-4.0.2_cbiceportal\server\default\.\deploy\user.war\WEB-INF-----即斜杆加工程名。 
[5]、request.getMethod:POST--------HTTP请求的的方法名,默认是GET,也可以指定PUT或POST。 
[6]、request.getScheme: http ---返回请求的方案名,如http,ftp,https等。 
[7]、request.getServletPath: /BoardAction.do---工程之后到参数之前的这部分字符串。 
[8]、request.getServerName: user.cbice.com ---服务器主机名。 
[9]、request.getServerPort: 80 ---服务器上web应用的访问端口。 
[10]、request.getRemoteAddr: 192.168.6.8 ---发送请求的客户端主机的IP 
[11]、request.getRemoteHost: 192.168.6.8 ---发送请求的客户端主机名,如果不确定返回的是IP。 
[12]、request.getQueryString:operation=doSearch&index=index&boardIndex=boardIndex------返回URL上的参数部分的字符串,必须是GET的请求才有效,不然报错. 
[13]、getServletContext:为servletconfig中的方法 获取ServletContext对象 
[14]、getRealPath:得到项目下webroot/
这里的URL参数中带有中文,是通过字符转码的:String eQuery=new String(request.getQueryString().getBytes("ISO-8859-1")) 
response的响应内容:response.setContentType("text/html;charset=gbk"),才可以正常显示页面中文。 
如果需要获得参数则用String status = new String(request.getParameter("username").trim().getBytes(), "GBK");获得其内容。 
另外一种方法是:用String eQuery=URLEncoder.encode("中文","GBK");按照参数编码格式进行转码, 
在页面中通过request.getParamter("eQuery")得到的参数是中文,但是在url中显示的是16进制或其他进制的编码。 

ServerHttpRequest

根据Spring Docs:ServerHttpRequest接口实现基于HttpRequest接口。

现在,有一个名为ServletServerHttpRequest的类实现了ServerHttpRequest接口,它还有公共方法getServletRequest()来获取实际的HttpServletRequest。

if(exchange.getRequest() instanceof ServletServerHttpRequest) {
   ServletServerHttpRequest request = (ServletServerHttpRequest) exchange.getRequest();
   HttpServletRequest httpServletRequest = request.getServletRequest();
}

如本文“对您有用”,欢迎随意打赏作者,让我们坚持创作!

0 打赏
Enamiĝu al vi
不要为明天忧虑.因为明天自有明天的忧虑.一天的难处一天当就够了。
543文章 68评论 294点赞 594299浏览

随机文章
SpringSecurity—自带防火墙
4年前
SpringBoot—Gson的使用
5年前
Docker—安装与基本组成(一)
5年前
SpringCloud—Hystrix(六)仪表盘与Turbine集群监控
5年前
Spring—Assert断言测试(旧)(常用方法)
5年前
博客统计
  • 日志总数:543 篇
  • 评论数目:68 条
  • 建站日期:2020-03-06
  • 运行天数:1927 天
  • 标签总数:23 个
  • 最后更新:2024-12-20
Copyright © 2025 网站备案号: 浙ICP备20017730号 身体没有灵魂是死的,信心没有行为也是死的。
主页
页面
  • 归档
  • 摘要
  • 杂图
  • 问题随笔
博主
Enamiĝu al vi
Enamiĝu al vi 管理员
To be, or not to be
543 文章 68 评论 594299 浏览
测试
测试
看板娘
赞赏作者

请通过微信、支付宝 APP 扫一扫

感谢您对作者的支持!

 支付宝 微信支付