0107-2020-HTTP&Servlet&Tomcat

Learning Source: xml learning materials dark horse Java57 part of JavaWeb course.

Tomcat: Open Source software running dynamic resources.
Servlet: is an interface that defines the rules for Java to be accessed by a browser (Tomcat can be identified) of.
Here Insert Picture Description

* 快速入门:
1. 创建JavaEE项目
2. 定义一个类,实现Servlet接口
	* public class ServletDemo1 implements Servlet
3. 实现接口中的抽象方法
4. 配置Servlet
	 在web.xml中配置:
    <!--配置Servlet -->
    <servlet>
        <servlet-name>demo1</servlet-name>
        <servlet-class>cn.itcast.web.servlet.ServletDemo1</servlet-class>
    </servlet>

    <servlet-mapping>
        <servlet-name>demo1</servlet-name>
        <url-pattern>/demo1</url-pattern>
    </servlet-mapping>
  • The implementation of the principle:
    1. Resource Path When the server receives the request of the client browser will parse the request URL path to get access to the Servlet
    2. Find web.xml file, whether or not there is a corresponding tag body content.
    3. If so, find the full class name corresponding
    4. tomcat will bytecode file is loaded into memory, and creates its objects
    5. Calling its methods

With more implementation class HttpServlet is
0. request mode determination

  1. Obtain data
    HTTP:
  • The concept: Hyper Text Transfer Protocol Hypertext Transfer Protocol

    • Transfer Protocol: define, when the client and server communications, data transmission format

    • Features:

      1. TCP / IP based high-level agreement
      2. The default port number: 80
      3. Based on a request / response model: Response time corresponding to the first request
      4. Stateless: requests each independent of each other, the data can not interact
    • historic version:

      • 1.0: in response to each request to establish a new connection will
      • 1.1: Multiple Access
  • A data request message format

    1. The request line
      request method request url request protocol / version
      GET /login.html HTTP / 1.1

      • Request mode:
        • 7 HTTP protocol has requested embodiment, there are two commonly used
          • GET:
            1. Request parameters in the request line, after the url.
            2. url requests limited length
            3. Less secure
          • POST:
            1. Request parameters in the request body
            2. url is not limited in the length of the request
            3. Relatively safe
    2. Request header: the client browser to tell some information about the server
      request header name: request header value

      • Common request headers:
        1. User-Agent: browser tells the server, I access the browser version you are using information

          • You can get information on the server side of the head, resolve browser compatibility issues
        2. Referer:http://localhost/login.html

          • Tells the server, I (the current request) come from?
            • effect:
              1. Security chain:
              2. quantity survey job:
    3. Request blank lines
      blank lines, that is, for dividing the request header POST request, and the request body.

    4. Request body (body):

      • POST request message encapsulating a parameter request
    • 字符串格式:
      请求行:POST /login.html HTTP/1.1
      请求头:Host: localhost
      User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:60.0) Gecko/20100101 Firefox/60.0
      Accept: text/html,application/xhtml+xml,application/xml;q=0.9,/;q=0.8
      Accept-Language: zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2
      Accept-Encoding: gzip, deflate
      Referer: http://localhost/login.html
      Connection: keep-alive
      Upgrade-Insecure-Requests: 1

Request body: username = zhangsan (POST only)
the Request

  1. Principle of the request and response objects

    1. request and response objects are created by the server. We use them
    2. request object is to get request message, response message in response to the object is set to
  2. request object inherits architecture:
    ServletRequest - Interface
    | inherit
    HttpServletRequest - Interface
    | achieve
    org.apache.catalina.connector.RequestFacade class (tomcat)

  3. request features:

    1. A data acquisition request message
      1. Line data acquisition request

        • GET /day14/demo1?name=zhangsan HTTP/1.1
        • method:
          1. Acquisition request method: GET

            • String getMethod()
          2. (*) Gets the virtual directory: / day14

            • String getContextPath()
          3. Get Servlet Path: / demo1

            • String getServletPath()
          4. Get request parameter acquisition mode: name = zhangsan

            • String getQueryString()
          5. (*) Acquisition request URI: / day14 / demo1

            • String getRequestURI(): /day14/demo1

            • StringBuffer getRequestURL() :http://localhost/day14/demo1

            • URL: Uniform Resource Locator: http: // localhost / day14 / demo1 People's Republic of China

            • URI: Uniform Resource Identifier: / day14 / demo1 Republic

          6. Acquisition protocol and version: HTTP / 1.1

            • String getProtocol()
          7. Obtain the IP address of the client:

            • String getRemoteAddr()
      2. Data acquisition request header

        • method:
          • (*) String getHeader (String name): Gets the value of the request header request header by name
          • Enumeration getHeaderNames (): Get all request header name
      3. 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
        • step:
          1. Get a stream object

            • BufferedReader getReader (): Get a character input stream, only operation of the character data
            • ServletInputStream getInputStream (): Get the input byte stream, you can operate all types of data
              • After the file is uploaded to explain the knowledge points
          2. Then take data from the stream object

Published 98 original articles · won praise 0 · Views 2206

Guess you like

Origin blog.csdn.net/weixin_43221993/article/details/103881436