Servlet Introduction: Servlets are a set of specifications that the Sun company proposes to standardize how Java develops dynamic Web sites. It provides support for server-side Java development and is the foundation of Javaweb Technology.
a A servlet is a Java class, but it is not an applet (Java application), because the servlet does not have a main method, he cannot execute it independently, it must be invoked and managed by the servlet container, control the creation, execution of the servlet, Destruction and other life cycle phases.
The servlet works by starting the server, making a request from the client, sending the request to the server through the computer network, creating a servlet on the server, and invoking the appropriate method (DoPost, Doget ...) based on the client request. Returns a response (response object) to the client after processing the request.
Web container: Also known as a Web server, tomcat is the Web container, and the servlet container we just mentioned is part of the Web container. The HTTP request made by the client first passes through the Web container, the Web container parses the HTTP request into a request object to the server servlet, and the response object returned by the server processing the request needs to be parsed by the Web container into an HTTP message to be returned to the client. The browser parses the HTTP message and displays the response results to the user.
The browser is mentioned here: now the mainstream Network program development architecture has been c/s turns to B/s, which refers to Brower browser. Browser is the HTML, CSS, JavaScript and other responsible for the page display of the scripting language interpreter, it supports the URL and can send HTTP requests through the URL, parsing HTTP requests, the browser also supports a variety of images, video, audio display; Now the browser also supports more features, such as cookies, caches, multiple network protocols ... In short, browsers are an absolutely integral part of today's web application development.
The advent of jsp:jsp is to solve dynamic response view problems, such as CGI programs, when a dynamic return response page is required, only through the Response.getwriter (). Write () method The output HTML script of one line is returned to the client, which is cumbersome, And the servlet, as the control layer and the view layer coupling, does not conform to the MVC architecture idea; With JSP, we can directly respond to a. JSP view or map to a logical view of a. JSP view (which is used in Struts2), where you can write HTML, CSS, JS code in the JSP, It is convenient to embed Java code, JSP-supported JSTL tags, ognl tags such as Struts2 such as framework support, to realize the function of dynamically displaying views according to the response data, and the servlet is responsible for the control of the business logic, JSP is responsible for the view layer work (interacting with the user, Making a request to the server, receiving a response from the server, and dynamically displaying the view from the response data, and then implementing a simple MVC architecture using the JavaBean encapsulated data model.
JavaBean: If we embed a lot of Java code in the JSP in order to use the server-side response data, our code looks very confusing and difficult to maintain. We expect the page to be static (containing only static content, such as HTML,CSS,JS, etc.), so we will encapsulate the parameters needed to complete a business logic into JavaBean, replacing the Java code with the JavaBean component tag in the JSP to ensure the static composition of the page. JavaBean is actually a Java class with a private property, which generally includes a constructor with arguments and no arguments and a get and set method for each property.
I just kept saying that. MVC Architecture: A Web development model that divides Web applications into three tiers
M--model model layer, generally refers to our development of database tables and entity (Pojo), DAO, persistence package content, mainly responsible for the data model packaging and database related operations;
V--view view layer, generally refers to the page such as JSP, HTML, etc., is mainly responsible for interacting with the user, to the server to make requests, receive responses, the response data from different dynamic display views.
C--controller control layer, generally refers to the project in the Servlet, action, controller package content, mainly responsible for receiving client requests, call the model layer related methods to process the request, and then return the response to the client.
service\serviceimpl--here, it should be explained that in the actual development, the control layer will not directly invoke the model layer method for data manipulation, often by invoking the service layer service, call the model layer method in Serviceimpl to realize the business. Because a function may be done by several basic database operations plus some special logic judgments, the interface-oriented design through Service\serviceimpl can further decouple the control layer from the model layer, making the program result clearer and easier to maintain and extend.
The advantage of MVC is that the hierarchy makes the program clear, so that each layer of developers can focus on their own modules, easy to develop, program decoupling makes the program easier to maintain and expand; The view layer's independence enables multiple businesses to share a single view as a response, enhancing the reusability of the code. The common MVC Framework is STRUTS2,SPRINGMVC and so on (more specifically, these frameworks are only part of MVC, and Struts2 and SPRINGMVC are more involved in the control layer, Persistent layer frameworks such as Hibernate and MyBatis are used to handle model-level database operations.
Learning Javaweb can't escape. Three technologies: Filters, interceptors, listeners
Filter: Prior to The servlet executes with the JSP, which is dependent on the Web container implementation. When a client makes a request (whether it is requesting an action or a direct location of the resource), it can be filtered by the filter. Most of us use filters to do some general processing, such as filtering of illegal URLs, setting of character encodings, or determining user permissions The core class of Struts2 is a filter called Strutsprepareandexcutefilter.
The Interceptor: similar to the filter function, but not dependent on Web container implementations. Interceptors are the core function of Struts2 and are an AOP concept. The interceptor can also intercept the request for preprocessing, but it can only intercept the action and not intercept the direct location of the resource, which is an important consideration when we choose to use filters and interceptors. The interceptor is more powerful than the filter, and he can access the action context Actioncontex to get the request, response, session, and so on.
The difference between interceptors and filters is also reflected in design thinking: Filters are filtered to get qualified requests or responses; The interceptor is blocking, blocking unreasonable requests or responses.
listener: Not commonly used, can listen to the session, servlet context, often used for some specific functions such as some initialization configuration work, such as when the servlet started to configure the database connection and connection pool.
just said the interceptor was a The concept of AOP, what is AOP? The following paragraph is an explanation of the AOP that I have read most understandable.
plane-oriented programming ( AOP is the acronym for Aspect Oriented program, and we know that object-oriented features are inheritance, polymorphism, and encapsulation. Encapsulation requires that functions be dispersed into different objects, which is often referred to as assignment of responsibilities in software design. In fact, let's say that different classes are designed to have different methods. So the code is scattered in a class. The benefit of this is to reduce the complexity of the code and make the class reusable.
However, it is also found that the code duplication is increased while the code is dispersed. What do you mean? For example, in two classes, we may need to log in each method. In terms of object-oriented design, We have to include the contents of the log in the methods of two classes. Perhaps they are exactly the same, but it is because object-oriented design makes it impossible for a class to be associated with a class, but not to unify the duplicated code.
someone might say, well, we can write this code in a separate class-independent method, and then call it in these two classes. However, in this way, these two classes are coupled with the independent classes we mentioned above , and the changes will affect these two classes. So, is there any way that we can randomly add code when we need it? At run time, the programming idea of dynamically cutting code into the class , at the specified location, is the aspect-oriented programming.
In general , the code snippet in which we cut into the specified class is called a slice, and which class to cut into and which method is called the pointcut. With AOP, we can extract several classes of common code into a slice, and then cut into the object when needed, thus altering its original behavior.
so it seems, AOP is really just a complement to OOP. OOP distinguishes a class from a landscape, while AOP adds a specific code to the object vertically. With the Aop,oop became three-dimensional. If you add a time dimension, AOP makes OOP from the original two dimensions into three-dimensional, from the plane to three-dimensional. Technically, AOP is basically implemented through proxy mechanisms.
AOP can be a milestone in programming history, and it is a useful complement to OOP programming.
Spring's AOP is the most successful to do, but at this stage we may use more of the other features of spring, control inversion (IOC) and Dependency Injection (DI):
IOC is a design idea called "inversion of Control".
Shallow level --parsing from the name
"Control" refers to the creation of objects, maintenance, destruction and other life cycle control, this process is generally by our program to actively control, such as using the New keyword to create an object (create), in the use of the process to maintain the reference (maintenance), after the loss of all references by the GC to reclaim objects (destroy).
"Inversion" means that the control of the life cycle of object creation, maintenance and destruction is changed from program control to IOC container, and it is obtained directly from the IOC container by name when an object is needed.
deeper levels -reference to Di, dependency injection, is an important implementation of the IOC
the creation of an object often involves the creation of other objects, such as an object A's member variable holds a reference to another object B, which is dependency, a depends on B. Since the IOC mechanism is responsible for the creation of objects, this dependency must also be handled by the IOC container. The way to be responsible is di--dependency injection, by writing dependencies to the configuration file, and then injecting dependent objects by the IOC container when creating dependent objects, such as when creating a, checking for dependencies, the IOC container injects object B of a dependency into a (assembly, implemented by reflection mechanism) , and then return a to the object requester to complete the work.
What does the IOC mean?
The IOC does not implement more features, but it does not require much code, does not need to consider the complex coupling between objects to get the right object from the IOC container, and provides reliable management of the object, greatly reducing the complexity of the development.
Learn Javaweb a year, little talk about some basic knowledge of understanding < turn >