Java学习笔记(七十)—— Response

功能

  设置响应消息

  • 设置响应行
  1. 格式:HTTP/1.1 200 ok
  2. 设置状态码:setStatus(int sc)
  • 设置响应头setHeader(String name,String value)
  • 设置相应体
  1. 使用步骤
    1.1 获取输出流
    1.1.1 字符输出流:PrintWriter getWriter()
    1.1.2 字符输出流:ServletOutputStream getOutputStream()
    1.2 使用输出流,将数据输出到客户端浏览器中
案例
  • 重定向
    		/*
    		// 设置状态码为302
            response.setStatus(302);
            // 设置响应头location
            response.setHeader("location","/day24_war_exploded/ResponseDemo2");
            */
            // 简单的重定向方法
            response.sendRedirect("/day24_war_exploded/ResponseDemo2");
    		// 动态获取虚拟目录,并重定向
    		// response.sendRedirect(request.getContextPath()+"/ResponseDemo2");
    		/*
    			转发的特点:forward
    				1.地址栏路径不变
    				2.只能访问当前服务器下的资源
    				3.转发是1次请求,可以使用request对象来共享数据
    			重定向的特点:redirect
    				1.地址栏路径改变
    				2.可以访问其他站点(服务器)的资源
    				3.重定向是2次请求,不可以使用request对象来共享数据
    			重定向路径的写法:
    				1.路径分类
    					1.相对路径:通过相对路径不可以确定唯一资源
    						* 不以/开头,以.开头的路径,./ResponseDemo2
    						* 规则:到到当前资源和目标资源之间的相对位置关系
    							* ./:当前目录
    							* ../:后退一级目录
    					2.绝对路径:通过绝对路径可以确定唯一资源
    						* 以/开头的路径,/day24_war_exploded/ResponseDemo2
    						* 规则:判断要定义的路径给谁用?
    							* 给客户端浏览器用:需要加虚拟目录(项目的访问路径)
    							* 给服务器使用:不需要加虚拟目录
    							* 动态获取虚拟目录 request.getContextPath()
    				2.
    		*/
  • 服务器输出字符数据到浏览器
    /*
    	步骤:
    		1.获取字符输出流
    		2.输出数据
    */
    		/*
    		// 在获取流对象之前,设置流的默认编码为utf-8
            response.setCharacterEncoding("utf-8"); 
            // 告诉浏览器服务器发送的消息体数据的编码,建议浏览器使用utf-8解码
            response.setHeader("content-type","text/html;charset=utf-8");
    		// 简单的形式,设置流的编码utf-8,并告诉浏览器使用uft-8解码
    		*/
    		response.setContentType("text/html;charset=utf-8");
    		PrintWriter writer = response.getWriter(); // 如果不设置流的编码,默认是ISO-8859-1的格式
    		writer.write("hello response "); // hello response写入页面
  • 服务器输出字节数据到浏览器
    response.setContentType("text/html;charset=utf-8");
            ServletOutputStream outputStream = response.getOutputStream();
            outputStream.write("heooll444实际".getBytes("utf-8"));
  • 验证码
    // 本质是一张图片
    // 为了防止恶意的注册
    		int width=100;
            int height=50;
            // 1.创建一个对象,在内存中去画图(验证码图片对象)
            BufferedImage img=new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
            // 2.美化图片
            // 2.1填充背景色
            Graphics graphics = img.getGraphics(); //画笔对象
            graphics.setColor(Color.PINK); // 设置画笔为粉红色
            graphics.fillRect(0,0,width,height);
            // 2.2 画边框
            graphics.setColor(Color.BLUE);
            graphics.drawRect(0,0,width-1,height-1);
            // 2.3写验证码
            String str="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
            // 生成随机角标
            Random random = new Random();
            for (int i = 1; i <= 4; i++) {
                int index = random.nextInt(str.length());
                // 获取字符
                char c = str.charAt(index);
                graphics.drawString(c+"",width/5*i,height/2);
            }
            // 2.4画干扰线
            graphics.setColor(Color.green);
            // 随机生成坐标点
            for (int i = 0; i < 20; i++) {
                int x1 = random.nextInt(width);
                int y1 = random.nextInt(height);
                int x2 = random.nextInt(width);
                int y2 = random.nextInt(height);
                graphics.drawLine(x1,y1,x2,y2);
            }
            // 3.将图片输出到页面展示
            ImageIO.write(img,"png",resp.getOutputStream());
发布了113 篇原创文章 · 获赞 1 · 访问量 920

猜你喜欢

转载自blog.csdn.net/weixin_44876003/article/details/103623068