热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

javaweb学习总结二十(http响应)

一:http响应

1:http响应的组成部分

状态行、响应头、空行、响应数据

javaweb学习总结二十(http响应)

 

 

2:状态行

 

javaweb学习总结二十(http响应)

 

常用状态码:

200:请求成功

302:请求路径已经转移,请访问别的地址

307或者304: 请访问缓存信息

404:访问资源不存在

403:资源存在,但是没有访问权限

500:服务器内部错误

 

二:HTTP协议响应头

1:常用响应头

javaweb学习总结二十(http响应)

 

 

2:常用响应头分析演示

1):location:http://www.it315.org/index.jsp   和403搭配使用,告诉浏览器重定向到其他的页面

代码如下:

 1 public class ServletDemo extends HttpServlet {
 2 
 3     public void doGet(HttpServletRequest request, HttpServletResponse response)
 4             throws ServletException, IOException {
 5         doPost(request, response);
 6     }
 7 
 8     public void doPost(HttpServletRequest request, HttpServletResponse response)
 9             throws ServletException, IOException {
10         response.setStatus(302); // 302状态码代表资源路径转移,重定向到新的资源路径
11         response.setHeader("location", "/servletDemo/1.html");
12 
13     }
14 
15 }

web.xml配置:

 1 xml version="1.0" encoding="UTF-8"?>
 2 <web-app version="2.5" 
 3     xmlns="http://java.sun.com/xml/ns/javaee" 
 4     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
 5     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
 6     http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
 7   <servlet>
 8     <description>This is the description of my J2EE componentdescription>
 9     <display-name>This is the display name of my J2EE componentdisplay-name>
10     <servlet-name>ServletDemoservlet-name>
11     <servlet-class>com.hlcui.servlet.ServletDemoservlet-class>
12   servlet>
13 
14   <servlet-mapping>
15     <servlet-name>ServletDemoservlet-name>
16     <url-pattern>/servlet/ServletDemourl-pattern>
17   servlet-mapping>
18  
19   <welcome-file-list>
20     <welcome-file>index.jspwelcome-file>
21   welcome-file-list>
22 web-app>

tomcat服务器启动后,访问路径:http://localhost:8080/servletDemo/servlet/ServletDemo

javaweb学习总结二十(http响应)

 

2):Server  Apache tomcat  告诉浏览器服务器的类型信息

3):Content-Encoding:gzip  告诉浏览器服务器回送数据的压缩类型

4):Content-Length: 80   告诉浏览器呢服务器回送数据大小

演示代码如下:

 1 public class ServletDemo extends HttpServlet {
 2 
 3     public void doGet(HttpServletRequest request, HttpServletResponse response)
 4             throws ServletException, IOException {
 5         doPost(request, response);
 6     }
 7 
 8     public void doPost(HttpServletRequest request, HttpServletResponse response)
 9             throws ServletException, IOException {
10         String cOntent= "wertyuiopdfghjkl;dfghjklertyuiodfghjkcvbnmcvb"
11                 + "wertyuiopdfghjkl;dfghjklertyuiodfghjkcvbnmcvbtyuiopvbhnjk"
12                 + "wertyuiopdfghjkl;dfghjklertyuiodfghjkcvbnmcvbtyuiopvbhnjk"
13                 + "wertyuiopdfghjkl;dfghjklertyuiodfghjkcvbnmcvbtyuiopvbhnjk"
14                 + "wertyuiopdfghjkl;dfghjklertyuiodfghjkcvbnmcvbtyuiopvbhnjk"
15                 + "wertyuiopdfghjkl;dfghjklertyuiodfghjkcvbnmcvbtyuiopvbhnjk"
16                 + "wertyuiopdfghjkl;dfghjklertyuiodfghjkcvbnmcvbtyuiopvbhnjk";
17 
18         System.out.println("压缩前数据大小:" + content.getBytes().length);
19         ByteArrayOutputStream array = new ByteArrayOutputStream();
20         GZIPOutputStream gzip = new GZIPOutputStream(array);
21         gzip.write(content.getBytes()); // 将内容进行压缩
22         gzip.close();
23 
24         System.out.println("压缩后数据大小:" + array.size());
25         // 告诉浏览器数据压缩格式以及大小
26         response.setHeader("Content-Encoding", "gzip");
27         response.setHeader("Content-Length", array.size() + "");
28         // 将内容写到浏览器
29         response.getOutputStream().write(array.toByteArray());
30 
31     }
32 
33 }

