Servlet学习笔记(HTTP请求详细了解以及文件下载示例)
2016-07-11 22:34
676 查看
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-cn User-Agent: Mozilla/4.0 Accept-Encoding: gzip, deflate Host: http://www.sohu.com:80 Connection: 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");
文件下载:
fileDown
4000
load.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); } }
相关文章推荐
- Java Servlet生成JSON格式数据并用jQuery显示的方法
- jsp和servlet中实现页面跳转的方式实例总结
- java servlet结合mysql搭建java web开发环境
- Servlet与JSP间的两种传值情况
- JSP、Servlet中get请求和post请求的区别总结
- jsp和servlet的区别探讨
- Struts2访问servlet分享
- java中Servlet处理乱码的方法
- Servlet 过滤器详细介绍
- JSP+Servlet+JavaBean实现登录网页实例详解
- servlet中session简介和使用例子
- 在Jsp Servlet中页面重新定向总汇
- JSP使用Servlet作为控制器实现MVC模式实例详解
- JSP使用Servlet过滤器进行身份验证的方法
- Servlet动态网页技术详解
- jsp和servlet操作mysql中文乱码问题的解决办法
- 基于jsp+servlet实现的简单博客系统实例(附源码)
- js调用后台servlet方法实例
- 利用JQuery和Servlet实现跨域提交请求示例分享