Response对象、完成重定向、服务器输出字符数据到浏览器、服务器输出字节数据到浏览器、验证码

Response对象

功能:设置响应消息

设置响应行

1.格式: HTTP/1.1 200 ok
2.设置状态码: setStatus(int sc)

设置响应头

setHeader(String name, String value)

设置响应体

使用步骤:
1.获取输出流
字符输出流: PrintWriter getWriter()
字节输出流: ServletoutputStream getoutputStream()
2.使用输出流,将数据输出到客户端浏览器


案例:

1.完成重定向

ResponseDemo1

/**
 * 重定向
 */
@WebServlet("/responseDemo1")
public class ResponseDemo1 extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        System.out.println("demo1...");

//        //访问/responseDemo1,会跳转知道/responseDemo2资源
//        //1.设置状态码302
//        response.setStatus(302);
//        //2.设置响应头location
//        response.setHeader("location","/responseDemo2");

        //动态获取虚拟目录
        String contextPath = request.getContextPath();

        //简单的重定向方法
        response.sendRedirect(contextPath+"/responseDemo2");
        //response.sendRedirect("https://www.baidu.com");
    }

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

ResponseDemo2

@WebServlet("/responseDemo2")
public class ResponseDemo2 extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("demo2...");
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
         this.doPost(request,response);
    }
}
  • 重定向的特点: redirect
    1.地址栏发生变化
    2.重定向可以访问其他站点(服务器)的资源
    3.重定向是两次请求。不能使用request对象来共享数据

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

补充:路径写法


  • 相对路径:
  • 通过相对路径不可以确定唯一资源
    如: ./index.html
    不以/开头,以.开头路径;
    规则:找到当前资源和目标资源之间的相对位调关系
    ./:当前目录
    ../:后退一级目录

  • 绝对路径:通过绝对路径可以确定唯一资源
    如: http://localhost/Respionse/responseDemo2
    /开头的路径
  • 规则:
  1. 给客户端浏览器使用:需要加虚拟目录(项目的访问路径);
  2. 建议虚拟目录动态获取:request.getContextpath();
  3. 给服务器使用:不需要加虚拟目录;

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

@WebServlet("/responseDemo3")
public class ResponseDemo3 extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        //获取流对象之前,设置流的默认编码
        response.setCharacterEncoding("utf-8");

        //告诉浏览器服务器发送消息体数据的编码。建议浏览器使用此编码解码
        //response.setHeader("content-type","text/html;charset=utf-8");

        //简单的形式。设置编码
        response.setContentType("text/html;charset=utf-8");

        //1.获取字符输出流
        PrintWriter pw = response.getWriter();
        //2.输出数据
        pw.write("<h1> 你好 hello response</h1>");

    }

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

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

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

乱码问题:
PrintWriter pw = response. getWriter( ); 获取的流的默认编码是ISO-8859-1
解决方法:

  1. 设置该流的默认编码
    response.setCharacterEncoding("utf-8");
  2. 告诉浏览器响应体使用的编码
    response.setHeader("content-type","text/html;charset=utf-8");

简单的形式,设置编码,是在获取流之前设置
response.setContentType("text/html;charset=utf-8");


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

@WebServlet("/responseDemo4")
public class ResponseDemo4 extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=utf-8");

        //1.获取字节输出流
        ServletOutputStream sos = response.getOutputStream();
        //2.输出数据
        sos.write("你好".getBytes());
    }

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

服务器输出字节数据到浏览器
步骤:
1.获取字节输出流
2.输出数据


4.验证码

CheckCodeServlet

@WebServlet("/checkCodeServlet")
public class CheckCodeServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        int width = 100;
        int height = 50;

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

        //2.美化图片
        //填充背景色
        Graphics g = image.getGraphics();//画笔对象
        g.setColor(Color.GRAY);
        g.fillRect(0,0,width,height);

        //画边框
        g.setColor(Color.BLUE);
        g.drawRect(0,0,width-1,height-1);
        
        //写验证码
        //创建随机下标
        g.setColor(Color.white);
        String str = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";

        Random ran = new Random();
        for(int i =1;i<=4;i++){
            int index = ran.nextInt(str.length());
            //获取字符
            char ch = str.charAt(index);//随机字符
            g.drawString(ch+"",width/5*i,height/2);
        }

        //画干扰线
        g.setColor(Color.GREEN);

        //随机生成坐标点
        for (int i=1;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,x2,y1,y2);
        }
        //3.将图片输出到页面展示
        ImageIO.write(image,"jpg",response.getOutputStream());
    }

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

register.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>注册页面</title>
</head>
<body>
    <img id="checkCode" src="/Response/checkCodeServlet"/>

    <a id="change" href="#">看不清换一张</a>
    
    <script>
        /*
        点击超链接或者图片,需要换一张
        1.给超链接和图片绑定单击事件
        2.重新设置图片的src属性值
         */

        //1.获取图片对象
        var img = document.getElementById("checkCode");
        //2.绑定单击事件
        img.onclick = function(){
            //加时间戳
            var date = new Date().getTime();
            img.src = "/Response/checkCodeServlet?"+date;
        }
        
        var aa = document.getElementById("change");
        aa.onclick = function(){
            //加时间戳
            var date = new Date().getTime();
            img.src = "/Response/checkCodeServlet?"+date;
        }

    </script>

</body>
</html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值