Response

Response笔记

1. 响应对象概述

1.1 什么是响应对象

响应,它表示了服务器端收到请求,同时也已经处理完成,把处理的结果告知用户。简单来说,指的就是服务器把请求的处理结果告知客户端。在B/S架构中,响应就是把结果带回浏览器。

响应对象,顾名思义就是用于在JavaWeb工程中实现上述功能的对象。就是我们的HttpServletResponse(此对象的实现类是由Tomcat提供)

1.2 响应对象的状态码

状态码说明
200成功
302重定向
404请求资源未找到
405请求方式不支持
500服务器错误

总结

  • 1xx:服务器就收客户端消息,但没有接受完成,等待一段时间后,发送1xx多状态码

  • 2xx:成功。代表:200

  • 3xx:重定向。代表:302(重定向)

  • 4xx:客户端错误。404(请求路径没有对应的资源) 405:请求方式没有对应的doXxx方法

  • 5xx:服务器端错误。代表:500(服务器内部出现异常)

1.3 响应对象的常用响应头

名称说明
Location请求重定向的地址,常与302配合使用。
Content-Type响应正文的MIME类型, 编码格式

2. 重定向功能

2.1 重定向的概念

客户端的一次请求到达后,发现需要借助其他 Servlet 来实现功能。比如ServletA和ServletB, ServletA完成后会返回给浏览器, 并告诉浏览器去找ServletB, 并且将ServletB的地址写在响应头里告诉浏览器. 浏览器就会根据返回的信息向ServletB发起请求.

2.2 重定向的实现

  1. 设置状态码为302
  2. 设置响应头location
@WebServlet("/demo1")
public class ServletDemo1 extends HttpServlet {
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //重定向分为两步
        //1. 设置状态码为302
        response.setStatus(302);
        //2. 设置响应头location
        response.setHeader("Location","https://www.baidu.com/");
    }

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

简化版:

@WebServlet("/demo1")
public class ServletDemo1 extends HttpServlet {
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //重定向分为两步
        //1. 设置状态码为302
        //2. 设置响应头locatio
        response.sendRedirect("https://www.baidu.com/");
    }

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

2.3 重定向与转发的区别

重定向的特点:redirect

  1. 地址栏发生变化

  2. 重定向可以访问其他站点(服务器)的资源

  3. 重定向是两次请求。不能使用request对象来共享数据

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

3. 向页面响应数据

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

  1. 获取字符输出流

    返回值方法名说明
    ServletOutputStreamgetOutputStream()获取响应字节输出流对象
  2. 输出数据

@WebServlet("/demo3")
public class ServletDemo3 extends HttpServlet {
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1. 得到字节流
        ServletOutputStream outputStream = response.getOutputStream();
        //2. 向页面写数据
        outputStream.write("Hello yll".getBytes());
    }

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

假如输出中文,会出现乱码问题, 解决办法就是设置我们的响应头

返回值方法名说明
voidsetContentType(“text/html;charset=UTF-8”)设置响应内容类型,解决中文乱码问题
@WebServlet("/demo3")
public class ServletDemo3 extends HttpServlet {
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 设置响应头解决中文乱码问题
        //response.setHeader("Content-Type","text/html;charset=UTF-8");
        // 简化版
        response.setContentType("text/html;charset=UTF-8");

        //1. 得到字节流
        ServletOutputStream outputStream = response.getOutputStream();
        //2. 向页面写数据
        outputStream.write("做人不能程冠希,要会自己修主机!".getBytes());
    }

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

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

  1. 获取字符输出流

    返回值方法名说明
    PrintWritergetWriter()获取响应字符输出流对象
  2. 输出数据

@WebServlet("/demo4")
public class ServletDemo4 extends HttpServlet {
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 解决输出中文乱码问题
        response.setContentType("text/html;charset=UTF-8");

        // 1.获取字符流
        PrintWriter out = response.getWriter();
        // 2.输出数据
        out.write("做人不能丧心病狂");
    }

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

4. 综合案例

4.1 登录案例

  1. 编写登录页面, 包含用户名,密码;
  2. 编写后台代码实现如下功能
    1. 用户名密码正确, 登陆成功, 在页面显示: “你好, XXX”
    2. 用户名密码错误, 登陆失败, 在页面显示: “该用户名密码不正确, 请重新登录”, 重新登录为超链接形式, 点击可跳转到登录页面

代码如下:

登录页面:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>登录页面</title>
</head>
<body>
    
    <form action="/day04/login" method="post">
        用户名: <input type="text" name="username"><br>
        密码: <input type="text" name="password"><br>
        <input type="submit" value="注册">
    </form>


</body>
</html>

后台代码:

@WebServlet("/login")
public class LoginServlet extends HttpServlet {
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1. 获取用户名,密码
        String username = request.getParameter("username");
        String password = request.getParameter("password");


        response.setContentType("text/html;charset=UTF-8");
        //2. 验证用户名密码  root 123
        if("root".equals(username) && "123".equals(password)){
            //2.1 登陆成功, 输出"你好, XXX"到页面
            ServletOutputStream out = response.getOutputStream();
            String msg = "你好,"+username;
            out.write(msg.getBytes());
        }else{
            //2.2 登陆失败, 在页面显示: "该用户名密码不正确, 请重新登录",
            // 重新登录为超链接形式, 点击可跳转到登录页面
            ServletOutputStream out = response.getOutputStream();
            out.write("该用户名密码不正确, 请<a href=\"/day04/login.html\">重新登录</a>".getBytes());
        }
    }

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

4.2 验证码案例

使用给出的验证码Servlet完成如下要求

  1. 在页面上显示验证码
  2. 完成点击验证码切换功能

页面代码如下

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>验证码页面</title>
    <script>
        /*
        点击图片,需要换一张
        * 1.给图片绑定点击事件
        * 2.触发点击事件时重新设置src的属性
        * */
        window.onload=function () {
            //1.得到图片对象
            var img = document.getElementById("checkImg");
            //2. 给图片绑定点击事件
            img.onclick=function () {
                //3.得到当前时间,作为时间戳
                var time = new Date().getTime();
                //3. 重置图片的src属性
                img.src="/day04/checkCode?time"+time;
            }
        }
    </script>
</head>
<body>
    <img id="checkImg" src="/day04/checkCode">
</body>
</html>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值