Java Road to God: Java Interview Preparation (12)

Two, JavaWeb

1. Briefly describe the similarities and differences between Servlet and JSP

difference:

JSP writes Java code in HTML code, while Servlet writes HTML code in Java code, which itself is a Java class

Servlet needs to be configured in web.xml, while JSP does not need to be configured

JSP is mainly used in the view layer, responsible for display, and Servlet is mainly used in the control layer, responsible for scheduling

Same point:

The essence of jsp is a Servlet, JVM can only recognize Java classes

2. Which of the following functions is correct to execute when the DOM is loaded

3. Can a char variable store a Chinese character

1. Java uses unicode encoding, 2 bytes to represent a character, both Chinese characters and numbers can be stored

2. Char is 2 bytes in java, so it can store Chinese

4. What is UML? What are the diagrams in UML

UML is the abbreviation of Unified Modeling Language, a graphical language that supports modeling and software system development, and provides model and visualization support for all stages of software development.

UML can help communication, assist application design and document generation, and explain the structure and behavior of the system

Diagrams in UML include: use case diagrams, class diagrams, sequence diagrams, collaboration diagrams, state diagrams, activity diagrams, component diagrams, deployment diagrams, etc.

5. The difference between session and cookie

1. From a storage perspective, session is a server-side storage technology, and cookie is a client-side data storage technology

2. From the perspective of problem solving, session solves the problem of data sharing for different requests of users, and cookie solves the sharing of request data for different requests.

3. From the perspective of the life cycle, the session id depends on the cookie for storage, and the id will become invalid when the browser is closed; and the cookie can be set separately for the time that the browser is stored

6, TCP three-way handshake

7. Which of the following descriptions of the meaning of the corresponding HTTP status codes are correct?

8. JSP nine built-in objects

1. The request object

The request object is an object of type javax.servlet.httpServletRequest. This object represents the client's request information, and is mainly used to accept data transmitted to the server through the HTTP protocol. (Including header information, system information, request method and request parameters, etc.). The scope of the request object is a request.

2. Response object

Response represents the response to the client, which is mainly to send the objects processed by the JSP container back to the client. The response object also has a scope, it is only valid within the JSP page.

3. Session object

The session object is an object that is automatically created by the server and related to the user's request. The server generates a session object for each user, which is used to save the user's information and track the user's operating status. The session object uses the Map class to save data, so the format for saving data is "Key/value". The value of the session object can be a complex object type, not just a string type.

4. Application object

The application object can store information in the server until the server is closed, otherwise the information stored in the application object will be valid in the entire application. Compared with the session object, the application object has a longer life cycle, similar to the "global variables" of the system

5. Out object

The out object is used to output information in the Web browser and manage the output buffer on the application server. When using the out object to output data, you can operate on the data buffer, clear the residual data in the buffer in time, and make room for other output. After the data output is completed, the output stream must be closed in time.

6, pageContext object

The function of the pageContext object is to obtain any range of parameters, through which out, request, response, session, application and other objects of the JSP page can be obtained. The creation and initialization of the pageContext object are all done by the container, and the pageContext object can be used directly in the JSP page.

7, config object

The main function of the config object is to obtain the configuration information of the server. A config object can be obtained through the getServletConfig() method of the pageConext object. When a servlet is initialized, the container passes certain information to the servlet through the config object. Developers can provide initialization parameters for Servlet programs and JSP pages in the application environment in the web.xml file.

8, page object

The page object represents the JSP itself and is only valid within the JSP page. The page implicit object essentially contains the variables referenced by the current Servlet interface, similar to the this pointer in Java programming.

9, exception object

The function of the exception object is to display exception information. It can only be used in a page that contains isErrorPage="true". Using this object in a general JSP page will not compile JSP files. The excepation object, like all objects in Java, has an inheritance structure provided by the system. The exception object defines almost all exceptions. In a Java program, you can use try/catch keywords to handle exceptions; if there are exceptions that are not caught in the JSP page, an exception object will be generated, and the exception object will be sent to the error page set in the page instruction , And then handle the corresponding exception object in the error page.

9, the advantages and disadvantages of ajax

