6.6.5、生产者、消费者限定
6.6.5.1、基本概念
首先让我们看一下通过HTTP协议传输的媒体类型及如何表示媒体类型:
一、Media Type:
互联网媒体类型,一般就是我们所说的MIME类型,用来确定请求的内容类型或响应的内容类型。
type主类型,任意的字符串,如text,如果是*号代表所有;
subtype 子类型,任意的字符串,如html,如果是*号代表所有;
parameter 可选,一些参数,如Accept请求头的q参数, Content-Type的 charset参数。
详见http://tools.ietf.org/html/rfc2616#section-3.7
常见媒体类型:
text/html : HTML格式 text/plain :纯文本格式 text/xml :XML格式
image/gif :gif图片格式 image/jpeg :jpg图片格式 image/png:png图片格式
application/x-www-form-urlencoded : <form encType=””>中默认的encType,form表单数据被编码为key/value格式发送到服务器(表单默认的提交数据的格式)。
multipart/form-data : 当你需要在表单中进行文件上传时,就需要使用该格式;
application/xhtml+xml :XHTML格式 application/xml : XML数据格式
application/atom+xml :Atom XML聚合格式 application/json : JSON数据格式
application/pdf :pdf格式 application/msword : Word文档格式
application/octet-stream : 二进制流数据(如常见的文件下载)。
在如tomcat服务器的 “conf/web.xml”中指定了扩展名到媒体类型的映射,在此我们可以看到服务器支持的媒体类型。
二、Content-Type:内容类型,即请求/响应的内容区数据的媒体类型;
2.1、请求头的内容类型,表示发送到服务器的内容数据的媒体类型;
request中设置请求头“Content-Type: application/x-www-form-urlencoded”表示请求的数据为key/value数据;
(1、控制器cn.javass.chapter6.web.controller.consumesproduces.contenttype.RequestContentTypeController
- @RequestMapping(value = "/ContentType", method = RequestMethod.GET)
- public String showForm() throws IOException {
- //form表单,使用application/x-www-form-urlencoded编码方式提交表单
- return "consumesproduces/Content-Type";
- }
- @RequestMapping(value = "/ContentType", method = RequestMethod.POST,
- headers = "Content-Type=application/x-www-form-urlencoded")
- public String request1(HttpServletRequest request) throws IOException {
- //①得到请求的内容区数据的类型
- String contentType = request.getContentType();
- System.out.println("========ContentType:" + contentType);
- //②得到请求的内容区数据的编码方式,如果请求中没有指定则为null
- //注意,我们的CharacterEncodingFilter这个过滤器设置了编码(UTF-8)
- //编码只能被指定一次,即如果客户端设置了编码,则过滤器不会再设置
- String characterEncoding = request.getCharacterEncoding();
- System.out.println("========CharacterEncoding:" + characterEncoding);
- //③表示请求的内容区数据为form表单提交的参数,此时我们可以通过request.getParameter得到数据(key=value)
- System.out.println(request.getParameter("realname"));
- System.out.println(request.getParameter("username"));
- return "success";
- }
showForm功能处理方式:展示表单,且form的enctype="application/x-www-form-urlencoded",在提交时请求的内容类型头为“Content-Type:application/x-www-form-urlencoded”;
request1功能处理方法:只对请求头为“Content-Type:application/x-www-form-urlencoded”的请求进行处理(即消费请求内容区数据);
request.getContentType():可以得到请求头的内容区数据类型(即Content-Type头的值)
request.getCharacterEncoding():如“Content-Type:application/json;charset=GBK”,则得到的编码为“GBK”,否则如果你设置过滤器(CharacterEncodingFilter)则得到它设置的编码,否则返回null。
request.getParameter():因为请求的内容区数据为application/x-www-form-urlencoded格式的数据,因此我们可以通过request.getParameter()得到相应参数数据。
<