Talk about Java Web (Servlet + JSP) those things

Question1: What is the relationship and difference between servlet & jsp-jsp and servlet?

Answer1:

1. As far as the JVM is concerned, the JVM can only recognize java classes, not JSP codes. The Web container compiles the JSP codes into java classes that the JVM can recognize . So, jsp becomes Servlet after compiling, the essence of JSP is Servlet.

2. In essence, Servlet is a complete Java class, and JSP is a simplification of Servlet . Servlet is a complete Java class. The Service method of this class is used to generate a response to the client; JSP is a simplification of Servlet. Using Jsp only needs to complete the content that the programmer needs to output to the client. As for the Java script in JSP Embedded into a class, completed by the JSP container, programmers do not have to worry.

3. In terms of pages , jsp is better at displaying pages, and servlets are better at logic control.

4. In terms of built-in objects, there are no built-in objects in Servlet, there are 9 kinds of built-in objects in Jsp, but they must be obtained through HttpServletRequest object, HttpServletResponse object and HttpServlet object.

Question2: What are the built-in objects of servlet & jsp-jsp? What is the role?

Answer2:

JSP has 9 built-in objects:

request: encapsulates the client's request, which contains the parameters from the GET or POST request;

response: encapsulates the server's response to the client;

pageContext: You can get other objects through this object;

session: the object that encapsulates the user's session;

application: objects that encapsulate the server's operating environment;

out: output stream object output by the server response;

config: configuration object of web application;

page: JSP page itself (equivalent to this in a Java program);

exception: Object that encapsulates the exception thrown by the page.

Question3: servlet & jsp-talk about the 4 scopes of jsp?

Answer3:

The four scopes in JSP include page, request, session, and application, specifically:

Page represents objects and attributes related to a page.

request represents the objects and attributes related to a request issued by the Web client. A request may span multiple pages and involve multiple Web components; temporary data that needs to be displayed on the page can be placed in this scope.

session represents objects and attributes related to a session established between a user and the server. Data related to a user should be placed in the user's own session.

application represents the objects and attributes related to the entire Web application. It is essentially a global scope that spans the entire Web application, including multiple pages, requests, and sessions.

Question4: session & cookie-What is the difference between session and cookie?

Answer4:

Since the HTTP protocol is a stateless protocol, when the server needs to record the state of the user, it needs to use a mechanism to identify the specific user. This mechanism is the Session. Typical scenarios such as shopping carts, when you click the order button Since the HTTP protocol is stateless, it does not know which user is operating, so the server must create a specific session for a specific user, used to identify this user, and track the user, so that we know how many in the shopping cart The book. This session is stored on the server and has a unique identifier. There are many ways to save the session on the server, including memory, database, and disk files. When clustering, you should also consider the transfer of Session. On large websites, there will usually be a dedicated Session server cluster to save user sessions. At this time, Session information is stored in memory, and some cache services such as Memcached are used. Come and put Session.

Think about how the server identifies specific customers? At this time, Cookie appeared. Each time the HTTP request, the client will send the corresponding Cookie information to the server. In fact, most applications use cookies to implement session tracking. When creating a session for the first time, the server will tell the client in the HTTP protocol that a session ID needs to be recorded in the cookie. The session ID is sent to the server and I know who you are. Someone asks, what if the client's browser disables cookies? Generally, in this case, a technique called URL rewriting is used for session tracking, that is, every HTTP interaction, the URL will be appended with a parameter such as sid = xxxxx, and the server recognizes the user based on this.

Cookies can actually be used in some user-friendly scenarios. Imagine that you have logged in to a website once and you do n’t want to enter your account number again the next time you log in. What should you do? This information can be written in a cookie. When you visit the website, the script on the website page can read this information, and it will automatically fill in the user name for you, which can facilitate the user. This is also the origin of the cookie name, a little sweetness to the user.

So, to sum up: Session is a data structure saved on the server, used to track the user's state, this data can be saved in the cluster, database, file; Cookie is a mechanism for the client to save user information, used to record Some user information is also a way to implement Session .

Question5: session & cookie-talk about how the session works?

Answer5:

In fact, the session is a file resembling a hash table stored on the server. It contains the information we need and can be taken out of it when we need it. Similar to a large map, the keys inside store the user's sessionid, and the user will bring this sessionid when sending a request to the server. At this time, the corresponding value can be taken out from it.

Question6: session & cookie-can the session be used if the client prohibits cookies?

Answer6:

