• 04JavaServlet-25. 设置响应消息头的使用


    https://blog.csdn.net/shentian885/article/details/108541881

    //添加头部信息
    
    resp<span class="token punctuation">.</span><span class="token function">addHeader</span><span class="token punctuation">(</span><span class="token string">"名字"</span><span class="token punctuation">,</span><span class="token string">"值"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token comment">//添加该名字与值</span>
    <span class="token keyword">boolean</span> falg <span class="token operator">=</span> resp<span class="token punctuation">.</span><span class="token function">containsHeader</span><span class="token punctuation">(</span><span class="token string">"名字"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token comment">//查询响应头中是否包含某个名字,存在返回true,否则返回 false</span>
    

    //添加响应头时,名字可以随便取,但是一般使用常见的名字

    <span class="token comment">//Server -&gt; tomcat版本</span>
    
    <span class="token comment">//Date -&gt; 时间</span>
        resp<span class="token punctuation">.</span><span class="token function">setDateHeader</span><span class="token punctuation">(</span><span class="token string">"Date"</span><span class="token punctuation">,</span><span class="token keyword">long</span><span class="token operator">-</span>时间的毫秒数<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment">//与resp.addHeader("Date","值")效果相同</span>
    
    <span class="token comment">//Content-Type -&gt; 响应返回的内容类型 </span>
        resp<span class="token punctuation">.</span><span class="token function">setContentType</span><span class="token punctuation">(</span><span class="token string">"text/html;charset=utf-8"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment">//与resp.addHeader("Content-Type","text/html;charset=utf-8")效果相同</span>
    
    <span class="token comment">//Location -&gt; 302时,返回请求资源的新路径</span>
    
    <span class="token comment">//Content-Encoding -&gt; 服务端能够发送压缩编码类型</span>
         resp<span class="token punctuation">.</span><span class="token function">addHeader</span><span class="token punctuation">(</span><span class="token string">"Content-Encoding"</span><span class="token punctuation">,</span><span class="token string">"gzip"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    
    <span class="token comment">//Content-Length -&gt; 服务器发送的压缩数据的长度</span>
        resp<span class="token punctuation">.</span><span class="token function">addHeader</span><span class="token punctuation">(</span><span class="token string">"Content-Length"</span><span class="token punctuation">,</span><span class="token string">"长度"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    
    <span class="token comment">//Content-language -&gt; 服务器发送的语言类型</span>
        resp<span class="token punctuation">.</span><span class="token function">addHeader</span><span class="token punctuation">(</span><span class="token string">"Content-Language"</span><span class="token punctuation">,</span><span class="token string">"zh-cn"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    
    <span class="token comment">//Last-Modified -&gt; 服务端对该资源最后的修改时间</span>
        resp<span class="token punctuation">.</span><span class="token function">addHeader</span><span class="token punctuation">(</span><span class="token string">"Last-Modifies"</span><span class="token punctuation">,</span><span class="token keyword">new</span> <span class="token class-name">Date</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">toString</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    
    <span class="token comment">//Content-Disposition -&gt; 服务器要求客户端以下载文件的方式打开该文件</span>
        resp<span class="token punctuation">.</span><span class="token function">addHeader</span><span class="token punctuation">(</span><span class="token string">"Content-Disposition"</span><span class="token punctuation">,</span><span class="token string">"attachment;filename=下载文件名.后缀名"</span><span class="token punctuation">)</span>
    
    <span class="token comment">//设置浏览器是否缓存</span>
        resp<span class="token punctuation">.</span><span class="token function">addHeader</span><span class="token punctuation">(</span><span class="token string">"Expires"</span><span class="token punctuation">,</span><span class="token string">"-1"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        resp<span class="token punctuation">.</span><span class="token function">addHeader</span><span class="token punctuation">(</span><span class="token string">"Cache-Control"</span><span class="token punctuation">,</span><span class="token string">"no-cache"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        resp<span class="token punctuation">.</span><span class="token function">addHeader</span><span class="token punctuation">(</span><span class="token string">"Pragma"</span><span class="token punctuation">,</span><span class="token string">"no-cache"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment">//针对不同浏览器的兼容</span>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
  • 相关阅读:
    python Json报错json.decoder.JSONDecodeError
    jupyter notebook改变行间图片大小
    SVM算法核函数的选择
    Linux中长时间运行程序的方法
    python通过多线程并获取返回值
    python多进程multiprocessing Pool相关问题
    pandas.read_sql_query()读取数据库数据用chunksize的坑
    Docker学习——Dockerfile
    Android之自定义ListView(一)
    Java编程思想——初始化与清理
  • 原文地址:https://www.cnblogs.com/sunny3158/p/14719350.html
Copyright © 2020-2023  润新知