Servlet Learning Notes (3)

HTTP protocol

1. Request: The client sends data from the server

1.1 HTTP Request Packets:

  • The first message
    • The request line
    • Request header field
    • Generic header field
    • Entity head field
  • Blank line (\r\n)
  • Message body (transmitted data)

2. Response: Data sent by the server to the client

2.1 HTTP Response Packets

  • The first message
    • Response line
    • Response header field
    • Generic header field
    • Entity head field
  • Blank line (\r\n)
  • Message body (transmitted data)

3. Response status code

  • 1XX: The server receives the message from the client, but does not complete it, and then responds with the 1XX status code after waiting for some time
  • 2xx: success, usually 200 (for successful request)
  • 3xx: redirect, commonly used 302 (redirect), 304 (access cache)
  • 4xx: Client error
    • 404: Request path has no resource
    • 405: Request mode does not correspond to doXxx method
  • 5xx: server side error, usually 500 (internal server problem)

2. Response object

1.Response Sets the Response message

1.1 Setting the Status Code: setStatus(int SC)

1.2 Setting response header: setHeader(String name, String Value)

  • If you want to redirect the flower, you can first set the response code 302, and then set the redirection path Location; Or use sendRedirect(String Name, String Value) for redirection

1.3 Setting the Response Body:

  • Get output stream
    • PrintWriter getWriter()
    • Byte output stream: ServletOutputStream getOutputStream()

2. Notes for writing paths:

2.1 Relative path and Absolute Path

  • Relative path: “.” for current directory, “..” Represents the upper level directory, “.. /..” Represents the directory at the next level up, and so on ** (is. Or.. First) **
  • An absolute path: such as: “www.baidu.com/directory/i…” The URI in the URL is “/directory/index.html”. The path bit is the absolute path ** (starts with /) **

2.2 == When to use relative path and absolute path? = =

  • If the path is sent to the client, use the absolute path (that is, to obtain the virtual directory, throughrequest.getContextPath()In order to get)
    • Absolute paths are required for a tags, form tags, redirects, and so on
  • If the path is for server use, use the relative path directly (do not need to obtain the virtual directory, use the resource path directly).
    • The relative path is used when the request is forwarded

3. Does the response object output garbled characters to the browser?

By default, the Tomcat server codes the response bit ISO-8859-1, while some codes of the browser are UTF-8 and some are GBK, so garbled codes appear. So if the direct Chinese output to certainly not. Solutions are as follows:

  • By setting the response. SetCharacterEncoding (” utf-8 “) : This can solve the problem of Chinese garbled characters in the request, but this method is equivalent to writing dead, because different browsers parse the request differently, Chrome according to UTF-8 parsing, then normal display, but Internet Explorer according to GBK parsing problems, so this is not recommended

  • Start by defining the encoding format for the browser to parse the request: Response.setContentType (“text/ HTML; charset=utf-8”); In the future, no matter which browser is used, the utF-8 encoding will be parsed. SetContentType (“text/ HTML; “); Response.setContentType (“text/ HTML; charset=utf-8”); Can solve the page output Chinese garble problem.

    // Use utF-8 to parse GBK (GBK, GBK, GBK)
    resp.setCharacterEncoding("UTF-8");
    // This setting makes Tomcat parse the response response using UTF-8 and tells the browser to decode it using UTF-8
    resp.setContentType("text/html; charset=utf-8");
    PrintWriter pw = resp.getWriter();
    pw.write("Hello!);
    Copy the code

The ServletContext object

What is a ServletContext object?

The ServletContext object represents the entire Web application and can communicate with the application’s container (that is, the server)

2. Obtain the ServletContext object

  1. Obtain from the Request object:request.getServletContext()
  2. Get from the HttpServlet object:this.getServletContext()

3. What functions/methods does the ServletContext object have?

  • Get the MIME type (a file data type defined during Internet communication)

    • getMimeType(String file)
    • Format: large type/small type text/ HTML image/ JPG
  • Set domain objects to share data

    • setAttribute(String name, Object value)
    • getAttribute(String name)
    • removeAttribute(String name)
    • The scope of the ServletContext object: all requested data for all users
  • Gets the real path of the file (which is the real path on the server)

    • GetRealPath (String path) : such as: String realPath = servletContext. GetRealPath (“/which xt “); This results in H:\Java\JavaWeb\out\artifacts\JavaWeb_war_exploded\ d.exploded because the path obtained is the root directory of your current Web application, plus the path to your files:

      •   String b = context.getRealPath("/b.txt");// Access resources in the web directory
          System.out.println(b);
        Copy the code
      •   String c = context.getRealPath("/WEB-INF/c.txt");// Access resources in the web-INF directory
          System.out.println(c);
        Copy the code
      •   String a = context.getRealPath("/WEB-INF/classes/a.txt");// access resources in the SRC directory
          System.out.println(a);
        Copy the code