–HTTP響應的格式
–設定響應報頭
–常見MIME類型
–常見HTTP 1.1響應報頭
–建構Excel電子表格
–動态生成JPEG圖像
–定時重新整理頁面
###################Michael分割線#######################
• HTTP響應的格式
<a href="http://redking.blog.51cto.com/attachment/200906/22/27212_1245711405wo1Z.png"></a>
• 設定響應報頭
– 設定任意響應頭
• 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類型
<a href="http://redking.blog.51cto.com/attachment/200906/22/27212_1245711409aR9U.png"></a>
• 常見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電子表格
<a href="http://redking.blog.51cto.com/attachment/200906/22/27212_1245711413gjmS.png"></a>
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.
public void doPost(HttpServletRequest request, HttpServletResponse response)
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 {
}
<a href="http://redking.blog.51cto.com/attachment/200906/22/27212_12457114190lm9.png"></a>
測試
<a href="http://redking.blog.51cto.com/attachment/200906/22/27212_1245711424w5hT.png"></a>
<a href="http://redking.blog.51cto.com/attachment/200906/22/27212_12457114293EWI.png"></a>
• 動态生成JPEG圖像
<a href="http://redking.blog.51cto.com/attachment/200906/22/27212_1245711775rltu.png"></a>
PrintPictureServlet.java
import java.io.InputStream;
import java.io.OutputStream;
public class PrintPictureServlet extends HttpServlet {
public PrintPictureServlet() {
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);
<a href="http://redking.blog.51cto.com/attachment/200906/22/27212_1245711781hVoD.png"></a>
看下效果
• 定時重新整理頁面
<a href="http://redking.blog.51cto.com/attachment/200906/22/27212_12457117913lBD.png"></a>
TimeServlet.java
import java.util.Date;
public class TimeServlet extends HttpServlet {
public TimeServlet() {
response.setHeader("refresh", "1");
response.setContentType("text/html;charset=gbk");
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();
<a href="http://redking.blog.51cto.com/attachment/200906/22/27212_1245711796RdP3.png"></a>
看下效果:
<a href="http://down.51cto.com/data/2353142" target="_blank">附件:http://down.51cto.com/data/2353142</a>
本文轉自redking51CTO部落格,原文連結:http://blog.51cto.com/redking/168991,如需轉載請自行聯系原作者