Request获取请求行数据方法介绍和Request获取请求行数据代码演示
Request_获取请求行数据_方法介绍
GET/day14/demo01?name=zhangsan HTTP/1.1
方法:
1.获取请求方式:GET
String getMethod()
2.获取虚拟目录:/day14
String getContextPath()
3.获取Servlet路径:/demo1
String getServletPath()
4.获取get方式请求参数:name=zhangsan
String getQueryString()
5.获取请求URI:/day14/demo1
String getRequestURI():/day14/demo1
StringBuffer getRequestURL() :http://localhost/day14/demo1
URL:统一资源定位符:http://localhost:8080/servlet/RequestD1
URI:统一资源标识符:/servlet/RequestD1
6.获取协议及版本:HTTP/1.1
String getProtocol()
7.获取客户机的IP地址:
String getRemoteAddr()
Request_获取请求行数据_代码演示
@WebServlet("/RequestD1") public class RequestD1 extends HttpServlet { protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // 1.获取请求方式:GET String method = request.getMethod(); System.out.println(method); // 2.获取虚拟目录:/day14 String contextPath = request.getContextPath(); System.out.println(contextPath); // 3.获取Servlet路径:/demo1 String servletPath = request.getServletPath(); System.out.println(servletPath); // 4.获取get方式请求参数:name=zhangsan String queryString = request.getQueryString(); System.out.println(queryString); // 5.获取请求URI:/day14/demo1 String requestURI = request.getRequestURI(); StringBuffer requestURL = request.getRequestURL(); System.out.println(requestURI); System.out.println(requestURL); // 6.获取协议及版本:HTTP/1.1 String protocol = request.getProtocol(); System.out.println(protocol); // 7.获取客户机的IP地址: String remoteAddr = request.getRemoteAddr(); System.out.println(remoteAddr); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { } }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 【自荐】一款简洁、开源的在线白板工具 Drawnix