Response of Java objects

Response object

A, Response and inheritance structure principle

  Principles and inheritance structure reference request.

Two, Response Object

   Response is used to set the response message.

  1, the line setting response

      Response header format:

HTTP/1.1 200 ok

     Set status code:

setStatus (int sc); // sc is the status code

  

  2, in response to the first set

    Setting response headers:

setHeader (String name, String value); // to set the response header information to designated by name 

  

  3, setting response member

     Steps for usage:

      (1) obtain an output stream

Character output streams: PrintWriter getWriter () 
output stream of bytes: ServletOutputStream getOutputStream ()

      (2) using the output stream, the output data to the client browser.

  4, complete redirection

     (1) Redirect : Resource jump way;

     (2) code implementation:

// Set 1 status code 302 
response.setStatus (302); 
// set the response header 2 LOCATION 
response.setHeader ( "LOCATION", "/ Web / demo2"); 

// simple redirection method (conventional) 
response.sendRedirect ( "/ web / demo2" );

      (3) redirect features

        Redirect (redirect) features: [important]

        ① send the address bar changes

        ② redirection can access other site (server) resources

        ③ twice redirection request, the request object can not be used to share data

         Request Forwarding (forward) features: [important]

        ① the same path forwarding address bar

        ② forwards only have access to resources at the current server

        ③ is a forwarding request, the request object can be used to share data

      (4) path problem

          Relative path: not determined uniquely by the path of the resource

               Such as: ./ index.html;

               Features: do not start with /, beginning with

               Rules: Find the relative positional relationship between the source and target resources currently

                  ./: Indicates the current directory

                 ../: Indicates Back go back one level

          Absolute path: it can determine the unique resource by absolute path

               如:http://localhost/web/demo1

               Features: / begin with

               Rule: determining a path defined by whom, it is determined where the request issued in the future

                 ① to the client browser to use: need to add (access path project) virtual directory

                  Such as: page hyperlinks, form forms, and redirection.

                  Note: not recommended for the virtual directory to write the dead, usually obtained dynamically. Acquisition method:

request.getContextPath()

                 ② server to use: no need to add virtual directory

                  Such as: forwarding path; 

  5, server output character data to the browser

    Step :

     (1) Get the character output stream

     (2) the output data

    Code:

1 //1.获取字符输出流
2 PrintWriter pw = response.getWriter();    获取的流的默认编码是ISO-8859-1
3 //2.输出数据
4 //pw.write("<h1>hello response</h1>");
5 pw.write("你好啊啊啊 response");

 

    上面的这种方式,向页面输出英文没有问题,而且HTML标签也会被正常解析,但是如果输出中文,就会产生乱码。

    乱码问题

    

     解决乱码:

 1 //获取流对象之前,设置流的默认编码:ISO-8859-1 设置为:utf-8
 2 // response.setCharacterEncoding("utf-8");
 3 //告诉浏览器,服务器发送的消息体数据的编码。建议浏览器使用该编码解码
 4 //response.setHeader("content-type","text/html;charset=utf-8");
 5 
 6 //简单的形式,设置编码
 7 response.setContentType("text/html;charset=utf-8");
 8 
 9 //1.获取字符输出流
10 PrintWriter pw = response.getWriter();
11 //2.输出数据
12 //pw.write("<h1>hello response</h1>");
13 pw.write("你好啊啊啊 response");

 

    注意:一定要在获取流之前设置编码。

  6、服务器输出字节数据到浏览器(一般用于非文字的传输)

    步骤

     (1)获取字节输出流

     (2)输出数据

    代码实现:

1  // 设置消息体数据及编码
2  response.setContentType("text/html;charset=utf-8");
3 
4  //1.获取字节输出流
5  ServletOutputStream sos = response.getOutputStream();
6  //2.输出数据
7  sos.write("你好".getBytes("utf-8"));

 

Guess you like

Origin www.cnblogs.com/niujifei/p/11620148.html