Home  >  Article  >  Java  >  Java high-frequency basic interview questions——(4)

Java high-frequency basic interview questions——(4)

王林
王林forward
2020-09-02 16:08:571768browse

Java high-frequency basic interview questions——(4)

1. Talk about the life cycle of Servlet?

(Recommended more related interview questions: java interview questions and answers)

Servlet has a well-defined life cycle, including loading and instantiation, initialization, request processing, and service end. This lifetime is expressed by the init(), service() and destroy methods of the javax.servlet.Servlet interface.

After the Servlet is instantiated by the server, the container runs its init method, and when the request arrives, it runs its service method. The service method automatically dispatches the doXXX method (doGet, doPost) corresponding to the request, etc., when the server decides to use the instance When destroyed, its destroy method is called.

The web container loads the servlet and the life cycle begins. Initialize the servlet by calling the servlet's init() method. This is achieved by calling the service() method, and different do***() methods are called according to different requests. To end the service, the web container calls the servlet's destroy() method.

2. What is the difference between forward() and redirect() in Servlet API?

1. From the address bar display,

forward is when the server requests resources. The server directly accesses the URL of the target address, reads the response content of that URL, and then sends the content again. To the browser. The browser has no idea where the content sent by the server comes from, so its address bar is still the original address.

Redirect means that the server sends a status code based on logic to tell the browser to request that address again. So the address bar displays the new URL. So redirect means that the client sends two requests to the server. Also accepts two responses.

2. From the perspective of data sharing

forward: the forwarded page and the forwarded page can share the data in the request

redirect: cannot share data

redirect can not only redirect to other resources of the current application, but also redirect to resources in other applications on the same site, and even redirect to resources of other sites using absolute URLs

forward The method can only forward requests between resources within the same web application. Forward is an operation inside the server

redirect is when the server notifies the client and allows the client to re-initiate the request

Therefore, you can say that redirect is an indirect request, but you cannot say "whether a request belongs to forward or redirect"

3. From the perspective of application

forward: generally used When the user logs in, it is forwarded to the corresponding module according to the role.

redirect: Generally used to return to the main page and jump to other websites when the user logs out.

4. Efficiency For example

forward:high.

redirect:low.

3. What is the difference between request.getAttribute() and request.getParameter()?

1. request.getParameter() is obtained through the implementation of the container to obtain the data passed in through methods such as post, get, etc.

request.setAttribute() and getAttribute() only flow within the web container and are only in the request processing stage.

2. getAttribute returns an object, getParameter returns a string

3. getAttribute() is always used together with setAttribute(). It can only be passed after setting it with setAttribute() first. getAttribute() to get the value, they pass Object type data. And it must be used in the same request object to be valid. , and getParameter() is to receive the parameters submitted by the get or post of the form

(Video tutorial recommendation: java course)

4. JSP static inclusion and dynamic inclusion The difference

1. <%@include file="xxx.jsp"%> is a compilation instruction in jsp. The inclusion of its file occurs during the conversion period from jsp to servlet, while is an action instruction in jsp. The inclusion of its files occurs during compilation, that is, the period when java files are compiled into class files

2. Using static inclusion will only generate one class file, while using dynamic inclusion will generate multiple class files

3. Using static inclusion, the request object of the containing page and the included page is the same object, because static Inclusion only copies the content of the included page to the included page; while dynamic inclusion, the including page and the included page are not the same page, and the range of parameters that can be obtained by the request object of the included page is relatively larger, not only You can get the parameters passed to the containing page, and you can also get the parameters passed down from the containing page

5. What technologies are used to implement each part of MVC? How to implement it?

MVC is the abbreviation of Model-View-Controller. Model represents the business logic of the application (implemented through JavaBeans and EJB components), View is the presentation surface of the application (generated by JSP pages), and Controller provides process control of the application (usually a Servlet). Through this design model Divide application logic, processing and display logic into different component implementations. These components can be interacted with and reused.

6. What are the built-in objects in jsp? What are their functions?

JSP has the following 9 built-in objects:

1. request client request, This request will contain parameters from the GET/POST request

2. The response web page returns the response from the client.

3. The pageContext properties of the web page are managed here.

4. Session is the session period related to the request.

5 , The content of the application servlet being executed

6. Out is used to send the response output

7. The framework component of the config servlet

8. The page JSP web page itself

9. exception Uncaught exception for error web pages

7. The difference between get and post methods in Http

1. Get is a request to the server for data , and Post is a request to submit data to the server

2. Get is to obtain information, not to modify information. Similar to the database query function, the data will not be modified

3. Get The parameters of the request will be passed after the URL, and the data of the request will be appended to the URL to split the URL and transmit the data. Parameters are connected with &. The XX in %XX is the ASCII symbol expressed in hexadecimal. , if the data is English letters/numbers, send it as it is, if it is a space, convert it to, if it is Chinese/other characters, directly encrypt the string with BASE64.

4. There is a size limit on the data transmitted by Get. Because GET submits data through the URL, the amount of data that can be submitted by GET is directly related to the length of the URL. Different browsers have different requirements for the length of the URL. The restrictions are different.

5. The data requested by GET will be cached by the browser, and the username and password will appear in clear text on the URL. Others can check the historical browsing records, and the data is not safe.

On the server side, use Request.QueryString to obtain the data submitted by Get method.

Post request is sent to the web server as the actual content of the http message. The data is placed in the HTML Header and submitted. Post does not limit the data submitted. Post is safer than Get. When the data is in Chinese or insensitive data, use get because with get, the parameters will be displayed in the address. For sensitive data and data that are not Chinese characters, use post.

6. POST represents a request that may modify resources on the server. On the server side, data submitted in the Post method can only be obtained using Request.Form.

8. What is a cookie? What is the difference between Session and cookie?

Cookie is a session technology, which saves the user's information to the browser object.

(Related recommendations: Java Getting Started)

Difference:

1. Cookie data is stored on the client's browser, and session data is placed on the server.

2. Cookies are not very secure. Others can analyze the COOKIE stored locally and perform COOKIE Deception, if security is the main consideration, session

3 should be used. The session will be saved on the server for a certain period of time. When access increases, it will take up more of your server's performance. If you mainly consider reducing server performance, you should use COOKIE

4. The limit of a single cookie on the client is 3K, which means that a site stores on the client COOKIE cannot be 3K.

Conclusion:

Store important information such as login information as SESSION; if other information needs to be retained, it can be placed in COOKIE.

9. What are the differences, common points, and scope of application between jsp and servlet?

JSP is an extension of Servlet technology, which is essentially a simple way of Servlet. After JSP is compiled, it is a "servlet-like".

The main difference between Servlet and JSP is that the application logic of Servlet is in the Java file and is completely separated from the HTML in the presentation layer. In the case of JSP, Java and HTML can be combined into a file with a .jsp extension.

JSP focuses on views, and Servlet is mainly used for control logic. In the struts framework, JSP is located in the view layer of the MVC design pattern, and Servlet is located in the control layer.

10. How does the tomcat container create a servlet class instance? What principles are used?

When the container starts, it will read the web.xml files in all web applications in the webapps directory, then parse the xml files, and read the servlet registration information. Then, the servlet classes registered in each application are loaded and instantiated through reflection. (Sometimes it is also instantiated on the first request)

Add 1 when registering the servlet. If it is a positive number, then Instantiate at the beginning. If not written or is a negative number, instantiation is requested for the first time.

The above is the detailed content of Java high-frequency basic interview questions——(4). For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete