获取请求行数据
GET /website/demo1?name=zhangsan HTTP/1.1
1.获取请求方式: GET
string getMethod()
2. (重要) 获取虚拟目录: /website
string getContextPath( )
3.获取Servlet路径: /demo1
String getServletPath()
4.获取get方式请求参数: name=zhangsan
String getQueryString()
5. (重要)获取请求URL : /website/demo1
string getRequestURI(): /website/demo1
stringBuffer getRequestURL() :http://localhost/website/demo1
URL :统一 资源定位符,要长一些
URI :统一资源标识符,要短一些
6.获取协议及版本: HTTP/1.1
string getProtocol()
7.获取客户机的IP地址:
string getRemoteAddr()
代码演示:
//演示Request
@WebServlet("/requestDemo1")
public class RequestDemo1 extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//1.获取请求方式: GET
String method = request.getMethod();
System.out.println(method);
//2. (重要) 获取虚拟目录: /website
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. (重要)获取请求URL : /website/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 remoteUser = request.getRemoteUser();
System.out.println(remoteUser);
}
}