这部分学习servlet如何处理头信息
下面部分是网上找来的
Servlet 请求头信息
一、典型的请求头信息
GET /servlet/Search?keywords=servlets+jsp HTTP 1.1
Accept:image gif.image.jpg
Accept-Encoding gzip
Connection:Keep-Alive
Cookie:userID=id456578
Host:www.somebookstore.com
Referer:http://www.somebookstorn.com/findbooks.html
User-Agent:Mozilla/4.0(compatible;MSIE 6.0;Windows NT 5.0)
GET:发送请求的方式
/servlet/Search?keywords=servlets+jsp:URL
HTTP 1.1:协议和版本号
Accept:可以接受的文件类型
Connection:是否保存长久连接
Cookie:持久保持的信息
Referer:当前请求的URL
User-Agent:浏览器类型
读取HttpServletRequest中的方法
一般方法
-getHeader(header名称不区分大小写)
-getHeaders
-getHeaderNames
专门方法
-getCookies
-getAuthType
-getRemoteUser
-getContentLength
-getContentType
-getDateHeader
-getIntHeader
相关信息
-getMethod
-getRequestURL
-getQueryString
-getProtocol
理解各种请求头的含义
-Accept
标识浏览器能出来MIME类型
能发送不同的内容到不同的客户端,例如,PNG文件有好的压缩特性,但是在浏览器中支持的不是很广泛
一个Servlet可以检查是否支持PNG文件格式,如果支持
- <IMG SRC="picture.png" ...>否则
- <IMG SRC="pictyre.gif">
-Accept-Encoding
标识浏览器能处理的编码类型
-Authorization
授权信息,通常出现在对服务器发送的WWW-Authenticate头的应答中
-Connection
表示是否需要持久连接。如果Servlet看到这里的值为"Keep-Alive",或者看到强求使用的是HTTP 1.1(HTTP1.1默认进行持久连接),它就可以利用持久连接的优点,当页面包含多个元素时,显著地减少下载所需要的时间,要实现这一点,Servlet需要ByteArrayOutputStream,然后在正式写出内容之前计算它的大小.
-Cookie
-Host
包含一个URL,用户从该URL代表的页面出发访问当前请求的页面
-If-Modified-Since
只有当所请求的内容,在指定的日期之后,又经过修改才返回它,否则返回304"not modified"应答
-Referer
包含一个URL,用户从该URL代表的页面出发访问当前请求的页面
-User-Agent
浏览器类型,如果Servlet返回的内容与浏览器类型有关则该值非常有用
继续就是
HeaderServlet.java
package test.demo1;
import java.io.IOException;
import java.util.Enumeration;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class HeaderServlet
*/
@WebServlet("/HeaderServlet")
public class HeaderServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public HeaderServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
Enumeration<String> ienum=request.getHeaderNames();
while(ienum.hasMoreElements()){
String name=ienum.nextElement();
String value=request.getHeader(name);
System.out.println(name+" -->> "+value);
}
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
http://localhost:8080/ServletDemo/HeaderServlet
在javaee平台下打开
accept -->> image/gif, image/jpeg, image/pjpeg, application/x-ms-application, application/xaml+xml, application/x-ms-xbap, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*
accept-language -->> zh-Hans-CN,zh-Hans;q=0.7,ja;q=0.3
ua-cpu -->> AMD64
accept-encoding -->> gzip, deflate
user-agent -->> Mozilla/5.0 (Windows NT 6.2; Win64; x64; Trident/7.0; rv:11.0) like Gecko
host -->> localhost:8080
connection -->> Keep-Alive
accept -->> image/gif, image/jpeg, image/pjpeg, application/x-ms-application, application/xaml+xml, application/x-ms-xbap, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*
accept-language -->> zh-Hans-CN,zh-Hans;q=0.7,ja;q=0.3
ua-cpu -->> AMD64
accept-encoding -->> gzip, deflate
user-agent -->> Mozilla/5.0 (Windows NT 6.2; Win64; x64; Trident/7.0; rv:11.0) like Gecko
host -->> localhost:8080
connection -->> Keep-Alive
在ie edge下打开
accept -->> text/html, application/xhtml+xml, image/jxr, */*
accept-language -->> zh-Hans-CN,zh-Hans;q=0.7,ja;q=0.3
user-agent -->> Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2486.0 Safari/537.36 Edge/13.10586
accept-encoding -->> gzip, deflate
host -->> localhost:8080
connection -->> Keep-Alive