Servlet响应头信息

Posted acgame

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Servlet响应头信息相关的知识,希望对你有一定的参考价值。

1.输出excel表格

 1 package servlet_pack;
 2 
 3 import java.io.IOException;
 4 import java.io.PrintWriter;
 5 
 6 import javax.servlet.ServletException;
 7 import javax.servlet.annotation.WebServlet;
 8 import javax.servlet.http.HttpServlet;
 9 import javax.servlet.http.HttpServletRequest;
10 import javax.servlet.http.HttpServletResponse;
11 
12 /**
13  * Servlet implementation class PrintExcel
14  */
15 @WebServlet("/PrintExcel")
16 public class PrintExcel extends HttpServlet {
17     private static final long serialVersionUID = 1L;
18        
19     /**
20      * @see HttpServlet#HttpServlet()
21      */
22     public PrintExcel() {
23         super();
24     }
25 
26     /**
27      * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
28      */
29     protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
30         response.setContentType("application/vnd.ms-excel");
31         PrintWriter out = response.getWriter();
32         out.println("\\tQ1\\tQ2\\tQ3\\tQ4\\ttotal");
33         out.println("apple\\t10\\t20\\t30\\t40\\t=sum(B2:E2)");
34         out.println("orange\\t11\\t22\\t33\\t44\\t=sum(B3:E3)");
35     }
36 
37     /**
38      * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
39      */
40     protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
41         doGet(request, response);
42     }
43 
44 }

运行结果:

技术分享图片

 

以上是关于Servlet响应头信息的主要内容,如果未能解决你的问题,请参考以下文章

java---servlet与filter的联系与区别

Servlet响应头信息

servlet,过滤器,监听器,拦截器的区别

servlet,filter,listener,intercepter区别

java 怎么修改servlet的请求头信息

Servlet-http消息头