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

–HTTP响应的格式 
–设置响应报头 
–常见MIME类型 
–常见HTTP 1.1响应报头 
–构建Excel电子表格 
–动态生成JPEG图像 
–定时刷新页面
###################Michael分割线#######################
• HTTP响应的格式
Java EE WEB工程师培训-JDBC+Servlet+JSP整合开发之15.Servlet响应头信息
• 设置响应报头 
– 设置任意响应头 
• 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类型
Java EE WEB工程师培训-JDBC+Servlet+JSP整合开发之15.Servlet响应头信息
• 常见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电子表格
Java EE WEB工程师培训-JDBC+Servlet+JSP整合开发之15.Servlet响应头信息
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    
        }    


Java EE WEB工程师培训-JDBC+Servlet+JSP整合开发之15.Servlet响应头信息
测试
Java EE WEB工程师培训-JDBC+Servlet+JSP整合开发之15.Servlet响应头信息
Java EE WEB工程师培训-JDBC+Servlet+JSP整合开发之15.Servlet响应头信息
• 动态生成JPEG图像
Java EE WEB工程师培训-JDBC+Servlet+JSP整合开发之15.Servlet响应头信息
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    
        }    


Java EE WEB工程师培训-JDBC+Servlet+JSP整合开发之15.Servlet响应头信息
看下效果
Java EE WEB工程师培训-JDBC+Servlet+JSP整合开发之15.Servlet响应头信息 
• 定时刷新页面
Java EE WEB工程师培训-JDBC+Servlet+JSP整合开发之15.Servlet响应头信息
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    
        }    


Java EE WEB工程师培训-JDBC+Servlet+JSP整合开发之15.Servlet响应头信息
看下效果:
Java EE WEB工程师培训-JDBC+Servlet+JSP整合开发之15.Servlet响应头信息 
Java EE WEB工程师培训-JDBC+Servlet+JSP整合开发之15.Servlet响应头信息 
###################Michael分割线#######################

附件:http://down.51cto.com/data/2353142









本文转自redking51CTO博客,原文链接:http://blog.51cto.com/redking/168991,如需转载请自行联系原作者


上一篇:如何在SAP CRM里创建和消费Web service


下一篇:Java Web开发中验证码的实现(JSP验证码)