Servlet学习笔记(HTTP请求详细了解以及文件下载示例)
来源:互联网 发布:组播mac地址有什么用 编辑:程序博客网 时间:2024/04/24 17:10
http协议
超文本传输协议(HyperText Transfer Protocol)是互联网上应用最为广泛的一种网络协议。
需要有以下的基本认识:
(1)http协议是建立在tcp/ip协议基础上
(2)http协议有1.0 以及 1.1版本 ,目前通用的是1.1版本。
其中:
http1.0 称为短连接
http1.1 称为长连接.
所谓长和短指的是持续时间的长短,长连接 1.1持续连接时间为30s ,短连接1.0是发送完数据就断开连接。
http请求部分
基本机构
GET /test/hello.html HTTP/1.1 [请求行]Accept: */* [消息名] 消息名:内容Referer: http://localhost:8080/test/abc.html Accept-Language: zh-cnUser-Agent: Mozilla/4.0Accept-Encoding: gzip, deflate Host: http://www.sohu.com:80Connection: Keep-Alive [消息头格式 (消息名: 内容 )
注意,不同的请求消息头不一定一样
发送的内容 [格式 : 内容名字=内容体]
请求行中的GET称之为请求方式,请求方式有:post、get、head、options、delete、trace、put
常用的有:post、get
example:
GET News/abc.jsp
http请求消息头
- Accept: text/html,image/* [告诉服务器,我可以接受 文本,网页,图片]
- Accept-Charset: ISO-8859-1 [接受字符编码 iso-8859-1]
- Accept-Encoding: gzip,compress [可以接受 gzip,compress压缩后数据.]
- Accept-Language: en-us,zh-cn [浏览器支持中,英文]
- Host: www.sohu.com:80 [我要找主机是 www.sohu.com:80]
- If-Modified-Since: Tue, 11 Jul 2000 18:23:51 GMT [ 告诉服务器,我的缓冲中有这个资源文件,该文件的时间是 。。。]
- Referer: http://www.sohu.com/index.jsp [告诉服务器,我来自哪里,该消息头,常用于防止盗链]
- User-Agent: Mozilla/4.0 (compatible; MSIE 5.5; Windows NT 5.0)[告诉服务器,浏览器内核]
- Cookie [cookie??]
- Connection: close/Keep-Alive [保持连接,发完数据后,我不关闭连接]
- Date: Tue, 11 Jul 2000 18:23:51 GMT [浏览器发送该http请求的时间]
运用referer防盗链
//防盗链部分,通过获取referer消息头实现String referer=request.getHeader("Referer"); if(referer==null||!referer.startsWith("http://localhost:8080/FileDownloadServlet")){ response.sendRedirect("/FileDownloadServlet/Error"); return; }
http相应部分
HTTP应答与HTTP请求相似,HTTP响应也由3个部分构成,分别是:
l 状态行
l 响应头(Response Header)
l 响应正文
在接收和解释请求消息后,服务器会返回一个HTTP响应消息。
HTTP/1.1 200 OK //响应行Date: Sat, 31 Dec 2005 23:59:59 GMT //消息头Content-Type: text/html;charset=ISO-8859-1 //消息头Content-Length: 122 //消息头<html><!--响应的实体内容--><head><title>Wrox Homepage</title></head><body><!-- body goes here --></body></html>
状态行:
格式:HTTP版本号 状态码 原因叙述
例子:HTTP/1.1 200 OK
状态码 含义
100-199 表示成功接收请求,要求客户端继续提交下一次请求才能完成整个处理过程
200-299 表示成功接收请求并完成整个处理过程,常用200
300-399 为完成请求,客户需要进行一步细化请求。
例如:请求的资源已经移动一个新的地址,常用302,307
400-499 客户端的请求有错误 404(找不到文件这种)
500-599 服务器端出现错误,常用500
http响应的状态行举例说明
200 就是整个请求和响应过程没有发生错误,这个最常见.
302: 表示当你请求一个资源的时候,服务器返回302 表示,让浏览器转向到另外一个资源,比如: response.sendRedirect(“/web应用/资源名”)
example:
response.setStatus(302);response.setHeader("Location", "/servletPro/Servlet2");//以上两行代码与下面一行代码等价response.sendRedirect("/servletPro/Servlet2");
404: 找不到资源
500: 服务器端错误
http响应消息头详解
Location: http://www.baidu.org/index.jsp 【让浏览器重新定位到url】Server:apache tomcat 【告诉浏览器使用的服务器为tomcat】Content-Encoding: gzip 【告诉浏览器使用 gzip进行压缩】Content-Length: 80 【告诉浏览器发送的数据大小80节】Content-Language: zh-cn 【支持中文】Content-Type: text/html; charset=GB2312 [内容格式text/html; 编码gb2312]Last-Modified: Tue, 11 Jul 2000 18:23:51 GMT 【告诉浏览器,该资源上次更新时间】Refresh: 1;url=http://www.baidu.com 【等待多少秒刷新到 http://www.baidu.com】Content-Disposition: attachment; filename=aaa.zip 【告诉浏览器,有文件下载】Transfer-Encoding: chunked [传输的编码]Set-Cookie:SS=Q0=5Lb_nQ; path=/search[?]Expires: -1[告诉浏览器如何缓存页面IE]Cache-Control: no-cache [告诉浏览器如何缓存页面火狐]Pragma: no-cache [告诉浏览器如何缓存页面]Connection: close/Keep-Alive [保持连接 1.1是Keep-Alive]Date: Tue, 11 Jul 2000 18:23:51 GMT
运动Refresh实现定时刷新
response.setHeader("Refresh","5;url=/servletPro/Servlet2");
文件下载:
fileDownload.html
<!DOCTYPE html><html><head><meta charset="UTF-8"><title>FileDownload</title></head><body><a href="/FileDownloadServlet/FileDownload?filename=1.jpg">1.jpg</a><a href="/FileDownloadServlet/FileDownload?filename=2.jpg">2.jpg</a><a href="/FileDownloadServlet/FileDownload?filename=3.jpg">3.jpg</a></body></html>
Servlet:
FileDownload.java
package com.cn;import java.io.FileInputStream;import java.io.IOException;import java.io.OutputStream;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;/** * Servlet implementation class FileDownload */public class FileDownload extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public FileDownload() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub response.setContentType("text/html;charset=utf-8"); response.setCharacterEncoding("utf-8"); //防盗链部分,通过获取referer消息头实现 String referer=request.getHeader("Referer"); if(referer==null||!referer.startsWith("http://localhost:8080/FileDownloadServlet")){ response.sendRedirect("/FileDownloadServlet/Error"); return; } //文件名获取及转码 String temp=request.getParameter("filename"); String filename=java.net.URLEncoder.encode(temp,"utf-8"); String r="attachment;filename="+temp; response.setHeader("Content-Disposition", r); //获取文件全路径 String path=this.getServletContext().getRealPath("/images/"+temp); //文件输入流 FileInputStream fis=new FileInputStream(path); //文件输出流 OutputStream os=response.getOutputStream(); //缓冲 byte[] buffer=new byte[1024]; int len=0; while((len=fis.read(buffer))>0){ os.write(buffer, 0, len); } os.close(); fis.close(); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); }}
- Servlet学习笔记(HTTP请求详细了解以及文件下载示例)
- Java Servlet学习笔记(四)Servlet客户端Http请求
- Servlet(10)Http协议、HTTP请求和响应头及其详解(文件下载)
- http请求下载文件
- Servlet学习笔记(八)—— 文件下载
- Servlet中的文件下载示例
- http请求下载文件(byte数组)
- http请求学习笔记
- servlet笔记(四)获取http请求头信息
- Servlet学习笔记(四):Servlet的请求与响应
- Servlet学习笔记(四):Servlet的请求与响应
- Servlet——使用http响应头Content-Disposition下载文件的示例
- Android之Http请求实例(文件上传+servlet接收)
- Ionic3学习笔记(十三)HttpClient 实现 HTTP 请求以及踩过的一些坑
- ESP8266--学习笔记(六)Http请求
- Servlet详细介绍,学习笔记
- Java Servlet学习笔记(五)Servlet服务器Http响应
- okhttp3+retrofit2进行HTTP请求以及设置请求超时示例
- 0.ARM裸机学前补充知识
- android:orientation
- 欢迎使用CSDN-markdown编辑器
- unity3d 第一天
- spring 常用配置
- Servlet学习笔记(HTTP请求详细了解以及文件下载示例)
- javascript的基础学习
- Reveal真机查看任意APP
- Android自定义控件:动画类----alpha、scale、translate、rotate、set的xml属性及用法
- jsp基础
- Milking Cows
- 11、mysql运算符----第2篇
- TOJ 3988.Password
- python 时间模块学习