今天一天都在做查询页面的数据导入部分,本来打算今天能够弄出来的,不过最后还是有点小问题,没有把表格中的数据导进去。
一、eclipse快捷键
1、alt + shift + M:重构(提取代码)
二、HTTP响应头
1、常用响应头
- Content-Encoding:服务器通过这个头,告诉浏览器,数据的压缩格式
Content-Length: 服务器通过这个头,告诉浏览器回送数据的长度
显示结果如下:<pre class="java" name="code">package com.day04; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.util.zip.GZIPOutputStream; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @WebServlet("/ServletDemo1") public class ServletDemo1 extends HttpServlet { private static final long serialVersionUID = 1L; protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { test2(request, response); } //数据压缩 public void test2(HttpServletRequest request, HttpServletResponse response) throws IOException { String data = "abcdddddddddddddddddddddddddddddddddddddddddddd"; System.out.println("原始数据的大小为:" + data.getBytes().length); ByteArrayOutputStream bout = new ByteArrayOutputStream(); GZIPOutputStream gout = new GZIPOutputStream(bout); //buffer。自带有缓冲的 gout.write(data.getBytes()); gout.close(); //得到压缩后的数据 byte g[] = bout.toByteArray(); response.setHeader("Content-Encoding", "gzip"); response.setHeader("Content-Length", g.length + ""); response.getOutputStream().write(g); } }
- Content-Type:服务器通过这个头,告诉浏览器回送数据的类型(content中的c有区分大小写吗?)
图片放置位置:package com.day04; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.util.zip.GZIPOutputStream; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @WebServlet("/ServletDemo1") public class ServletDemo1 extends HttpServlet { private static final long serialVersionUID = 1L; protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { test3(request, response); } //指定回送数据类型 public void test3(HttpServletRequest request, HttpServletResponse response) throws IOException { response.setHeader("content-type", "image/jpg"); //在D:\apache-tomcat-7.0.54\conf\web.xml中搜索>jpg就能找到image/jpg InputStream in = this.getServletContext().getResourceAsStream("/1.jpg"); byte buffer[] = new byte[1024]; int len = 0; OutputStream out = response.getOutputStream(); while((len = in.read(buffer)) > 0) { out.write(buffer, 0, len); } } }
- Refresh:服务器通过这个头,告诉浏览器定时刷新
package com.day04; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.util.zip.GZIPOutputStream; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @WebServlet("/ServletDemo1") public class ServletDemo1 extends HttpServlet { private static final long serialVersionUID = 1L; protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { test4(request, response); } //指定浏览器定时刷新 public void test4(HttpServletRequest request, HttpServletResponse response) throws IOException { // response.setHeader("refresh", "3"); //每3秒刷新一次 response.setHeader("refresh", "3; url = 'http://www.sina.com'"); //3秒之后调到新浪主页 response.getWriter().write("abcd"); } }
- Content-Disposition:服务器通过这个头,告诉浏览器以下载方式打数据
package com.day04; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.util.zip.GZIPOutputStream; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @WebServlet("/ServletDemo1") public class ServletDemo1 extends HttpServlet { private static final long serialVersionUID = 1L; protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { test5(request, response); } //指定浏览器下载 public void test5(HttpServletRequest request, HttpServletResponse response) throws IOException { response.setHeader("content-disposition", "attachment;filename=xxx.jpg"); //要下载文件(1.jpg)显示的名称 InputStream in = this.getServletContext().getResourceAsStream("/1.jpg"); byte buffer[] = new byte[1024]; int len = 0; OutputStream out = response.getOutputStream(); while((len = in.read(buffer)) > 0) { out.write(buffer, 0, len); } } public void test3(HttpServletRequest request, HttpServletResponse response) throws IOException { response.setHeader("content-type", "image/jpg"); //在D:\apache-tomcat-7.0.54\conf\web.xml中搜索>jpg就能找到image/jpg InputStream in = this.getServletContext().getResourceAsStream("/1.jpg"); byte buffer[] = new byte[1024]; int len = 0; OutputStream out = response.getOutputStream(); while((len = in.read(buffer)) > 0) { out.write(buffer, 0, len); } } }
- 控制浏览器不要缓存:
Expires: -1
Cache-Control: no-cache
Pragma: no-cache