Advantages: reduce the burden on the server, fetch data on demand, reduce redundant requests to the greatest extent, and refresh the page partially. Reduce user waiting time and improve user experience

Disadvantages: Ajax uses a lot of javascript and ajax engines, which depend on the browser's support

10. The difference between forward() and redirect() in Servlet API

In order to realize the modularization of the program, it is necessary to ensure that different Servlets can jump to each other, and there are mainly two ways to realize jumps in Servlet, forward and redirect

foward(): request forwarding, redirection inside the server, the server directly accesses the URL of the target address, and reads the response content of that URL, but the client does not know it, so it will not be displayed in the address bar of the client browser Display the address after the jump, or the original address. Since the same Request is used in the entire orientation process, FORWARD will bring the Request information to the directed JSP or Servlet for use

redirect(): The redirection of the client is a complete redirection, that is, the client browser will obtain the redirected address and then resend the request, so the redirected address will be displayed in the browser. At the same time, because this method has one more network request than the FORWARD method, its efficiency is lower than the FORWARD method. It should be noted that the client-side redirection can be achieved by setting specific HTTP headers or writing JavaScript scripts.

When the general forward method can meet the demand, use forward as much as possible. But sometimes when you need to request a resource on another server, you must use the redirect method

11. BOM and DOM and their relationship

The BOM browser object model, which is composed of a series of objects, is a method to access, control, and modify the properties of the browser

The DOM document object model consists of a series of objects and is a standard method for accessing, retrieving, and modifying the content and structure of XHTML documents

relationship:

  • BOM describes the method and interface for interacting with the browser
  • DOM describes the methods and interfaces for processing web content
  • DOM is an attribute of BOM

12. The difference between get and post

1. From the perspective of data transmission, both get and post in http are insecure, and the transmitted data can be captured by third-party tools. What we usually call Get is insecure, because in the process of transmission, the data is placed in the requested URL; the Post data is placed in the request body, and the operation is invisible to the user

2. In terms of the amount of data, theoretically the amount of data transmitted by get and post is unlimited. The amount of data transmitted by Get is small, mainly because it is limited by the length of the URL in the browser; the amount of data transmitted by Post is large, and it is generally defaulted to be unlimited

3. Get execution efficiency is better than Post method, Get is the default submission method of the form

4. There is not much difference between get and post in essence, get can also put data in the request body, but different browser vendors implement differently, and may not be able to recognize the data in the request body in the get request, which causes the current get The difference with post

13, the four scopes of JSP

  • page represents objects and attributes related to a page
  • request represents objects and attributes related to a request issued by a Web client. A request may span multiple pages, involving multiple Web components, the temporary data displayed on the page can be placed in this scope
  • Session represents objects and attributes related to a session established by a user and the server, and data related to a user should be stored in the user's own session
  • application represents the objects and attributes related to the entire web application. It essentially spans the entire web application and is a global scope

14. Session tracking technology

Since the HTTP protocol itself is stateless, in order to distinguish between different users, the server needs to track the users back. The common understanding is that after the user logs in, the server will assign a unique id to the user, and the user's next request will include this id. The server judges the user based on the id

  • URL rewriting

Add user response information as a parameter in the URL

  • Set hidden form fields

Add the fields related to session tracking to the hidden form field, the information will not be displayed in the browser

  • cookie

There are two types of cookies. One is window-based. After the browser window is closed, the cookie is gone; the other is to store the information in a temporary file and set the time of existence. When the user establishes a session with the server through the browser, the session ID will be stored in a window-based cookie along with the response information.

As long as the browser is not closed and the session does not time out, the session id will be submitted to the server for the server to identify the user information in the next request.

  • session

When a user visits a website, the server will automatically create an HTTPSession, each user can access their own HTTPSession

15. Servlet life cycle

The life cycle of a servlet is the process from the appearance of the servlet to the destruction, which is mainly divided into the following stages

  • Load class
  • Instantiate, allocate space for the object
  • Initialization, assign values ​​to object properties
  • Request processing, service phase doget, doPost
  • destroy

16. How does the interface handle repeated requests?

Guess you like

Origin blog.csdn.net/weixin_54707168/article/details/113976846