Java EE WEB工程师培训-JDBC+Servlet+JSP整合开发之15.Servlet响应头信息

本文介绍了HTTP响应的基础知识,包括响应格式、报头设置等内容,并通过实例展示了如何利用Servlet构建Excel电子表格、动态生成JPEG图像及实现页面定时刷新。
–HTTP响应的格式
–设置响应报头
–常见MIME类型
–常见HTTP 1.1响应报头
–构建Excel电子表格
–动态生成JPEG图像
–定时刷新页面
###################Michael分割线#######################
• HTTP响应的格式
image
• 设置响应报头
– 设置任意响应头
• public void setHeader(String headerName, String headerValue)
– 设定任意的报头

• public void setDateHeader(String name,long millisecs)
– 将自1970年以来的毫秒数转换成GMT格式日期字符串
• public void setIntHeader(String name,int headerValue)
– 可以省去在调用setHeader之前将int转换成字符串的麻烦
• addHeader, addDateHeader, addIntHeader
– 增加新报头,而非替换已有的报头
– 普通响应报头的设置
• setContentType

– 设定Content-Type报头
– servlet几乎总会用到这个报头
– 参见常见MIME类型的表格

• setContentLength
– 设定Content-Length报头
– 用于持续性HTTP连接。
– 参见Connection请求报头

• addCookie
– 为Set-Cookie报头增加一个值
– 参见介绍cookie的部分

• sendRedirect
– 设定Location报头(以及改变状态代码)
• 常见MIME类型
image
• 常见HTTP 1.1响应报头
– Cache-Control (1.1) 和Pragma (1.0)
• no-cache值阻止浏览器缓存页面。
– Content-Disposition
• 通过这个报头,可以请求浏览器询问用户将响应存储到磁盘上给定名称的文件中
• Content-Disposition: attachment; filename=file-name

– Content-Encoding
• 文档的编码方式
– Content-Length
• 响应中的字节数
– Content-Type
• 返回文档时所采用的MIME类型。
• 使用setContentType设置这个报头。

– Expires
• 特定的一段时间,这段时间后应该将文档认作是过期,不应该再继续缓存
• 使用setDateHeader设置这个报头

– Last-Modified
• 文档最后被改动的时间
• 不要直接设置这个报头,而应该提供getLastModified方法

–Location
• 浏览器应该重新连接到的URL
• 不要直接设置这个报头,而要使用sendRedirect进行设定

–Refresh
• 多少秒后浏览器应该重新载入页面
–Set-Cookie
• 浏览器应该记下来的cookie。不要直接设置这个报头,而应该使用addCookie
–WWW-Authenticate
• 授权的类型和范围需要在Authorization报头中给出
• 构建Excel电子表格
image
Servlet_ResponseHeader
package com.michael.servlet;    

import java.io.IOException;    
import java.io.PrintWriter;    

import javax.servlet.ServletException;    
import javax.servlet.http.HttpServlet;    
import javax.servlet.http.HttpServletRequest;    
import javax.servlet.http.HttpServletResponse;    

public class PrintExcelServlet extends HttpServlet {    

         /**    
         * Constructor of the object.    
         */
    
         public PrintExcelServlet() {    
                 super();    
        }    

         /**    
         * Destruction of the servlet. <br>    
         */
    
         public void destroy() {    
                 super.destroy(); // Just puts "destroy" string in log    
                 // Put your code here    
        }    

         /**    
         * The doGet method of the servlet. <br>    
         *    
         * This method is called when a form has its tag value method equals to get.    
         *    
         * @param request the request send by the client to the server    
         * @param response the response send by the server to the client    
         * @throws ServletException if an error occurred    
         * @throws IOException if an error occurred    
         */
    
         public void doGet(HttpServletRequest request, HttpServletResponse response)    
                         throws ServletException, IOException {    

                doPost(request,response);    
        }    

         /**    
         * The doPost method of the servlet. <br>    
         *    
         * This method is called when a form has its tag value method equals to post.    
         *    
         * @param request the request send by the client to the server    
         * @param response the response send by the server to the client    
         * @throws ServletException if an error occurred    
         * @throws IOException if an error occurred    
         */
    
         public void doPost(HttpServletRequest request, HttpServletResponse response)    
                         throws ServletException, IOException {    

                response.setContentType( "application/vnd.ms-excel");    
                PrintWriter out = response.getWriter();    
                out.println( "\tQ1\tQ2\tQ3\tQ4\tTotal");    
                out.println( "Apples\t78\t87\t92\t29\t=SUM(B2:E2)");    
                out.println( "Oranges\t77\t86\t93\t30\t=SUM(B3:E3)");    
        }    

         /**    
         * Initialization of the servlet. <br>    
         *    
         * @throws ServletException if an error occure    
         */
    
