Request获取请求行数据方法介绍和获取请求行数据代码演示
Request获取请求行数据:
- GET/day14/demo1?name=zhangsan HTTP/1.1
- 方法
- 获取请求方法:GET
- String getMethod();
- 获取虚拟目录:/day14
- String getContextpath();
- 获取Servlet路径:/demo1
- String getServletPath();
- 获取get方式请求参数:
- String getQueryString();
- 获取请求URL:/day14/demo1
- String getRequestURL();
- StringBuffer getRequestURL();
- 获取协议及版本:HTTP/1.1
- String getProtocol();
- 获取客户机的IP地址:
- String getRemoteAddr();
- 获取请求方法:GET
代码演示:
package com.ailyt.request;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebServlet(value = "/r1")
public class RequestStudy1 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
/**
* 获取请求方法:GET
* String getMethod();
* 获取虚拟目录:/day14
* String getContextpath();
* 获取Servlet路径:/demo1
* String getServletPath();
* 获取get方式请求参数:
* String getQueryString();
* 获取请求URL:/day14/demo1
* String getRequestURL();
* StringBuffer getRequestURL();
* 获取协议及版本:HTTP/1.1
* String getProtocol();
* 获取客户机的IP地址:
* String getRemoteAddr();
*/
//获取请求方法
String method = req.getMethod();
System.out.println("请求方法--:"+method);
//获取虚拟目录
String contextPath = req.getContextPath();
System.out.println("虚拟目录--:"+contextPath);
//获取Servlet路径
String servletPath = req.getServletPath();
System.out.println("Servlet路径--:"+servletPath);
//获取get方式请求参数
String queryString = req.getQueryString();
System.out.println("获取get方式请求参数--:"+queryString);
//获取请求URL
String requestURI = req.getRequestURI();
System.out.println("请求URL--:"+requestURI);
//获取协议及版本
String protocol = req.getProtocol();
System.out.println("协议及版本--:"+protocol);
//获取客户机的IP地址
String remoteAddr = req.getRemoteAddr();
System.out.println("客户机的IP地址--:"+remoteAddr);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
super.doPost(req, resp);
}
}
运行结果: