Request_獲取請求行資料_方法介紹以及程式碼演示
阿新 • • 發佈:2022-12-04
Request_獲取請求行資料_方法介紹以及程式碼演示
request功能:
獲取請求訊息
獲取請求行資料
GET /demo3?name=zhangsan HTTP/1.1
方法:
1.獲取請求方式:GET
String getMethod();
2.(*)獲取虛擬目錄:/day14
String getContextPath();
3.獲取Serevlet路徑:/demo1
String getServletPath()
4.獲取get方式請求引數:name=zhangsan
String getQueryString()
5.(*)獲取請求URI:/day14/demo1
String getRequestURI():/day14/demo1 共和國
StringBuffer getRequestURL():http://localhost/day14/demo1 中華人民共和國
URL:統一資源定位符
URI:統一資源識別符號
6.獲取協議及版本:HTTP/1.1
String getProtocol()
7.獲取客戶機的IP地址:
String getRemotAddr()
@WebServlet(name = "RequestDemo1", value = "/RequestDemo1") public class RequestDemo1 extends HttpServlet { @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {/* 1.獲取請求方式:GET String getMethod(); 2.(*)獲取虛擬目錄:/day14 String getContextPath(); 3.獲取Serevlet路徑:/demo1 String getServletPath() 4.獲取get方式請求引數:name=zhangsan String getQueryString() 5.獲取請求URI:/day14/demo1 String getRequestURI():/day14/demo1 StringBuffer getRequestURL():http://localhost/day14/demo1 6.獲取協議及版本:HTTP/1.1 String getProtocol() 7.獲取客戶機的IP地址: String getRemotAddr() */ //1.獲取請求方式:GEI String method = request.getMethod(); System.out.println(method); //2.(*)獲取虛擬目錄:/day14 String contextPath = request.getContextPath(); System.out.println(contextPath); //3.獲取Serevlet路徑:/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(); System.out.println(requestURI); StringBuffer requestURL = request.getRequestURL(); 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); } @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { } }