Servlet review study notes --request objects

Original link: http://www.cnblogs.com/fingerboy/p/5178839.html

  request and response are a partner, responsible for responding to a request for a responsible, parametric methods are Servlet.service (), the previous knowledge of carded a response, just say here about the request, each request is issued at the client, server creates a request object, the package requested data to the request, and then calling Servlet.service () is passed in. we normally use doGet or doPost method when you create a servlet, and did not see the service () method It is the parent class because the service has been read request to the request in the way, and then decided to call the doGet method doPost method you override or at the request of the way.

  • Some common methods:
    • getRemoteAddr (): get remote IP address
    • getMethod (): Get request method, GET or POST
    • getHeader (String name): the first acquisition request, e.g. getHeader ( "User-Agent"), can be identified by User_agent user operating system and browser information

Here is a url: HTTP: // localhost:? 8080 / MyPro / aServlet name = "zhangsan"

    • String getScheme (): Gets protocol, HTTP
    • String getServerName (): Gets the server name, localhost
    • String getServerPort (): Gets the server port, 8080
    • String getContextPath (): Gets the project name, MyPro
    • String getServletPath (): Gets Servlet path, / aServlet
    •  String getQueryString (): parameter acquisition section portion, i.e., after the question mark. name = "zhangSan"
    • String getRequestURI (): Gets the request URI, equal to the project name + Servlet path. / myPro / AServlet
    • String getRequestURL (): Get request URL, equal to the entire request does not contain a path parameter. http: // localhost: 8080 / myPro / AServlet
  • Get request parameters
    • String getParameter (String name): value of the specified parameter name acquisition request, the request for a single value
    •  String [] getParameterValues ​​(String name): Get request parameter values ​​specified name, a request for multi-value
    •  Enumeration <String> getParameterNames (): Get all request parameter names, parameter values ​​can be acquired by traversing each name corresponding to the name
  • And a request forwarding request contains (in a chain comprising a plurality of the servlet request)
    1. Forwards the request:. Request.getRequestDispatcher ( "/ BServlet") forward (request, response); ---> The most commonly used
    2. Request comprising: request.getRequestDispatcher ( "/ BServlet") include (request, response); ---> is not used.

    So both in the end what difference does it make? Suppose a request before pointing Aservlet, and Aservlet output a line of words "AServlet" to the browser, and then forwards the request to the BServlet, BServlet also output a line of words "BServlet" to the browser, then the user can only see in a browser "BServlet", and if it is included in the request Aservlet BServlet, the user can see the "AservletBservlet" in the browser, which means that the body does not respond to the request forwarded contains the current servlet, and the request contains the response body will contain the current page , special attention is, either forward the request or the request contained in the request within a range, use the same request and response, and redirect the request to use two and two response this is a big difference to sum up here and forwards the request to redirect the difference between:

  1.  Forwards the request is a response to a request, two requests are redirected twice response
  2.  Request forwarding address bar does not change, but the redirect will display a requested address
  3.  This request is forwarded only be forwarded to other Servlet project, and not only redirect to other redirect Servlet this project, but also directed to other projects
  4.  Forwards the request is a server-side behavior, just give Servlet forwarding path, redirected need to give requestURI, namely that contains the project name!
  5.  Forwarding and redirection request is forwarded high efficiency! Because it is a request!
  6.  Need to change the address bar, you must use redirection!
  7.  Need to get data request in the next field of a Servlet, you must use forward!
  • request field (used to pass values)
    1.    void setAttribute(String name, Object value):
    2.    Object getAttribute(String name)

 

Reproduced in: https: //www.cnblogs.com/fingerboy/p/5178839.html

Guess you like

Origin blog.csdn.net/weixin_30687587/article/details/94790478