Cookie and Session are generally considered to be two separate things. Session adopts the scheme of maintaining state on the server side, while Cookie adopts the scheme of maintaining state on the client side. But why can't you get Session if you disable cookies? Because the Session uses the Session ID to determine the server session corresponding to the current session, and the Session ID is passed through the cookie, disabling the cookie is equivalent to losing the Session ID, and you will not get the Session.

Assuming that the user uses the session when the cookie is closed, that is, without using the cookie to pass the Session ID, you can use the following methods to pass the Sesssion ID:

1. Set "session.use_trans_sid = 1" in the php.ini configuration file, or open the "--enable-trans-sid" option when compiling, and let PHP automatically transfer the Session ID across pages.

2. Manually pass the value by URL and pass the session ID by hiding the form.

3. Save the session ID in the form of disk files, databases, etc., and call it manually during the cross-page process.

Question7: struts & springmvc-What is the difference between struts and springmvc?

Answer7:

Intercept level, intercept implementation, network request, singleton, url, receive parameter

Struts2 is a class-level interception;
Struts2 interception uses its own Interceptor mechanism, resulting in Struts2's configuration file is larger than SpringMVC;
each request will create an Action, you need to load all the attribute value injection, performance is low;
can not use annotations or Other ways to identify the method belongs to, can only be designed as multiple cases;
one Action corresponds to one request-response context, one method in Action corresponds to one url;
when receiving parameters, it is received through the class attribute, because the class attribute parameter is to allow multiple methods Shared, so all methods in the class share request-response data.

SpringMVC is a method-level interception;
SpringMVC uses an independent AOP method;
all requests will only create a Controller, only need to load a method parameter injection, performance is higher;
you can use annotations or other methods to identify the method, designed as a single For example, the processing result is finally returned to the framework through the ModelMap;
one method corresponds to a request-response context, and one method corresponds to a url;
when receiving parameters, it is received through the method parameters, because the method parameters are unique to each method, so each method Exclusive request-response data.

Underlying framework and timing of initialization

The bottom layer of Struts2 is implemented by Filter (StrutsPrepareAndExecuteFilter). The Filter is initialized after the container is started, and the service stops crashing. The
bottom layer of SpringMVC (DispatcherServlet) is implemented by Servlet. The Servlet is initialized when the container is called, and the service stops being destroyed.
Because the initialization of Servlet is earlier than the initialization of Filter, the call of SpringMVC is earlier than the call of Struts2.

Integration with Spring

When Struts2 is integrated in Spring, the Action Bean of Struts2 is prototype mode by default.
When SpringMVC is integrated in Spring, the Controller Bean of SpringMVC is Singleton by default, so by default, only one Controller will be created for all requests, and because there are no shared properties, it is thread-safe. If you want to change the default scope , Need to add @Scope annotation to modify.

As a child container of Spring, SpringMVC is seamlessly integrated with Spring, and the management and security of the entire project are higher than Struts2.

Struts2 SpringMVC
Intercept level Class interception Method interception
Interception implementation Struts2 has its own Interceptor mechanism, so the configuration file is large SpringMVC uses an independent AOP method, so the configuration file is small
Network request Each request will create an Action, which needs to be loaded with all attribute injections, with low performance All requests will only create a Controller, only need to load the method parameter injection once, the performance is higher
Singleton Annotations or other methods cannot be used to identify the method, and can only be designed as multiple cases You can use annotations or other methods to identify the method, design it as a singleton, and finally return the processing result to the framework through the ModelMap
url An Action corresponds to a request-response context, and a method in Action corresponds to a url One method corresponds to one request-response context, and one method corresponds to one url
Receive parameters When receiving parameters, receive through the class attribute, because the class attribute parameter is shared by multiple methods, so all methods in the class share request-response data When receiving parameters, receive through the method parameters, because the method parameters are exclusive to each method, so each method exclusively requests-response data
Underlying framework and timing of initialization Filter Servlet, because the initialization of Servlet is earlier than the initialization of Filter, so the call of SpringMVC is earlier than the call of Struts2
Spring integration During Spring integration, the Action Bean of Struts2 defaults to the prototype mode Prototype During Spring integration, Spring MVC's Controller Bean defaults to singleton mode Singleton, and SpringMVC as a child container of Spring, and Spring integration is seamless
Published 207 original articles · praised 80 · 120,000 views

Guess you like

Origin blog.csdn.net/qq_36963950/article/details/105554813