1 /** 2 * 演示Request对象获取请求行数据 3 */ 4 @WebServlet("/test") 5 public class RequestDemo1 extends HttpServlet { 6 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 7 8 } 9 10 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 11 //一、获取请求行 12 //1.获取请求方式:GET 13 String method = request.getMethod(); 14 System.out.println(method); 15 //2.(*)获取虚拟目录:/request 16 String contextPath = request.getContextPath(); 17 System.out.println(contextPath); 18 //3.获取Servlet路径:/demo 19 String servletPath = request.getServletPath(); 20 System.out.println(servletPath); 21 //4.获取get方式请求参数:name=zhangshan 22 String queryString = request.getQueryString(); 23 System.out.println(queryString); 24 //5.(*)获取请求URI:/request/demo 25 //* String getRequestURI(): /request/demo 26 String requestURI = request.getRequestURI(); 27 System.out.println(requestURI); 28 //* StringBuffer getRequestURI():http://localhost/request/demo 29 StringBuffer requestURL = request.getRequestURL(); 30 System.out.println(requestURL); 31 //6.获取协议及版本:HTTP/1.1 32 String protocol = request.getProtocol(); 33 System.out.println(protocol); 34 //7.获取客户机的IP地址: 35 //*String getRemoteAddr() 36 String remoteUser = request.getRemoteUser(); 37 System.out.println(remoteUser); 38 } 39 }