         public void init() throws ServletException {    
                 // Put your code here    
        }    

}
image
测试
image
image
• 动态生成JPEG图像
image
PrintPictureServlet.java
package com.michael.servlet;    

import java.io.IOException;    
import java.io.InputStream;    
import java.io.OutputStream;    
import javax.servlet.ServletException;    
import javax.servlet.http.HttpServlet;    
import javax.servlet.http.HttpServletRequest;    
import javax.servlet.http.HttpServletResponse;    

public class PrintPictureServlet extends HttpServlet {    

         /**    
         * Constructor of the object.    
         */
    
         public PrintPictureServlet() {    
                 super();    
        }    

         /**    
         * Destruction of the servlet. <br>    
         */
    
         public void destroy() {    
                 super.destroy(); // Just puts "destroy" string in log    
                 // Put your code here    
        }    

         /**    
         * The doGet method of the servlet. <br>    
         *    
         * This method is called when a form has its tag value method equals to get.    
         *    
         * @param request the request send by the client to the server    
         * @param response the response send by the server to the client    
         * @throws ServletException if an error occurred    
         * @throws IOException if an error occurred    
         */
    
         public void doGet(HttpServletRequest request, HttpServletResponse response)    
                         throws ServletException, IOException {    

                doPost(request,response);    
        }    

         /**    
         * The doPost method of the servlet. <br>    
         *    
         * This method is called when a form has its tag value method equals to post.    
         *    
         * @param request the request send by the client to the server    
         * @param response the response send by the server to the client    
         * @throws ServletException if an error occurred    
         * @throws IOException if an error occurred    
         */
    
         public void doPost(HttpServletRequest request, HttpServletResponse response)    
                         throws ServletException, IOException {    

                response.setContentType( "image/jpeg");    
                InputStream in = this.getClass().getClassLoader().getResourceAsStream( "Michael.JPG");    
                 int len = in.available();    
                 byte[] buffer = new byte[len];    
                in.read(buffer);    
                 //输出流    
                OutputStream out = response.getOutputStream();    
                out.write(buffer);    
        }    

         /**    
         * Initialization of the servlet. <br>    
         *    
         * @throws ServletException if an error occure    
         */
    
         public void init() throws ServletException {    
                 // Put your code here    
        }    

}
image
看下效果
image 
• 定时刷新页面
image
TimeServlet.java
package com.michael.servlet;    

import java.io.IOException;    
import java.io.PrintWriter;    
import java.util.Date;    

import javax.servlet.ServletException;    
import javax.servlet.http.HttpServlet;    
import javax.servlet.http.HttpServletRequest;    
import javax.servlet.http.HttpServletResponse;    

public class TimeServlet extends HttpServlet {    

         /**    
         * Constructor of the object.    
         */
    
         public TimeServlet() {    
                 super();    
        }    

         /**    
         * Destruction of the servlet. <br>    
         */
    
         public void destroy() {    
                 super.destroy(); // Just puts "destroy" string in log    
                 // Put your code here    
        }    

         /**    
         * The doGet method of the servlet. <br>    
         *    
         * This method is called when a form has its tag value method equals to get.    
         *    
         * @param request the request send by the client to the server    
         * @param response the response send by the server to the client    
         * @throws ServletException if an error occurred    
         * @throws IOException if an error occurred    
         */
    
         public void doGet(HttpServletRequest request, HttpServletResponse response)    
                         throws ServletException, IOException {    

                doPost(request,response);    
        }    

         /**    
         * The doPost method of the servlet. <br>    
         *    
         * This method is called when a form has its tag value method equals to post.    
         *    
         * @param request the request send by the client to the server    
         * @param response the response send by the server to the client    
         * @throws ServletException if an error occurred    
         * @throws IOException if an error occurred    
         */
    
         public void doPost(HttpServletRequest request, HttpServletResponse response)    
                         throws ServletException, IOException {    
                response.setHeader( "refresh", "1");    

                response.setContentType( "text/html;charset=gbk");    
                PrintWriter out = response.getWriter();    
                out    
                                .println( "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");    
                out.println("<HTML>");    
                out.println("    <HEAD><TITLE>A Servlet</TITLE></HEAD>");    
                out.println("    <BODY>");    
                out.print("        当前页面刷新时间为: ");    
                out.print(new Date().toLocaleString());    
                out.println("    </BODY>");    
                out.println("</HTML>");    
                out.flush();    
                out.close();    
        }    

        /**    
         * Initialization of the servlet. <br>    
         *    
         * @throws ServletException if an error occure    
         */
    
        public void init() throws ServletException {    
                // Put your code here    
        }    

}
image
看下效果:
image 
image 
###################Michael分割线#######################
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值