• 周四. 10 月 3rd, 2024

5G编程聚合网

5G时代下一个聚合的编程学习网

热门标签

Java Web Learning (6) — the difference between request object / response object / redirection and f

King Wang

1 月 3, 2022

JavaWeb Study ( 6、 … and )—Request object /Response object / The difference between redirection and forwarding requests

Web The server received… From the client http request , For every request , Create one to represent the request request object 、 And on behalf of the response response object . therefore , If we want to get the client’s request data , Just go through request Object can be implemented , If you want to send back some data back to the client , Use response Object can .

01 Request object

When the client passes HTTP When the protocol accesses the server ,HTTP All the information in the request header is encapsulated in this object , adopt HttpServletRequest Methods provided , You can get all the information requested by the client .

  • Request Object is a common way to get client information
getRequestURL();// Returns the integrity of the request from the client URL.
getRequestURI();// Return the resource name part of the request line .
getQueryString();// Return the parameter part in the request line .
getPathInfo();// Return request URL Additional path information in . The extra path information is the request URL Located in Servlet After the path and before the query parameters , It uses “/” start .
getRemoteAddr();// Returns the client that made the request IP Address .
getRemoteHost();// Returns the full host name of the client that made the request .
getRemotePort();// Returns the network port number used by the client .
getLocalAddr();// return WEB Server's IP Address .
getLocalName();// return WEB The hostname of the server
  • Request Object implements request forwarding

    Request forwarding : Means a web After the resource receives a client request , Tell the server to call another web Resources to deal with .

    stay Servlet There are two ways to realize request forwarding in :

    • adopt ServletContext Of getRequestDispatcher(String path) Method , This method returns a RequestDispatcher object , Calling this object forward Method can implement request forwarding .
    RequestDispatcher reqDispatcher =this.getServletContext().getRequestDispatcher("{ Page url}");
    reqDispatcher.forward(request, response);
    
    • adopt request Object supplied getRequestDispatche(String path) Method , This method returns a RequestDispatcher object , Calling this object forward Method can implement request forwarding .
    request.getRequestDispatcher("/test.jsp").forward(request, response);
    

02 Response object

HttpServletResponse Object represents the response of the server . This object encapsulates sending data to the client 、 Send response header , Method of sending response status code .

  • Client side ( browser ) How to send data
getOutputStream(); // Get the output stream
getWriter(); // Get the print stream

Use PrintWriter Stream output Chinese note : Remember to set the character encoding

response.setCharacterEncoding("UTF-8");// Set character to "UTF-8" Code output to client browser
//PrintWriter out = response.getWriter(); Must be on response.setCharacterEncoding("UTF-8"); after

Details :

getOutputStream and getWriter The two methods are mutually exclusive , After calling any of these methods , You can’t call another method .

Serlvet Of service After the method is over ,Servlet The engine will check getWriter or getOutputStream Whether the output stream object returned by the method has been called close Method , without ,Servlet The engine will call close Method to close the output stream object .


  • Response Object implements request redirection

    Request redirection refers to : One web After the resource receives a client request , Notify the client to access another web resources , This is called request redirection .

    ** Realization way :** call response Object’s sendRedirect Method to implement request redirection

response.sendRedirect(" page url");//url There is no need to add /

03 Interview questions : The difference between redirection and forwarding requests

request redirections It’s client behavior ,web After the resource receives a client request , Inform the browser to visit Another one web resources ( Let the client request another url). You can’t carry parameters . The address bar will change .

Forwarding requests is server behavior ,web The resource receives the client request , Inform the browser to call Another one web resources ( The server forwards itself to another url). You can carry parameters ( adopt forward Methods will req,resp Forward to another url). The address bar doesn’t change .


发表回复