启动tomcat服务器,访问路径:http://localhost:8080/servletDemo/servlet/ServletDemo

浏览器显示压缩后的数据:

javaweb学习总结二十(http响应)

javaweb学习总结二十(http响应)

 

5):Content-Language:zh-cn  告诉浏览器服务器的语言环境

6):Content-Type:text/html ; charset=utf-8 告诉浏览器服务器回送数据的内容类型

代码如下:

 1 public class ServletDemo extends HttpServlet {
 2 
 3     public void doGet(HttpServletRequest request, HttpServletResponse response)
 4             throws ServletException, IOException {
 5         doPost(request, response);
 6     }
 7 
 8     public void doPost(HttpServletRequest request, HttpServletResponse response)
 9             throws ServletException, IOException {
10         // 告诉浏览器显示内容格式
11         //response.setContentType("image/jpeg");
12         // 或者设置响应头的方式
13         response.setHeader("Content-Type", "image/jpeg");
14         // 读取图片文件
15         InputStream in = this.getServletContext().getResourceAsStream("/1.jpg");
16         int len = 0;
17         byte[] buf = new byte[1024];
18         while ((len = in.read(buf)) > 0) {
19             response.getOutputStream().write(buf, 0, len);
20         }
21 
22     }
23 
24 }

查看文件响应头的写法,tomcat服务器到 conf/web.xml

javaweb学习总结二十(http响应)

 

重新部署服务,访问url:http://localhost:8080/servletDemo/servlet/ServletDemo

javaweb学习总结二十(http响应)

7):Last-Modified:服务器告诉浏览器资源最后的修改时间,如果修改时间和缓存一直,那么就使用缓存时间,如果比缓存时间

更新,那么就重新发送数据到浏览器。

8):Refresh:1;url=http://www.baidu.com  服务器告诉浏览器每隔1秒刷新一次

代码如下:

 1 public class ServletDemo extends HttpServlet {
 2 
 3     public void doGet(HttpServletRequest request, HttpServletResponse response)
 4             throws ServletException, IOException {
 5         doPost(request, response);
 6     }
 7 
 8     public void doPost(HttpServletRequest request, HttpServletResponse response)
 9             throws ServletException, IOException {
10         Date date = new Date();
11         SimpleDateFormat sdf = new SimpleDateFormat("yyyy:MM:dd HH:mm:ss");
12 
13         response.setHeader("refresh", "1"); // 让日期时间每隔1秒刷新1次
14         response.getOutputStream().write(sdf.format(date).getBytes());
15     }
16 
17 }

javaweb学习总结二十(http响应)

refresh请求头还可以实现定时重定向页面的作用,例如注册页面注册成功后跳转到首页:

代码如下:

 1 public class ServletDemo extends HttpServlet {
 2 
 3     public void doGet(HttpServletRequest request, HttpServletResponse response)
 4             throws ServletException, IOException {
 5         doPost(request, response);
 6     }
 7 
 8     public void doPost(HttpServletRequest request, HttpServletResponse response)
 9             throws ServletException, IOException {
10         String msg = "页面跳转中...";
11         response.setHeader("refresh", "5;url='http://www.baidu.com'"); // 让日期时间每隔1秒刷新1次
12         response.getOutputStream().write(msg.getBytes());
13     }
14 
15 }

访问页面5秒后进行页面跳转!!!

