Java Web+ data processing interview questions

One, please elaborate on the structure of the HTTP request:

  1. HTTP request consists of three parts: request line, request header, request body
    request line -usually in the first line of HTTP, it indicates the sending method (get/post), the sending address, and the HTTP version number.
    The request header -describes the auxiliary information sent from the browser to the server.
    Accept-Language: zh-CN indicates that the browser preferentially uses Chinese
    User-Agent: represents the user's use environment (judge whether the user is using a mobile phone or accessing a computer, and then continuously display differently according to the browser's specifications)
    Content-Type: Explains the format
    request body of the submitted form-the real data sent by the browser to the server. In the request question, the data uses the form of key-value pairs. The "key" and "value" are connected by "=". Use "&" to separate multiple key-value pairs. (The request body is only present in the post request, and there is no request body in the get request) The request body will be appended to the url and sent to the server.
    Insert picture description here
  2. Response-The result returned by the server to the browser. The
    HTTP response contains three parts: the response line, the response header, and the response body.
    Response line -usually in the first line of the response, it contains the http version, status code, and the English description of the status code.
    200-indicates that the access is successful
    404-indicates that the resource is not found
    500-represents an internal error of the server.
    Response header – expresses some auxiliary information of the returned data, which web server is used,
    Service – means which web server is used,
    Content-Type: means what method the browser uses to process the data after it is returned to the browser It. (Text/html-means to interpret the returned data as html for display)
    Date-the time
    response body generated by the response data- the real data returned by the server to the browser, (html fragments, binary content, xml)

Second, please explain the Servlet execution process?

  • The client sends an http request to the Tomcat server, including the servlet mapping address and the parameters to be passed-"Tomcat parses each web.xml file, finds the matching URL and the corresponding servlet name-"Find the corresponding servlet according to the servlet name, And to instantiate and initialize this servlet-"Tomcat executes the service() method in the servlet instance-"After the method runs, the program execution result is returned to the browser through the response-"The browser receives this code and explains it.
    Insert picture description here

3. Timing of Servlet instantiation:
By default , servlet is instantiated when it is accessed for the first time. You can also configure loadon-startup through web.xml to make it instantiate when the fifteenth phase starts.

Fourth, how does servlet deal with in a concurrent environment?
Servlet is based on the concurrency of singleton multi-threaded processing. Use multi-threading technology to provide web services.

5. In the case of multi-threaded processing, how to solve the thread safety problem?
All threads share a servlet instance. So when we use servlet, it is not allowed to create state variables and objects in the servlet. Because this will produce unexpected results during concurrent access.

Sixth, what is the Servlet life cycle?

  1. Loading-When the java application is started, tomcat will scan the web.xml file to know which servlets are currently available. (Servlet will not be instantiated when loading) (Object creation at the java level during creation)
  2. Create-Created when the url accesses the servlet address for the first time. At the same time the constructor is executed.
  3. Initialization-After the servlet creates the object, it immediately executes the init() initialization function to initialize the servlet. (Initialization is a method used exclusively by the servlet itself to initialize the execution resources of the servlet)
  4. Provide service-service() method.
    servlce() method-For all incoming requests (no matter it is post/get), the servlet method is used for receiving and processing.
    If the request is refined, the service() method can also be refined into the doGet()/doPost() method. doGet()-only handles get requests,
    doPost()-only handles post requests.
  5. Destroy-Use the destroy() method to completely destroy the servlet's resources when the web application is restarted or closed.

Seven, the difference between request forwarding and response redirection :

There are two ways of resource jump in javaweb:

  1. Request forwarding -is a server jump, only one request will be generated. The request will be forwarded to the next request intact. (Server Jump)
  • 语法:request.getRequestDispatcher().forward();
  • Execution process: The browser sends a request to tomcat-"After tomcat receives the request, it uses the corresponding servlet to process the request -" and executes request.getRequestDispatcher().forward();-"leave the request intact after processing. Distributed to another servlet-"After the servlet processes the request, it generates a response object-"Send the corresponding response result to the browser through tomcat-"The browser sees the final response result (there is only one request globally, and the resource The jump is generated inside the server, and the browser cannot feel it)
  • Address bar: Request forwarding will only display the only requested url
    Insert picture description here
  1. Responding to redirection: it is a redirection on the browser side, which will generate 2 requests
  • Syntax: response.sendRedirect()
  • Execution process: The browser initiates a request to tomcat-"After delivery, servlet1 calls the response.sendRedirect() method, returns a response and sends it to the browser via tomcat, telling the browser to create a new request-"The browser receives the information and sends it The second request is to servlet2-"servlet2 processes request 2 and returns the result to the browser-"Browser displays the result
  • Address bar: Only the url
    Insert picture description here
    eight of the second response will be displayed . The working principle of session is session-
    also known as user session, which is bound to the client browser window and stored in the server's internal user data.
    The working principle of the session is that after the client login is completed, the server will create the corresponding session. After the session is created, the session id will be sent to the client, and the client will store it in the browser's cookie. As long as the browser is not closed, this cookie will always exist. In this way, each time the client accesses the server, it will carry the sessionid. After the server gets the sessionid, it finds the corresponding session in the memory so that it can work normally.
    Insert picture description here
    Eight, nine built-in JSP objects:
    Insert picture description here

data processing

1. The difference between using Statement and PreparedStatement in JDBC

  1. PreparedStatement is a pre-compiled SQL statement, which is more efficient than Statement. Especially when complex SQL statements are executed or a certain SQL is executed frequently, the execution efficiency advantage is more obvious.

  2. PreparedStatement supports parameterized operations, which is more flexible than Statement using string connection to block SQL statements. Better readability

  3. PreparedStatement can prevent SQL injection and is safer than Statement

2. Please explain the steps to use JDBC:

  1. Load the JDBC driver

  2. Created in the database link (Connection)

  3. Create command (PreparedStatement/Statement)

  4. For the processing results of the query, the ResultSet object is used to receive, and the results are processed through traversal.

  5. Close database connection

Guess you like

Origin blog.csdn.net/qq_36792120/article/details/114198225