servlet学习笔记—HttpServletResponse接口

servlet学习笔记—HttpServletResponse接口

  1. HttpServletResponse接口介绍
    • HttpServletResponse接口来自于Servlet规范中,在Tomcat中存在servlet-api.jar
    • HttpServletResponse接口实现类有Http服务器负责提供。
    • HttpServletResponse接口负责将doGet/doPost方法执行结果写入到【响应体】交给浏览器
    • 开发人员习惯将HttpServletResponse接口修饰的对象称为【响应对象】
  2. 主要功能
    • 将执行结果以二进制形式写入到【响应体】。
    • 设置响应头中【content-type】属性值,从而控制浏览器使用对应编译器将响应体二进制数据编译为【文字、图片、视频、命令】
    • 设置响应头中【location】属性,将一个请求地址赋值给location,从而控制浏览器向指定服务器发送请求。
  3. 代码实现
    1. 案例一 (通过输出流,将执行结果以二进制形式写入到响应体)

      创建OneServlet实现类,重写doGet方法。

      public class OneServlet extends HttpServlet{
          @Override
          protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
              String result = "Hello World";//执行结果
              //---------------响应对象将结果写入响应体---------
                  //1.通过响应对象,向Tomcat索要输出流
                  PrintWriter out = resp.getWriter();
                  //2.通过输出流,将执行结果以二进制形式写入到响应体
                  out.write(result);
              //---------------响应对象将结果写入响应体---------
      
          }//doGet执行完毕
          //Tomcat将响应包推送给浏览器
      }
      

      配置web.xml

      <servlet>
              <servlet-name>OneServlet</servlet-name>
              <servlet-class>com.ljq.servlet.OneServlet</servlet-class>
          </servlet>
      
          <servlet-mapping>
              <servlet-name>OneServlet</servlet-name>
              <url-pattern>/one</url-pattern>
          </servlet-mapping>
      

      配置tomcat,然后启动服务,访问http://localhost:8080/04_servlet_war_exploded/one

      页面显示 “Hello world”

    2. 案例二(使用print将执行结果写入到影响体)

      创建TwoServlet实现类,重写doGet方法。

      public class TwoServlet extends HttpServlet {
          @Override
          protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
      
              int money = 50;
              PrintWriter out = resp.getWriter();
              out.write(money);
          }
      }
      

      配置web.xml启动服务后,访问http://localhost:8080/04_servlet_war_exploded/two发现页面显示的结果是“2”。

      我们的money是50,为什么到浏览器接收的数据变成2了呢?

      问题原因是:out.write方法可以将【字符】、【字符串】、【ASCII码】写入到响应体重。

      而50在ASCII码中对应的是2,所以我们的页面响应的数据是2.

      解决方案是:在实际开发中,都是通过out.print()将真实数据写入到响应体中。

      public class TwoServlet extends HttpServlet {
          @Override
          protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
      
              int money = 50;
              PrintWriter out = resp.getWriter();
              out.print(money);
          }
      }
      

      这样我们的页面显示的就是50了。

    3. 案例三(设置响应头中的content-type)

      创建ThreeServlet实现类,重写doGet方法。

      public class ThreeServlet  extends HttpServlet {
          @Override
          protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
              String result = "java<br/>python<br/>c++";
              PrintWriter out = resp.getWriter();
              out.print(result);
          }
      }
      

      我们期望的结果浏览器输出

      java
      python
      c++
      

      但是我们启动服务以后发现浏览输出的结果是

      java<br/>python<br/>c++
      

      这个结果并没有像我们设想的那样
      标签在浏览器中起到换行效果。

      这是因为浏览器在接收到响应包以后,根据【响应头content-type】属性值,来采用对应【编译器】对【响应体中二进制内容】进行编译处理,并且在默认情况下,content-type的值是“text”,content-type=“text”,此时浏览器将会采用【文本编译器】对响应体二进制数据进行编译。

      所以我们要像在浏览器中让我们
      标签其作用,我们要修改content-type的值。

      public class ThreeServlet  extends HttpServlet {
          @Override
          protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
              String result = "java<br/>python<br/>c++"; //既有文字又有html标签
              resp.setContentType("text/html");
              PrintWriter out = resp.getWriter();
              out.print(result);
          }
      }
      

      重启服务,这是浏览器的输出结果为

      java
      python
      c++
      

      如果我们要在浏览器中显示汉字比如这样

      public class ThreeServlet  extends HttpServlet {
          @Override
          protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
              String result = "java<br/>python<br/>c++"; //既有文字又有html标签
              String result2 = "<br/>狗子<br/>猫咪"; //汉字
              resp.setContentType("text/html");
              PrintWriter out = resp.getWriter();
              out.print(result);
              out.print(result2);
          }
      }
      

      重启服务,发现显示结果为

      java
      python
      c++
      ??
      ??
      

      这是因为在Content-Type中text/html;charset=ISO-8859-1,这里charset是不支持汉语的,所以我们要将charset改为utf-8

      public class ThreeServlet  extends HttpServlet {
          @Override
          protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
              String result = "java<br/>python<br/>c++"; //既有文字又有html标签
              String result2 = "<br/>狗子<br/>猫咪"; //汉字
              resp.setContentType("text/html;charset=utf-8");
              PrintWriter out = resp.getWriter();
              out.print(result);
              out.print(result2);
          }
      }
      

      重启服务,这时浏览器实现就是我们想要的结果

      java
      python
      c++
      狗子
      猫咪
      
    4. 案例四(设置响应头中【location】属性)

      创建FourServlet实现类,重写doGet方法。

      public class FourServlet extends HttpServlet {
          @Override
          protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
              String url = "http://www.baidui.com";
              resp.sendRedirect(url);// 响应头 locattion=“http://www.baidui.com”
          }
      }
      
      • 浏览器接收到响应包之后,如果发现响应头中存在loaction属性,自动通过地址栏向location指定网站发送请求。
      • sendRedirect方法远程控制浏览器请求行为【请求地址,请求方式,请求参数】

      配置好web.xml和Tomcat后,启动服务,访问http://localhost:8080/04_servlet_war_exploded/four,网页自动跳转到百度页面。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值