JAVA request&response

request&response


 

Principle of the request and response objects

  request and response objects are created by the server

  request object is to get request message, response message in response to the object is set to


request object inherits architecture

ServletRequest - Interface
  | inherit
HttpServletRequest - Interface
  | achieve
org.apache.catalina.connector.RequestFacade class (tomcat)


request function

  1, a data acquisition request message

    Line data acquisition request GET / wdnmd / demo1? Name = wdnmd HTTP / 1.1

1 . Acquisition request mode: the GET
      String getMethod ()  
 2. Gets the virtual directory : / wdnmd
      String getContextPath ()
 3. Get Servlet path: / the demo1
      String getServletPath ()
 4. get acquisition mode request parameters: name = wdnmd
      String the getQueryString ()
 The acquisition request URI of the : / wdnmd / demo1
      String getRequestURI (): / wdnmd / demo1
      StringBuffer getRequestURL (): HTTP: // localhost / wdnmd / demo1
 
the URL of: uniform resource locator: HTTP: // localhost / wdnmd / demo1 Apple 
URI: uniform resource identifier: / Day14 / demo1 fruit
                
 6. get the protocol and version: HTTP / 1.1
      GetProtocol String ()

 7 . To obtain the IP address of the client:
       String getRemoteAddr ()

    Data acquisition request header

      String getHeader (String name): Gets the value of the first request by name request header
      Enumeration <String> getHeaderNames (): Get all request header name

    Volume data acquisition request

      Request POST request parameter only mode, only the request body, encapsulating the POST request in the request body: the request body

         BufferedReader getReader (): Get a character input stream, only the character data operation
         ServletInputStream getInputStream (): Get the input byte stream, can operate all types of data

 

1. Get request parameters common mode: Whether get or post requests embodiment can be acquired using the following method request parameters
    String getParameter (String name): Get The parameter name parameter value username = ZS & password = 123
    String [] the getParameterValues (String name): the acquired parameter name parameter value array & Hobby = XX = Hobby Game
    the Enumeration <String> the getParameterNames (): Get the name of the parameter for all requests
    map <String, String []> getParameterMap (): Get all map set of parameters

Chinese garbage problem:
    get way: tomcat 8 has been garbled way to get the problem solved
    post by: garbled
    Solution: Before acquisition parameters, set the encoding request of request.setCharacterEncoding ( "utf-8") ;


request request forwarding

  Jump in the way of a resource internal server

1. By acquiring request object forwards the request object: RequestDispatcher the getRequestDispatcher (String path)
2. Use RequestDispatcher object to forward: forward (ServletRequest request, ServletResponse response )

Features:

    1. browser address bar does not change the path
    2. only be forwarded to internal resources in the current server
    3. The first request is forwarded

 


BeanUtils Tools

  Simplified data package, for packaging a JavaBean

    1. Requirements

      JavaBean: standard Java classes

      Public class must be modified

      You must provide an empty argument constructor

      You must use private member variables modified

      Public setter and getter methods

    Function 2: Data encapsulation

    3. Method:
        the setProperty ()
        getProperty ()
        the populate (Object obj, the Map map): a set of keys for the map information corresponding to the packaged objects JavaBean

  


 

 Response Redirect

    Redirect: Resource jump way  

      Simple redirection method
      response.sendRedirect ( "/ responseServlet");

 

    Redirection features: redirect
      address bar changes to
      redirect access to other sites (servers) resource
      redirection are two requests. You can not use the request object to share data
    forwarding features: forward
      forwarding address bar the same path
      forward can only access the server resources under the current
      forwarding is a request, the request object can be used to share data


Response output stream

   Server output character data to the browser

    Steps of:
      obtaining an output stream of characters
      output data

    Distortion problem:
      the PrintWriter response.getWriter PW = (); default encoded stream acquired ISO-8859-1 is
      disposed default encoding of the stream
      tells the browser to use the response code of the body

      response.setContentType ( "text / html; charset
= utf-8");     server output byte data to the browser
      the steps of:
      obtaining an output stream of bytes
      of output data

@WebServlet ( "/ responseServlet" )
 public  class ResponseDemo4 the extends the HttpServlet {
     protected  void the doPost (the HttpServletRequest Request, the HttpServletResponse Response) throws ServletException, IOException { 

        // Before obtaining the stream object, stream setting default encoding: ISO-8859-1 to: GBK
        // ; response.setCharacterEncoding ( "UTF-. 8") 

        // encoder tells the browser, the message body data sent by the server. Recommended browser uses the codec
         // simple form, provided encoding 
        the response.setContentType ( "text / HTML; charset = UTF-. 8" ); 

        // 1. Get the character output stream 
        the PrintWriter PW = response.getWriter ();
         / / 2 output data
        pw.write("你好 world");
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request,response);
    }
}

path

Path Category
       1 relative path: can not determine the unique resource through a relative path
            , such as: ./ index.html
            not to / the beginning to the beginning of the path. 

           Rules: Find the relative position relationship between the current source and target resources
            ./ : the current directory
            ../ : Back-level directory 
absolute path: the only resource can be determined by absolute path , such as: HTTP: // localhost / day15 / responseDemo2 / day15 / responseDemo2 to / path beginning with the rules: the path is defined to determine who is using? Request sent from the future to determine where the client browser to use: need to add virtual directory (access path of the project) is recommended to obtain dynamic virtual directory: request.getContextPath () <a>, <form> redirect ... to server use: You do not need to add virtual directory forwarding path

 

Guess you like

Origin www.cnblogs.com/viperqy/p/11546202.html