9):  content-disposition :attachment;filename=""  服务器告诉浏览器以下载的方式处理文件

 1 public class ServletDemo extends HttpServlet {
 2 
 3     public void doGet(HttpServletRequest request, HttpServletResponse response)
 4             throws ServletException, IOException {
 5         doPost(request, response);
 6     }
 7 
 8     public void doPost(HttpServletRequest request, HttpServletResponse response)
 9             throws ServletException, IOException {
10         if ("download".equals(request.getParameter("type"))) {
11             response.setHeader("content-disposition",
12                     "attachment;filename=1.jpg");
13             InputStream in = this.getServletContext().getResourceAsStream(
14                     "/1.jpg");
15             OutputStream out = response.getOutputStream();
16             int len = 0;
17             byte[] buf = new byte[1024];
18             while ((len = in.read(buf)) > 0) {
19                 out.write(buf, 0, len);
20             }
21             out.flush();
22             out.close();
23             in.close();
24         } else {
25             response.setCharacterEncoding("utf-8");
26             response.setContentType("text/html");
27             PrintWriter pw = response.getWriter();
28             pw.print("");
29             pw.print(""
34                     +"");
35             /*pw
36                     .print("下载");*/
37             pw.print("");
38             pw.print("");
39             pw.close();
40 
41         }
42 
43     }
44 }

访问url:http://localhost:8080/servletDemo/servlet/ServletDemo

显示“下载”按钮,点击下载按钮下载文件:

javaweb学习总结二十(http响应)

javaweb学习总结二十(http响应)

10):Cache-control:no-cache  或者 pragma : no-cache 或者 expires :-1都是控制浏览器

是否缓存服务器数据,一般情况下都写上,就不会有浏览器兼容问题。

 

11):range 断续下载

javaweb学习总结二十(http响应)

如果下载服务器的某个资源,已经下载一半,突然网络断了,下次有网络再下载另一半资源,迅雷下载比较常见:

代码演示:

 1 /**
 2  * 
 3  */
 4 package com.hlcui.socket;
 5 
 6 import java.io.FileOutputStream;
 7 import java.io.IOException;
 8 import java.io.InputStream;
 9 import java.net.HttpURLConnection;
10 import java.net.MalformedURLException;
11 import java.net.URL;
12 
13 /**
14  * @author Administrator 实现断续下载文件
15  */
16 public class DownloadRange {
17 
18     /**
19      * @param args
20      * @throws IOException
21      */
22     public static void main(String[] args) throws IOException {
23         URL url = new URL("http://localhost:8080/servletDemo/111.txt");
24         HttpURLConnection cOnn= (HttpURLConnection) url.openConnection();
25 
26         // 设置断续下载,从第4个字符开始下载
27         conn.addRequestProperty("range", "bytes=4-");
28         InputStream in = conn.getInputStream();
29         FileOutputStream out = new FileOutputStream("F:\\111.txt",true); //追加到内容的末尾
30         int len = 0;
31         byte[] buf = new byte[1024];
32         while ((len = in.read(buf)) > 0) {
33             out.write(buf, 0, len);
34         }
35         in.close();
36         out.close();
37 
38     }
39 
40 }

代码均已经验证!


