[Servlet] 5: Detailed explanation of the response object HttpServletResponse

Table of contents

| Response object HttpServletResponse interface

Basic overview of HttpServletResponse

Response object returns String to Browser & Chinese garbled problem

Response object returns int to Browser

The response object is returned to the Browser after parsing HTML tags


This article belongs to the third part of the back-end full set of notes

(Updating) [Getting started with the back end and getting into the soil! 】Java+Servlet+JDBC+SSM+SpringBoot+SpringCloud Basic Introduction_m0_57265007's Blog - An article on the CSDN blog, from getting started with the back end to entering the soil. Contains Java Basic + Advanced, MySQL, JDBC, Servlet, SSM, SpringBoot, SpringCloud, project notes. https://blog.csdn.net/m0_57265007/article/details/127962617?csdn_share_tail=%7B%22type%22%3A%22blog%22%2C%22rType%22%3A%22article%22%2C%22rId%22 %3A%22127962617%22%2C%22source%22%3A%22m0_57265007%22%7D

| Response object HttpServletResponse interface


Basic overview of HttpServletResponse

What is HttpServletResponse?

  • HttpServletResponse comes from the Servlet specification and is an interface

  • Its implementation class is provided by the HTTP server (Tomcat)

  • The implementation class object of HttpServletResponse is called the response object

The main function?

  • Write the execution result of the doGet/doPost method to the [Response Body] and hand it over to the browser (that is, write the execution result to the [Response Body] in binary form)

  • Set the [content-type] attribute value in the response header, so as to control the browser to use the corresponding compiler to compile the binary data of the response body into [text, picture, video, command]

  • Set the [location] attribute in the response header, and assign a request address to the location, thereby controlling the browser to send a request to the specified server


<a name="The output of the response object is not garbled Chinese"></a>

Response object returns String to Browser & Chinese garbled problem

grammar

  • If the output content contains Chinese, you must use setContentType for the resp object to specify the content category and encoding rules. And note: it must be set before obtaining the PrintWriter object!

  • Use the PrintWriter object out for output

  • The write of out is output according to bytes, which is generally used for String. For the int type, you need to use out's print method for character output. It is recommended to use print

resp.setContentType("text/html;charset=utf-8");  //设置content-Type中的字符规则为UTF-8,以保证显示到网页为中文
PrintWriter out = resp.getWriter();   //获取输出流
out.write("从前台收到的参数值为" +   strValue);   //write方法,是字节输出流的方法(输出int的时候会按照ASCII码输出)

example

    <form action="/web3/test1" method="post">
      <input type="text" name="p1">
      <input type="submit" value="测试响应对象返回String">
    </form>

 

public class TestResponse extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        //接收测试参数
        req.setCharacterEncoding("utf-8");  //对POST请求体进行UTF-8编码
        String strValue = (String)req.getParameter("p1");  //获取参数值

        //测试返回String
        resp.setContentType("text/html;charset=utf-8");  //设置content-Type中的字符规则,以保证显示到网页为中文
        PrintWriter out = resp.getWriter();
        out.write("从前台收到的参数值为" +   strValue);
    }
}

 

<?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>testWeb1</servlet-name>
        <servlet-class>TestResponse</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>testWeb1</servlet-name>
        <url-pattern>/test1</url-pattern>
    </servlet-mapping>

</web-app>

 

 


Response object returns int to Browser

grammar

  • The write of out is output according to bytes, which is generally used for String. For the int type, you need to use the print method of out's character output

  • In the actual development process, the real data is written into the response body through out.print()

example

The web.xml, index.jsp and other configurations are exactly the same as the previous section.

public class TestResponse extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        resp.setContentType("text/html;charset=utf-8");  //设置content-Type中的字符规则,以保证显示到网页为中文
        PrintWriter out = resp.getWriter();

        //测试返回 int
        out.write("测试返回int");
        out.write(50);  //按字节输出,得到的是50的ASCII码对应的字符,输出的是2
        out.print(50);  //按字符输出,得到的是原本的50
    }
}

The response object is returned to the Browser after parsing HTML tags

grammar

  • If the output sentence contains HTML tags, if it is output directly, because the value of the content-type attribute is "text" by default, content-type="text"

    At this time, [text compiler] is used to parse the binary data of the response body, which cannot be parsed into HTML commands, resulting in the output of the HTML tags intact.

  • If you want the browser to parse the HTML tags and then display them, you can useresponse.setContentType("text/html");

    Specify the browser to use the [compiler] to compile the [binary content in the response body]

  • This syntax is also used in Response Object Output No Garbled Chinese

response.setContentType("text/html");

example

public class TestResponse extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        //测试返回String
        resp.setContentType("text/html;charset=utf-8");  //设置content-Type中的字符规则,以保证显示到网页为中文、按HTML标签和text文本编译
        
        //测试输出编译好的HTML标签
        out.print("第一行");
        out.print("</br>");
        out.print("第二行");
    }
}

Guess you like

Origin blog.csdn.net/m0_57265007/article/details/128005880