Request:
1.request对象和response对象的原理
1.request和response对象是由服务器创建的。我们来使用它们。
2.request对象是来请求消息,respect对象是用来设置响应消息的。
获取请求消息数据:
get/day14/demo1?name=zhangsan HTTP/1.1
*方法
1.获取请求方式:get
*String getMethod()
2.获取Servlent的路径
@WebServlet(value = "/demo")
public class GetServer extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req , HttpServletResponse resp) throws ServletException, IOException {
//获取请求方式
String reqMethod = req.getMethod ();
System.out.println ("请求方式:"+reqMethod);
//获取Servlent的路径
String servletPath = req.getServletPath ();
System.out.println ("Servlent的路径:"+servletPath);
//获取的虚拟路径(重点)
String contextPath = req.getContextPath ();
System.out.println ("虚拟路径:"+contextPath);
//获取get的提交的请求参数
String queryString = req.getQueryString ();
System.out.println ("参数:"+queryString);
//获取Servlent的URI(重点)
String requestURI = req.getRequestURI ();
System.out.println ("请求的URI:"+requestURI);
StringBuffer requestURL = req.getRequestURL ();
System.out.println ("请求的URL:"+requestURL);
//获取协议和版本信息
String protocol = req.getProtocol ();
System.out.println ("协议和版本信息:"+protocol);
//获取客户端地址值
String reqRemoteAddr = req.getRemoteAddr ();
System.out.println ("地址值:"+reqRemoteAddr);
}
获取请求头数据:
1.(重点)String getHeader(String name):通过请求头获取请求值
2.getHeaderNames:获取所有请求头键的名称。
@WebServlet(value = "/r3")
public class Requestf1Servlet extends HttpServlet {
protected void doPost(HttpServletRequest request , HttpServletResponse response) throws ServletException, IOException {
}
protected void doGet(HttpServletRequest request , HttpServletResponse response) throws ServletException, IOException {
//获取所有请求头,名称(获取所有请求头不常用)
Enumeration <String> headerNames = request.getHeaderNames ();
//遍历
while (headerNames.hasMoreElements ()) {
String name = headerNames.nextElement ();
//根据名称获取请求头的值
String value = request.getHeader