Request_获取请求数据_方法介绍、Request_获取请求头
Request_获取请求数据_方法介绍
获取请求消息数据
1,获取请求行数据
GET /day14/ demo1?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
* tring getRequestURI(): /day14/demo1
stringBuffer getRequestURL():http://localhost/day14/demo1
6.获取协议及版本:HTTP/1.1
*string getProtocol()
7.获取客户机的IP地址:
*string getRemoteAddr()
*
案例:
@WebServlet( value = "/ServletHttp")
public class ServletHttp extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
/**
* 1,获取请求行数据
*
* GET /day14/ demo1?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
* * tring getRequestURI(): /day14/demo1
* stringBuffer getRequestURL():http://localhost/day14/demo1
* 6.获取协议及版本:HTTP/1.1
* *string getProtocol()
* 7.获取客户机的IP地址:
* *string getRemoteAddr()
* *
*/
//1.获取请求方式:GET
String method = request.getMethod();
System.out.println(method);
// 2.获取虚拟目录:!/day14
String path = request.getContextPath();
System.out.println(path);
//3.获取servlet路径:/demo1
String s = request.getServletPath();
System.out.println(s);
// 4.获取get方式请求参数:name=zhangsan
String string = request.getQueryString();
System.out.println(string);
//5.获取请求URI : /day14/ demo1
String requestURI = request.getRequestURI();
System.out.println(requestURI);
// 6.获取协议及版本:HTTP/1.1
String protocol = request.getProtocol();
System.out.println(protocol);
//.获取客户机的IP地址:
String remoteAddr = request.getRemoteAddr();
System.out.println(remoteAddr);
}
}
Request_获取请求头
获取请求头的数据:
方法∶
* (*)string getHeader(string name):通过请求头的名称获取请求头的值
* Enumeration<strihg> getHeaderNames():获取所有的请求头名称