1.HTTP请求数据的封装:
3.1 请求行
GET /day09/hello HTTP/1.1
Accept: text/html,image/* -- 浏览器接受的数据类型 Accept-Charset: ISO-8859-1 -- 浏览器接受的编码格式 Accept-Encoding: gzip,compress --浏览器接受的数据压缩格式 Accept-Language: en-us,zh- --浏览器接受的语言 Host: www.it315.org:80 --(必须的)当前请求访问的目标地址(主机:端口) If-Modified-Since: Tue, 11 Jul 2000 18:23:51 GMT --浏览器最后的缓存时间 Referer: http://www.it315.org/index.jsp -- 当前请求来自于哪里 User-Agent: Mozilla/4.0 (compatible; MSIE 5.5; Windows NT 5.0) --浏览器类型 Cookie:name=eric -- 浏览器保存的cookie信息 Connection: close/Keep-Alive -- 浏览器跟服务器连接状态。close: 连接关闭 keep-alive:保存连接。 Date: Tue, 11 Jul 2000 18:23:51 GMT -- 请求发出的时间
2.Rquest 对象 封装请求的数据 (注意:request 与response对象均由Tomcat创建传入Service方法中的 再有service传入到各个请求方式的方法中 例如 doget、 dopost等等)
常用方法: 例如一个访问的路径是 www.localhost:8080/day14/demo1
//获取请求行的方法
String getMethod() 获取请求的方法 : get
String getContextPath 获取虚拟的路径 也就是项目的根目录 /day14
String getServletPath 获取访问servlet的路径 /demo1
String getRequestURI 获取URI day14/demo1
String getProtocol 获取协议 和版本
//获取请求头的方法
String getHeader(String name) //通过头获取头的值
//传值问题
String par = request.getParameter("传递参数的名字");
Enumeration<String> parameterNames = request.getParameterNames();
String[] vs = request.getParameterValues("主要针对复选框传值");
Map<String, String[]> parameterMap = request.getParameterMap();
设置乱码:
注意:tomcat8 或者以上版本 已经将Get方式提交的数据乱码已经解决了 不需要在去 new String(str.getbytes("iso-8859-1"), "utf-8)来解决了;
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
request的转发:
request.getRequestDispatcher("/index.jsp").forward(requst,response);
requset的域对象
request.setAttribute("name","对象"); request.getAttribute("name"); request.removeAttribute("name");
request获取ServletContext对象(这个对象可以理解为就是Tomcat容器,装了很多的servlet对象的容器)
ServletContext servletContext = request.getServletContext();
3.Response 对象 封装响应的数据
Location: http://www.it315.org/index.jsp -表示重定向的地址,该头和302的状态码一起使用。 Server:apache tomcat ---表示服务器的类型 Content-Encoding: gzip -- 表示服务器发送给浏览器的数据压缩类型 Content-Length: 80 --表示服务器发送给浏览器的数据长度 Content-Language: zh-cn --表示服务器支持的语言 Content-Type: text/html; charset=GB2312 --表示服务器发送给浏览器的数据类型及内容编码 Last-Modified: Tue, 11 Jul 2000 18:23:51 GMT --表示服务器资源的最后修改时间 Refresh: 1;url=http://www.it315.org --表示定时刷新 Content-Disposition: attachment; filename=aaa.zip --表示告诉浏览器以下载方式打开资源(下载文件时用到) Transfer-Encoding: chunked Set-Cookie:SS=Q0=5Lb_nQ; path=/search --表示服务器发送给浏览器的cookie信息(会话管理用到) Expires: -1 --表示通知浏览器不进行缓存 Cache-Control: no-cache Connection: close/Keep-Alive --表示服务器和浏览器的连接状态。close:关闭连接 keep-alive:保存连接 4.4 案例- 请求重定向(Location)(setstutua(“302)) Respose.setStutio(“302); Response.sethead(”Location”,”www.baidu.com”) 4.5 案例- 定时刷新(refresh) Respose.sethead(“refresh”, “1; www.baidu.com”) 4.6 案例-content-Type作用(content-Disposition:attachment;filename=aaa.zip)
4.路径的书写问题
相对的写法:
./ 当前目录
../ 向上退一级
![](https://img2020.cnblogs.com/blog/1647475/202005/1647475-20200519164937865-271838275.png)
当目标资源与资源处于同一级目录下的情况可以写成 ./xxxx资源 或者 xxxx资源即可
![](https://img2020.cnblogs.com/blog/1647475/202005/1647475-20200519165003617-460909767.png)
绝对路径
需要加虚拟目录的列如有:href , 表单的action ,还有 跳转等 /虚拟目录/资源
不需要加虚拟目录的 :例如有 转发 /资源