In the java web section, I summarized all the interview questions

Posted by McMaster on Wed, 29 Dec 2021 23:56:25 +0100

 servlet There is a good definition of lifetime, including loading and instantiation, initialization, processing requests, and service termination. This survival period is determined by javax.servlet.Servlet Interfaced init,service and destroy Method expression.

	servlet After being instantiated by the server, the container runs its init Method to run its when the request arrives service method, service Method to automatically dispatch the run corresponding to the request doXXX Method( doGet,doPost)When the server decides to destroy the instance, call its destroy method.

	web Container loading servlet,The life cycle begins. By calling servlet of init()Method servlet Initialization of. By calling service()Method implementation, calling different methods according to different requests do***()method. End of service, web Container call servlet of destroy()method. 

5. Basic architecture of Servlet

package test;

import java.io.IOException;

import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

public class ServletName extends HttpServlet {

	public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, 			IOException {

	}

	public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, 			IOException {

	}

} 

6. What is the difference between forward () and redirect() in servlet API?

The former is only the change of control in the container, and the address after the change will not be displayed in the address bar of the client browser; The latter is a complete jump. The browser will get the jump address and resend the request link. In this way, you can see the link address after jump from the address bar of the browser. Therefore, the former is more efficient. When the former can meet the needs, try to use it forward()Method, and it also helps to hide the actual links. In some cases, for example, you need to jump to a resource on another server sendRedirect()method. 

7. Common methods of Request object

setAttribute(String name,Object): Set name to name of request Parameter value of

getAttribute(String name): Return by name

getCookies(): Returns all of the client's Cookie Object, the result is a Cookie array

getCharacterEncoding(): Returns the character encoding method in the request

getParameter(String name): Get the information transmitted from the client to the server name Specified parameters

getRequestURI(): Gets the address of the client issuing the request string

getRemoteAddr(): Get client's IP address

getRemoteHost(): Gets the name of the client

getServerName(): Gets the name of the server

getServletPath(): Gets the path to the script file requested by the client

getServerPort(): Gets the port number of the server

removeAttribute(String name): Delete an attribute in the request 

8. Briefly describe the function and usage of HttpSession, which can be described by code

HttpSession User information can be tracked and stored in, and the value can be set into the attribute. There are two methods: setAttribute(),getAttribute();

For example, in a method session.setAttribute("student",student);In a session Set a property named student,The value is a value named student Object. Then at the same time session Used within the scope getAttribute("student")Take out the attribute and get student Object. 

9. What are the built-in objects of jsp? What are the functions? What are the methods?

jsp There are 9 built-in objects:

request   Client request, which will contain information from GET/POST Requested parameters

response  The web page returns the client's response

pageContext  The properties of web pages are managed here

session   Session duration associated with the request

application  servlet Executing content

out The output used to transmit the response

config servlet Frame components

page JSP Web page itself

exception  Uncapped exceptions for error pages





request: express HttpServletRequest Object. It contains information about browser requests and provides several methods for obtaining cookie, header, and session A useful method of data.

response: express HttpServletResponse Object and provides several methods for setting the response sent back to the browser (such as cookies,Header information, etc.)

out: Object is javax.jsp.JspWriter And provides several methods that you can use to send back the output results to the browser.

pageContext: Represents a javax.servlet.jsp.PageContext Object. It is used to facilitate access to a wide range of namespaces servlet Of related objects API,And packed with general servlet Methods of related functions.

session: Represents a request javax.servlet.http.HttpSession Object. Session User status information can be stored

applicaton : Represents a javax.servle.ServletContext Object. This helps you find information about servlet Engine and servlet Environmental information

config: Represents a javax.servlet.ServletConfig Object. This object is used to access servlet Initialization parameters of the instance.

page: Represents a generated from the page servlet example 

10. How to use JavaBean s in JSP?

stay JSP Used in JavaBean Common actions are:

<jsp:useBean />: Used to create and find bean Object;

<jsp:setProperty />: Used to set bean Property of the, that is, its setXxx()method;

<jsp:getProperty />: Used to obtain bean Property of the, that is, its getXxx()method 

11. Common instructions for JSP

isErrorPage(Can it be used Exception object),isELIgnored(Ignore expression) 

12. Four ranges of jsp?

page Valid on the same page;

request It is valid between the same request (request page). Generally, it will become invalid after the request is completed, but if it passes forward Jump in the same way, then forward The page is still available request But if it is through redirect It is equivalent to sending a page request again, request The value in is invalid;

session It is effective in the life cycle of a session. In short, it is the time of connecting to the server. Note that all pages are shared. If the connection is disconnected during this period, it will become invalid;

application The scope is the largest. It will not expire until the server stops. All pages are shared. However, it should not be defined too much, otherwise it may increase the burden on the server; 

13. jsp scope from large to small

application> session> request> page

It is worth noting that one request Can contain multiple page(include, forward, filter) 

14. What are the functions of filters?

It can verify whether the customer comes from a trusted network, re encode the data submitted by the customer, obtain the configuration information from the system, filter out some words that should not appear, verify whether the user logs in, verify whether the customer's browser supports the current application, record the system log, etc. 

15. Filter usage? (uniformly encode the requests of the client and authenticate the client)

First of all, we should realize Filter Interface, covering at the same time Filter Three methods of interface:

init(FilterConfig config)//Used to obtain the FilterConfig object;

doFilter(ServletRequest request,ServletResponse response,FilterChain chain)//Some businesses for filtering;

destroy()//Destroy the Filter. 

16. How to realize the request forwarding in JSP and Servlet respectively?

JSP Request forwarding in can take advantage of forward Action realization:<jsp:forward/>;

Servlet The method of request forwarding in is as follows: getServletContext().getRequestDispacher(pach).forward(req.res).  

17. What are the similarities and differences between JSP and Servlet, and what is the relationship between them?

JSP yes Servlet The extension of technology is essentially Servlet Simple way, more emphasis on the application of appearance expression. JSP Compiled as"class servlet". Servlet and JSP The main difference is, Servlet The application logic is Java File, and completely from the presentation layer HTML Separate from the inside. and JSP The situation is Java and HTML Can be combined into one extension.jsp File. JSP Focus on views, Servlet Mainly used for control logic. 

18. Describe MVC in detail.

At the end of the paper, java interview questions, advanced technology outline, architecture and data sharing

I sorted out all the topics of the three Ali interviews, attached with detailed answer analysis, and generated a PDF document. Friends who are interested You can click here to get it for free

  • The first thing to share with you is algorithms and data structures

  • The second is the high-frequency knowledge points and performance optimization of the database

  • The third is concurrent programming (72 knowledge points learning)

  • The last one is the interview point of major JAVA architecture topics + analysis + some of my learning books and materials

204888/java-p7)

  • The first thing to share with you is algorithms and data structures

[external chain picture transferring... (img-quvTFL0N-1628485720788)]

  • The second is the high-frequency knowledge points and performance optimization of the database

[external chain picture transferring... (img-mE7wpk7U-1628485720791)]

  • The third is concurrent programming (72 knowledge points learning)

[external chain picture transferring... (img-dUvZ2kBD-1628485720793)]

  • The last one is the interview point of major JAVA architecture topics + analysis + some of my learning books and materials

[external chain picture transferring... (img-pxElz22K-1628485720796)]

There are more learning notes on Redis, MySQL, JVM, Kafka, microservices, Spring family bucket and so on, which are not listed here one by one

Topics: Java Back-end Interview Programmer