速解HTTP协议之response对象

6 篇文章 0 订阅


在这里插入图片描述

功能

设置响应消息
1. 设置响应行
		1. 格式:HTTP/1.1  200    ok
		2. 设置状态码: setStatic(int sc)
2. 设置相应头
	setHeader(String name,String value)
3. 设置响应体
   使用步骤:
   		1. 获取输出流
   				* 字符输出流:PrintWriter getWriter()
   				* 字节输出流: ServletOutputStream getOutStream()
   		2. 使用输出流,将数据输出到客户端浏览器

案例

完成重定向

资源跳转的方式。

重定向

代码实现:

这是第一个访问的Servlet (/rd1)
在这里插入图片描述
这是第二个Servlet (/rd2) 重定向来的

这是运行结果:

特点

转发特点:foward
	1. 转发地址栏路径不变
	2. 转发只能访问当前服务器下的资源
	3. 转发是一次请求,可以使用request对象来共享数据

重定向特点:redirect
	1. 转发地址栏路径发生变化
	2. 转发可以访问其他站点(服务器)的资源
	3. 转发是两次请求,不能使用request来共享数据

路径的写法

  1. 路径的分类
    1. 相对路径

        不能确定唯一资源
        如:./index.html
      
        规则:找到当前资源和目标资源的相对位置关系
            ./:当前目录
           ../:后退一级目录
      
    2. 绝对路径

       可以确定唯一的资源
       *如:http://localhost:8080/3_response_war_exploded/rd2
       *以/开头:/3_response_war_exploded/rd2
      
       规则:判断路径给谁用,判断请求从哪儿发出
       	*给客户端使用:需要加虚拟目录(项目的访问路径)
       	   *建议虚拟目录动态获取:request.getContextPath()
       	   *<a>,<form> 重定向...
       	*给服务器使用,不需要加虚拟目录
       	  * 转发路径
      

服务器输出字符数据到浏览器

步骤

  1. 获取字符输出流

  2. 输出数据

  3. 注意:

    乱码问题:
    1. PrintWriter pw = response.getWriter();
    2. 设置该流的默认编码
    3. 告诉浏览器,响应体用的编码
    
	//简单的形式,设置编码,在获取响应流之前设置
response.setContentType("text/html;charset=UTF-8");
  1. 代码:
package com.web.servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet(urlPatterns = "/rd3")
public class ResponseDemo3 extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
     //获取流对象之前,设置流的默认编码:ISO -8859-1  设置为GBK
        response.setContentType("text/html;charset=UTF-8");
        
        //1.获取字符输出流
        PrintWriter pw = response.getWriter();

        //2.输出数据
        pw.write("<h1>hello response<h1>");
        pw.write("你好 response");
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }
}

服务器输出字节数据到浏览器

步骤:

1.  获取字节输出流
2.  输出数据

代码:

package com.web.servlet;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author OldAZ-PC
 */
@WebServlet(urlPatterns = "/rd4")
public class ResponseDemo4 extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
     //获取流对象之前,设置流的默认编码:ISO -8859-1  设置为GBK
        response.setContentType("text/html;charset=UTF-8");


        //1.获取字节输出流
        ServletOutputStream sos = response.getOutputStream();


        //2.输出数据
        sos.write("hello".getBytes());

    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }
}

验证码

1. 本质:图片
2. 目的:防止恶意表单注册
  	注:以后写的话  ,几乎都是在网上找美观的代码。自己改!!!

代码:

package com.web.servlet;

import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.*;
import java.util.*;
import java.awt.image.BufferedImage;
import java.io.IOException;

/**
 * @author OldAZ-PC
 */
@WebServlet(urlPatterns = "/rd5")
public class ResponseDemo5 extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
     //获取流对象之前,设置流的默认编码:ISO -8859-1  设置为GBK
       // response.setContentType("text/html;charset=UTF-8");

            int width = 100;
            int height = 50;


        //1.创建一个对象,在内存中图片(验证码图片对象)
        BufferedImage image = new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);



        //2.美化图片
        //2.1 填充背景色
        Graphics g = image.getGraphics();  //画笔对象

        g.setColor(Color.pink);  //设置画笔颜色
        g.fillRect(0,0,width,height);

        //2.2 画边框
        g.setColor(Color.blue);
        g.drawRect(0,0,width-1,height-1);

        String str = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
        //生成随机角标
        Random ran = new Random();
        for (int i = 1; i <5 ; i++) {
            int index = ran.nextInt(str.length());
            //获取字符
            char ch = str.charAt(index);//随机字符
            //2.3写验证码
            g.drawString(ch+"",width/5*i,height/2);
        }


            //2.4画干扰线
        g.setColor(Color.green);
        //随机生成坐标点
        for (int i = 0; i <10 ; i++) {
            int x1 = ran.nextInt(width);
            int x2 = ran.nextInt(width);
            int y1 = ran.nextInt(height);
            int y2 = ran.nextInt(height);

            g.drawLine(x1,y1,x2,y2);
        }



        //3.将图片展示到页面
        ImageIO.write(image,"jpg",response.getOutputStream());

    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }
}

HTML页面代码:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<img id="checkCode" src="/3_response_war_exploded/rd5"/>
<a id="change" herf="">看不清,换一张?</a>
</body>
<script>
/*
    分析:
      点击超链接或者图片,需要换一张
    1. 给超链接或者图片绑定单击事件
    2.重新设置图片的SRC属性值
*/
    window.onload = function () {
        //1.获取图片对象
        var img = document.getElementById("checkCode");
        img.onclick=function () {
            //时间戳
            var date = new Date().getTime();

            img.src = "/3_response_war_exploded/rd5?"+date;
        }



        var img2 = document.getElementById("change");
        img2.onclick = function () {
            //时间戳
            var date = new Date().getTime();

            img.src = "/3_response_war_exploded/rd5?"+date;
        }


    }


</script>
</html>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值