Reponse

作者: 丫甘九 | 来源:发表于2019-04-01 21:29 被阅读0次

    内容
    reponse
    request
    编码
    路径


    服务器处理请求的流程
    服务器每次收到请求时,都会为这个请求开辟一个新的线程,
    服务器会把客户端的请求数据封装在request对象中,request就是请求数据的载体,(袋子),
    服务器还会创建reponse对象,这个对象和客户端连接在一起,它可以用来向客户端发送响应(手机)


    响应头

    200 ok //状态码 对状态码的解释
    Content-type:text/html,charset=utf-8 //响应头的类型以及编码格式
    //响应头空一行

                                                                          //html代码
    <html>                                                     
    false
    false
    false
    </html>
    

    reponse发送状态码

    回忆一下,http协议中响应的内容包含哪些东西呢?
    状态码:200表示成功,302重定向,404客户端错误,505服务器错误

    sendError(int sc)发送错误的状态码
    sendError(int sc,String msg)发送错误的状态码的同时还发送一个错误信息
    setStatus(int sc),发送正常的状态码,可以用来发送302
    与http协议相关

    案例

    发送404

    package baoming;
    
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import java.io.IOException;
    
    public class AServlet extends HttpServlet {
        public  void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException{
            response.sendError(404,"您访问的资源存在,就是不给你看");
    
        }
    }
    
    
    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee 
    http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
             version="4.0">
        <servlet>
            <servlet-name>AServlet</servlet-name>
            <servlet-class>baoming.AServlet</servlet-class>
    
        </servlet>
        <servlet-mapping>
            <servlet-name>AServlet</servlet-name>
            <url-pattern>/AServlet</url-pattern>
        </servlet-mapping>
    </web-app>
    
    image.png

    reponse的响应头相关方法

    用reponse这个东西发送响应头给浏览器看浏览器能读得懂这些头
    响应头:Content-type,Location等
    头就是一个键值对!可能会存在一个头(一个名称一个值),也可能会存在一个头(一个名称多个值)

    • response.setHeader(String name,String value);
      适用于单值的响应头,例如 response.setHeader("aaa","AAA");重要
    • response.addHeader(String name,String value);
      适用于多值的响应头,例如:
      response.addHeader("aaa","A");
      response.addHeader("aaa","AA");
      response.addHeader("aaa","AAA");
    • response.setIntHeader(String name,int value);
      适用于单值的int类型的响应头,例如:response.setIntHeader("Content-Length",123);
    • response.addIntHeader(String name,int value);
      适用于多值的int类型的响应头
    • response.setDateHeader(String name,long value);
      适用于单值的毫秒类型的响应头, response.setDateHeader("aaa",10006060*24);
    • response.addDateHeader(String name,long value);
      适用于单值的毫秒类型的响应头,response.addDateHeader("aaa",10006060*24);

    reponse完成重定向

    案例:

    • 发送302,设置Location头(服务器相当于10086,你给10086打电话,让他告诉你为什么1+1=2;10086说他解决不了,叫你去打119,这里的119就相当于Location头,是服务器访问一个页面访问不了,服务器重新给了一个地址,然后让浏览器去访问他),完成重定向
    package baoming;
    
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import java.io.IOException;
    
    public class AServlet extends HttpServlet {
        /*演示重定向*/
    
        /*用户请求Aservlet,然后Aservlet响应302,给出Location头*/
        public  void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException{
            System.out.println("输出AServlet");
    
            /*重定向
            * 1、设置Location头
            * 2、发送状态码*/
            response.setHeader("Location","/day10/BServlet");
            response.setStatus(302);
    
        }
    }
    
    
    package baoming;
    
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import java.io.IOException;
    
    public class BServlet extends HttpServlet {
        /*浏览器会重定向到这里来*/
        public  void doGet(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {
            System.out.println("CServlet");
    
    
        }
    }
    
    
    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee 
    http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
             version="4.0">
        <servlet>
            <servlet-name>AServlet</servlet-name>
            <servlet-class>baoming.AServlet</servlet-class>
    
        </servlet>
        <servlet-mapping>
            <servlet-name>AServlet</servlet-name>
            <url-pattern>/AServlet</url-pattern>
        </servlet-mapping>
        <servlet>
            <servlet-name>BServlet</servlet-name>
            <servlet-class>baoming.BServlet</servlet-class>
    
        </servlet>
        <servlet-mapping>
            <servlet-name>BServlet</servlet-name>
            <url-pattern>/BServlet</url-pattern>
        </servlet-mapping>
    </web-app>
    
    image.png

    image.png
    • 定时刷新,设置Refresh头
    package baoming;
    
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import java.io.IOException;
    import java.io.PrintWriter;
    
    public class CServlet extends HttpServlet {
        /*演示定时刷新*/
    
        /*设置一个Refresh,它表示定时更新*/
        public  void doGet(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {
            /*下面是用来发送响应体*/
            PrintWriter writer = response.getWriter();
            writer.print("欢迎登录,5秒钟后会自动跳转到主页,你看到的一定是乱码");
    
             /*设置名为Refresh的响应头*/
            response.setHeader("Refresh","10;URL=/day10/BServlet");
    
    
        }
    }
    
    
    package baoming;
    
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import java.io.IOException;
    
    public class BServlet extends HttpServlet {
    
        public  void doGet(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {
            response.getWriter().print("还是乱码");
    
    
        }
    }
    
    
    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee 
    http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
             version="4.0">
        <servlet>
            <servlet-name>AServlet</servlet-name>
            <servlet-class>baoming.AServlet</servlet-class>
    
        </servlet>
        <servlet-mapping>
            <servlet-name>AServlet</servlet-name>
            <url-pattern>/AServlet</url-pattern>
        </servlet-mapping>
        <servlet>
            <servlet-name>BServlet</servlet-name>
            <servlet-class>baoming.BServlet</servlet-class>
    
        </servlet>
        <servlet-mapping>
            <servlet-name>BServlet</servlet-name>
            <url-pattern>/BServlet</url-pattern>
        </servlet-mapping>
        <servlet>
            <servlet-name>CServlet</servlet-name>
            <servlet-class>baoming.CServlet</servlet-class>
    
        </servlet>
        <servlet-mapping>
            <servlet-name>CServlet</servlet-name>
            <url-pattern>/CServlet</url-pattern>
        </servlet-mapping>
    </web-app>
    
    image.png
    image.png
    • reponse头,禁用浏览器缓存 Cache-Control,pragma, expirea
    package baoming;
    
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import java.io.IOException;
    
    public class BServlet extends HttpServlet {
        /*禁用浏览器缓存*/
        /* Cache-Control,pragma, expirea*/
    
        public  void doGet(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {
            response.setHeader("Cache-Control","no-cache");
            response.setHeader("pragma","no-cache");
           response.setDateHeader("expirea",-1);/*表示不缓存*/
    
        }
    }
    
    
    • <meta>标签可以代替响应头
    <meta http-equiv="keyvords" content="keyvord1,keyvord2,keyvord3">
    

    响应体,通常是html,也可以是图片

    response的俩个流,

    • ServletOutputStrem,用来向客户端发送字节数据
      ServletOutputStrem out = response.getOutputStrem();
    • PrintWriter,用来向客户发送字符数据,需要设置编码
      PrintWriter writer = response.getWriter();
      注:俩个流不能同时使用
      案例:
    • 使用PrintWriter发送字符数据
     String s = "hello ServletOutputStrem";
            byte[] bytes =  s.getBytes();
            response.getOutputStream().write(bytes);
    
    • 使用ServletOutputStrem发送字节数据(图片)
    String path = "D:/D/搜狗高速下载/aa.jpg";
            FileInputStream in = new FileInputStream(path);
            byte[] bytes = IOUtils.toByteArray(in);
            response.getOutputStream();
    
    

    快捷重定向方法

     /*快捷重定向方法*/
            response.sendRedirect("http://www.baidi.com");
    
    

    相关文章

      网友评论

          本文标题:Reponse

          本文链接:https://www.haomeiwen.com/subject/cljbbqtx.html