推荐阅读
  • 这是原文链接:sendingformdata许多情况下,我们使用表单发送数据到服务器。服务器处理数据并返回响应给用户。这看起来很简单,但是 ... [详细]
  • 本文介绍了使用AJAX的POST请求实现数据修改功能的方法。通过ajax-post技术,可以实现在输入某个id后,通过ajax技术调用post.jsp修改具有该id记录的姓名的值。文章还提到了AJAX的概念和作用,以及使用async参数和open()方法的注意事项。同时强调了不推荐使用async=false的情况,并解释了JavaScript等待服务器响应的机制。 ... [详细]
  • Windows下配置PHP5.6的方法及注意事项
    本文介绍了在Windows系统下配置PHP5.6的步骤及注意事项,包括下载PHP5.6、解压并配置IIS、添加模块映射、测试等。同时提供了一些常见问题的解决方法,如下载缺失的msvcr110.dll文件等。通过本文的指导,读者可以轻松地在Windows系统下配置PHP5.6,并解决一些常见的配置问题。 ... [详细]
  • http:my.oschina.netleejun2005blog136820刚看到群里又有同学在说HTTP协议下的Get请求参数长度是有大小限制的,最大不能超过XX ... [详细]
  • Webmin远程命令执行漏洞复现及防护方法
    本文介绍了Webmin远程命令执行漏洞CVE-2019-15107的漏洞详情和复现方法,同时提供了防护方法。漏洞存在于Webmin的找回密码页面中,攻击者无需权限即可注入命令并执行任意系统命令。文章还提供了相关参考链接和搭建靶场的步骤。此外,还指出了参考链接中的数据包不准确的问题,并解释了漏洞触发的条件。最后,给出了防护方法以避免受到该漏洞的攻击。 ... [详细]
  • 本文介绍了南邮ctf-web的writeup,包括签到题和md5 collision。在CTF比赛和渗透测试中,可以通过查看源代码、代码注释、页面隐藏元素、超链接和HTTP响应头部来寻找flag或提示信息。利用PHP弱类型,可以发现md5('QNKCDZO')='0e830400451993494058024219903391'和md5('240610708')='0e462097431906509019562988736854'。 ... [详细]
  • WebSocket与Socket.io的理解
    WebSocketprotocol是HTML5一种新的协议。它的最大特点就是,服务器可以主动向客户端推送信息,客户端也可以主动向服务器发送信息,是真正的双向平等对话,属于服务器推送 ... [详细]
  • 本文讨论了如何在codeigniter中识别来自angularjs的请求,并提供了两种方法的代码示例。作者尝试了$this->input->is_ajax_request()和自定义函数is_ajax(),但都没有成功。最后,作者展示了一个ajax请求的示例代码。 ... [详细]
  • 网络请求模块选择——axios框架的基本使用和封装
    本文介绍了选择网络请求模块axios的原因,以及axios框架的基本使用和封装方法。包括发送并发请求的演示,全局配置的设置,创建axios实例的方法,拦截器的使用,以及如何封装和请求响应劫持等内容。 ... [详细]
  • android listview OnItemClickListener失效原因
    最近在做listview时发现OnItemClickListener失效的问题,经过查找发现是因为button的原因。不仅listitem中存在button会影响OnItemClickListener事件的失效,还会导致单击后listview每个item的背景改变,使得item中的所有有关焦点的事件都失效。本文给出了一个范例来说明这种情况,并提供了解决方法。 ... [详细]
  • 本文讨论了Alink回归预测的不完善问题,指出目前主要针对Python做案例,对其他语言支持不足。同时介绍了pom.xml文件的基本结构和使用方法,以及Maven的相关知识。最后,对Alink回归预测的未来发展提出了期待。 ... [详细]
  • ZSI.generate.Wsdl2PythonError: unsupported local simpleType restriction ... [详细]
  • 本文介绍了Web学习历程记录中关于Tomcat的基本概念和配置。首先解释了Web静态Web资源和动态Web资源的概念,以及C/S架构和B/S架构的区别。然后介绍了常见的Web服务器,包括Weblogic、WebSphere和Tomcat。接着详细讲解了Tomcat的虚拟主机、web应用和虚拟路径映射的概念和配置过程。最后简要介绍了http协议的作用。本文内容详实,适合初学者了解Tomcat的基础知识。 ... [详细]
  • Android系统移植与调试之如何修改Android设备状态条上音量加减键在横竖屏切换的时候的显示于隐藏
    本文介绍了如何修改Android设备状态条上音量加减键在横竖屏切换时的显示与隐藏。通过修改系统文件system_bar.xml实现了该功能,并分享了解决思路和经验。 ... [详细]
  • 解决nginx启动报错epoll_wait() reported that client prematurely closed connection的方法
    本文介绍了解决nginx启动报错epoll_wait() reported that client prematurely closed connection的方法,包括检查location配置是否正确、pass_proxy是否需要加“/”等。同时,还介绍了修改nginx的error.log日志级别为debug,以便查看详细日志信息。 ... [详细]
author-avatar
手机用户2502861713
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有