zoukankan      html  css  js  c++  java
  • Response用处

    一---HttpServletResponse

    web服务器接收到客户端的请求,针对这个请求,分别创建一个代表请求的HttpServletRequest对象,代表响应的一个HttpServletResponse。我们实际上就是操作这两个对象。

       如果要获取我们客户端请求过来的参数:找HttpServletRequest

       如果要给客户端响应一些信息:找HttpServletResponse

    1)简单分类

     负责向浏览器发送数据的方法

    ServletOutputStream getOutputStream() throws IOException; // 写平常的流
    
    PrintWriter getWriter() throws IOException; // 写中文用这个 

    负责向浏览器发送一些响应头的方法

    1     void setCharacterEncoding(String var1);
    2 
    3     void setContentLength(int var1);
    4 
    5     void setContentLengthLong(long var1);
    6 
    7     void setContentType(String var1);
    8       void setDateHeader(String var1, long var2);

    void addDateHeader(String var1, long var2);

    void setHeader(String var1, String var2);

    void addHeader(String var1, String var2);

    void setIntHeader(String var1, int var2);

    void addIntHeader(String var1, int var2);

    响应的状态码:

     1 int SC_CONTINUE = 100;
     2     int SC_SWITCHING_PROTOCOLS = 101;
     3     int SC_OK = 200;
     4     int SC_CREATED = 201;
     5     int SC_ACCEPTED = 202;
     6     int SC_NON_AUTHORITATIVE_INFORMATION = 203;
     7     int SC_NO_CONTENT = 204;
     8     int SC_RESET_CONTENT = 205;
     9     int SC_PARTIAL_CONTENT = 206;
    10     int SC_MULTIPLE_CHOICES = 300;
    11     int SC_MOVED_PERMANENTLY = 301;
    12     int SC_MOVED_TEMPORARILY = 302;
    13     int SC_FOUND = 302;
    14     int SC_SEE_OTHER = 303;
    15     int SC_NOT_MODIFIED = 304;
    16     int SC_USE_PROXY = 305;
    17     int SC_TEMPORARY_REDIRECT = 307;
    18     int SC_BAD_REQUEST = 400;
    19     int SC_UNAUTHORIZED = 401;
    20     int SC_PAYMENT_REQUIRED = 402;
    21     int SC_FORBIDDEN = 403;
    22     int SC_NOT_FOUND = 404;
    23     int SC_METHOD_NOT_ALLOWED = 405;
    24     int SC_NOT_ACCEPTABLE = 406;
    25     int SC_PROXY_AUTHENTICATION_REQUIRED = 407;
    26     int SC_REQUEST_TIMEOUT = 408;
    27     int SC_CONFLICT = 409;
    28     int SC_GONE = 410;
    29     int SC_LENGTH_REQUIRED = 411;
    30     int SC_PRECONDITION_FAILED = 412;
    31     int SC_REQUEST_ENTITY_TOO_LARGE = 413;
    32     int SC_REQUEST_URI_TOO_LONG = 414;
    33     int SC_UNSUPPORTED_MEDIA_TYPE = 415;
    34     int SC_REQUESTED_RANGE_NOT_SATISFIABLE = 416;
    35     int SC_EXPECTATION_FAILED = 417;
    36     int SC_INTERNAL_SERVER_ERROR = 500;
    37     int SC_NOT_IMPLEMENTED = 501;
    38     int SC_BAD_GATEWAY = 502;
    39     int SC_SERVICE_UNAVAILABLE = 503;
    40     int SC_GATEWAY_TIMEOUT = 504;
    41     int SC_HTTP_VERSION_NOT_SUPPORTED = 505;

    2)下载文件

     1:向浏览器输出消息

      2:下载文件

        1:要获取下载文件的路径

        2:下载的文件名

        3:想办法让浏览器能够支持我们需要下载的东西

        4:获取下载文件的输入流

        5:创建缓冲区

        6:获取OutputStream对象

        7:将FileOutputStream流写入到buffer缓冲区

        8:使用OutputStream流对象将缓冲区中的数据输出到客户端

     1 public class FileSeeervlet extends HttpServlet {
     2     @Override
     3     protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
     4         // 1:要获取下载文件的路径
     5         String realPath = "D:\\java31\\javaweb-02-servlet\\response\\src\\main\\resources\\尹雄.png";
     6         System.out.println("路径" + realPath);
     7         // 2:下载的文件名
     8         String filename = realPath.substring(realPath.lastIndexOf("\\") + 1);
     9         // 3:想办法让浏览器能够支持我们需要下载的东西,中文文件名用URLEncoder.encode(filename,"utf-8")编码,否则有可能乱码
    10         resp.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode(filename,"utf-8"));
    11         // 4:获取下载文件的输入流
    12         FileInputStream in = new FileInputStream(realPath);
    13         // 5:创建缓冲区
    14         int len = 0;
    15         byte[] buffer = new byte[1024];
    16         // 6:获取OutputStream对象
    17         ServletOutputStream out = resp.getOutputStream();
    18         // 7:将FileOutputStream流写入到buffer缓冲区
    19         // 使用OutputStream流对象将缓冲区中的数据输出到客户端
    20         while((len = in.read(buffer)) > 0) {
    21             out.write(buffer, 0, len);
    22         }
    23 
    24         in.close();
    25         out.close();
    26 
    27     }
    28 
    29     @Override
    30     protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    31         doGet(req, resp);
    32     }
    33 }

    3.验证码功能

    验证怎么来的?

    1)前端实现

    2)后端实现,需要用到java的图片类,生成一个图片

     1 public class ImageServlet extends HttpServlet {
     2     @Override
     3     protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
     4         // 如何让浏览器5秒自动刷新一次;
     5         resp.setHeader("refresh","3");
     6         // 在内存中创建一个图片
     7         BufferedImage image = new BufferedImage(80,20,BufferedImage.TYPE_INT_RGB);
     8         // 得到图片
     9         Graphics2D g =(Graphics2D) image.getGraphics(); // g是一支2D的笔
    10         // 设置图片的背景颜色
    11         g.setColor(Color.WHITE);
    12         g.fillRect(0,0,80,20);
    13         // 给图片写数据
    14         g.setColor(Color.BLUE);
    15         g.setFont(new Font(null,Font.BOLD,20));
    16         g.drawString(makeNum(),0,20);
    17 
    18         // 告诉浏览器这个请求用图片的方式打开
    19         resp.setContentType("image/jpeg");
    20         // 网站是有缓存的,我们不让浏览器缓存
    21         resp.setDateHeader("expires",-1);
    22         resp.setHeader("Cache-Control","no-cache");
    23         resp.setHeader("Progma","no-cache");
    24 
    25         //把图片写给浏览器
    26         boolean write = ImageIO.write(image,"jpg", resp.getOutputStream());
    27     }
    28 
    29     // 生成随机数
    30     private String makeNum () {
    31         Random random = new Random();
    32         String num = random.nextInt(99999999) + "";
    33         StringBuffer sb = new StringBuffer();
    34         // 保证都是7位数
    35         for (int i = 0; i < 7 - num.length() ;  i++) {
    36             sb.append("0");
    37         }
    38         String s = sb.toString() + num;
    39         return num;
    40     }
    41 
    42 
    43 
    44 
    45     @Override
    46     protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    47        doGet(req, resp);
    48     }
    49 }
  • 相关阅读:
    文件下载链接
    Python Web服务器
    打印字符串替换
    python 将数据写入excel
    python Telnet通讯
    Python 串口通讯
    monkey 原理,环境搭建、命令详解
    Andriod 四大组件,六大布局
    python练习题100例
    Activity的基本概念与Activity的生命周期
  • 原文地址:https://www.cnblogs.com/YXBLOGXYY/p/14606962.html
Copyright © 2011-2022 走看看