Servlet technology 13_HttpServletResponse class

The role of the HttpServletResponse class:

The HttpServletResponse class is the same as the HttpServletRequest class. Every time a request comes in, the Tomcat server will create a Response object and pass it to the Servlet program to use. HttpServletRequest represents the requested information, HttpServletResponse represents all the response information, if we need to set the information returned to the client, we can set it through the HttpServletResponse object

Description of the two output streams:

Byte stream getOutputStream(); Often used for downloading (transferring binary data)
Character stream getWriter (); Often used to return strings (commonly used)

Only one of the two streams can be used at the same time.

If the byte stream is used, the character stream can no longer be used, and vice versa, otherwise an error will be reported.

public class ResponseIOServlet extends HttpServlet {
    
    
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    
    
        resp.getWriter();
        resp.getOutputStream();
    }
}

Insert picture description here

How to send data back to the client:

Requirement: Send string data back to the client.

public class ResponseIOServlet extends HttpServlet {
    
    
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    
    
//        resp.getWriter();
//        resp.getOutputStream();

//        要求:往客户端回传字符串数据。
        PrintWriter writer = resp.getWriter();      //快捷键:alt + enter
        writer.write("response's content!!!");
    }
}

To solve the garbled response:

public class ResponseIOServlet extends HttpServlet {
    
    
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    
    
//        resp.getWriter();
//        resp.getOutputStream();

//        要求:往客户端回传字符串数据。
        PrintWriter writer = resp.getWriter();      //快捷键:alt + enter
//        writer.write("response's content!!!");
        writer.write("旭哥好帅");
    }
}

If the write is in Chinese, there will be garbled problems

Insert picture description here

Take a look at the corresponding character set:

//获取响应的字符集
System.out.println(resp.getCharacterEncoding());        //默认ISO-8859-1

We can set the character set of the server:

//设置服务器字符集为UTF-8
resp.setCharacterEncoding("UTF-8");

At this time, the problem is still not resolved, because the browser does not know which character set the server uses, we also need to set the character set of the browser
Insert picture description here

The browser uses GBK by default

We can set the browser to also use the UTF-8 character set through the response header

//通过响应头,设置浏览器也使用UTF-8字符集
resp.setHeader("Content-Type","text/html; charset=UTF-8");

Insert picture description here

Complete code:

//        第一种方法
        //设置服务器字符集为UTF-8
        resp.setCharacterEncoding("UTF-8");
        //通过响应头,设置浏览器也使用UTF-8字符集
        resp.setHeader("Content-Type","text/html; charset=UTF-8");

There is a simpler way:

//        第二种方法
//        他会同时设置服务器和客户端都是用UTF-8字符集,还设置了响应头
//        注意:此方法一定要在获取流对象之前才有效
        resp.setContentType("text/html; charset=UTF-8");
        System.out.println(resp.getCharacterEncoding());

Request redirection:

Request redirection means that the client sends a request to the server, and then the server tells the client that I will give you some addresses and you go to a new address to visit, which is called request redirection (because the previous address may have been abandoned).

Insert picture description here

public class Response1 extends HttpServlet {
    
    
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    
    
        System.out.println("曾到此地一游 Response1");

        //设置响应状态码302,表示重定向(已搬迁)
        resp.setStatus(302);
        resp.setHeader("Location","Http://localhost:8080/07_servlet/response2");
    }
}
public class Response2 extends HttpServlet {
    
    
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    
    
        resp.getWriter().write("response2's result!");
    }
}

Insert picture description here

Features of request redirection:

  1. The browser address bar will change
http://localhost:8080/07_servlet/response1
变成了  ==>
http://localhost:8080/07_servlet/response2
  1. Two requests

Insert picture description here

  1. Do not share data in the Request field

    Reason: Tomcat parses the requested data every time it receives a request and encapsulates it into a request object

  2. Cannot access resources under WEB-INF

    Put a form.html in the WEB-INF directory

public class Response1 extends HttpServlet {
    
    
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    
    
        System.out.println("曾到此地一游 Response1");

        req.setAttribute("key1","value1");

        //设置响应状态码302,表示重定向(已搬迁)
        resp.setStatus(302);
//        resp.setHeader("Location","Http://localhost:8080/07_servlet/response2");
        resp.setHeader("Location","Http://localhost:8080/07_servlet/WEB-INF/form.html");
    }
}

​ 访问http://localhost:8080/07_servlet/response1:

Insert picture description here

​ Reason:

​ The second request still sends the request from the browser to the server, but the WEB-INF directory is protected and the browser cannot directly access

  1. Can access resources outside the project
resp.setHeader("Location","Http://www.baidu.com");

​ Can be accessed normally:

Insert picture description here

The second method of request redirection: (recommended)

//请求重定向的第二种方法:
resp.sendRedirect("http://localhost:8080");
`
resp.setHeader("Location","Http://www.baidu.com");

Guess you like

Origin blog.csdn.net/weixin_45024585/article/details/108905105