什么是http协议
http协议: 对浏览器客户端 和 服务器端 之间数据传输的格式规范
查看http协议的工具
1)使用火狐的firebug插件(右键->firebug->网络)
2)使用谷歌的“审查元素”
http协议内容
请求(浏览器-》服务器)
GET /day09/hello HTTP/1.1
Host: localhost:8080
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:35.0) Gecko/20100101 Firefox/35.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: zh-cn,en-us;q=0.8,zh;q=0.5,en;q=0.3
Accept-Encoding: gzip, deflate
Connection: keep-alive
响应(服务器-》浏览器)
HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
Content-Length: 24
Date: Fri, 30 Jan 2015 01:54:57 GMT
this is hello servlet!!!
Http请求
GET /day09/hello HTTP/1.1 -请求行
Host: localhost:8080 --请求头(多个key-value对象)
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:35.0) Gecko/20100101 Firefox/35.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: zh-cn,en-us;q=0.8,zh;q=0.5,en;q=0.3
Accept-Encoding: gzip, deflate
Connection: keep-alive
--一个空行
name=eric&password=123456 --(可选)实体内容
请求行
GET /day09/hello HTTP/1.1
http协议版本
http1.0:当前浏览器客户端与服务器端建立连接之后,只能发送一次请求,一次请求之后连接关闭。
http1.1:当前浏览器客户端与服务器端建立连接之后,可以在一次连接中发送多次请求。(基本都使用1.1)
请求资源
URL: 统一资源定位符。http://localhost:8080/day09/testImg.html。只能定位互联网资源。是URI的子集。
URI: 统一资源标记符。/day09/hello。用于标记任何资源。可以是本地文件系统,局域网的资源(//192.168.14.10/myweb/index.html可以是互联网。
常见的请求方式
GET 、 POST、 HEAD、 TRACE、 PUT、 CONNECT
防止非法链接(referer)
<filter>
<filter-name>ImgFilter</filter-name>
<filter-class>com.itmayiedu.filter.ImgFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>ImgFilter</filter-name>
<url-pattern>/static/*</url-pattern>
</filter-mapping>
public class ImgFilter implements Filter {
public void init(FilterConfig filterConfig) throws ServletException {
System.out.println("初始化...");
}
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException {
System.out.println("doFilter....");
HttpServletRequest req = (HttpServletRequest) request;
HttpServletResponse res = (HttpServletResponse) response;
//获取请求头中来源
String referer = req.getHeader("referer");
//获取当前请求名称
String serverName = request.getServerName();
System.out.println("referer:"+referer+"----serverName:"+serverName+":"+serverName);
if(referer==null||(!referer.contains(serverName))){
req.getRequestDispatcher("/imgs/error.png").forward(req, res);
return ;
}
chain.doFilter(req, res);
}
public void destroy() {
}
}