JSP study notes one

JSP is an extension of Servlet. Before there was no JSP, Servlet technology appeared. Servlet uses the output stream to dynamically generate HTML pages, including every HTML tag and every content that appears in the HTML page.

Due to including a large number of HTML tags, a large number of static text and formats, etc., the development efficiency of Servlet is extremely low. All the presentation logic, including layout, colors, and images, has to be coupled in Java code, which is really annoying. The emergence of JSP makes up for this deficiency. JSP inserts Java code into standard HTML pages, and its static part does not need Java program control. Only those who need to read from the database and dynamically generate information according to the program use Java script control. . If you use Servlet to control the appearance of the page, it will be a very troublesome thing. Using JSP will give the complicated printing task to the JSP engine, and the programmer can concentrate on the logic control.

On the surface, JSP pages no longer need Java classes, and seem to be completely out of Java's object-oriented features. In fact, JSP is a special form of servlet, and each JSP page is a servlet instance - JSP pages are compiled into servlets by the system, and servlets are then responsible for responding to user requests. JSP is actually a simplification of Servlet. When using JSP, Servlet is still used, because each JSP page in a web application will generate a corresponding Servlet by the servlet container. For Tomcat, the Servlet generated by the JSP page is placed under the Web application corresponding to the work path.

The difference and connection between jsp and servlet:
1. JSP becomes Servlet after being compiled.
(The essence of JSP is Servlet, JVM can only recognize java classes, not JSP code, Web container compiles JSP code into JVM Recognized java class)
2.jsp is better at page display, servlet is better at logic control.
3.There is no built-in object in Servlet, the built-in object in Jsp must be obtained through HttpServletRequest object, HttpServletResponse object and HttpServlet object.
Jsp is a simplification of Servlet. Using Jsp only needs to complete the content that the programmer needs to output to the client. How the Java script in Jsp is embedded into a class is completed by the Jsp container.
The Servlet is a complete Java class, the Service method of this class is used to generate the response to the client.

Contact:
JSP is an extension of Servlet technology, which is essentially a simple way of Servlet. JSPs are "servlet-like" after compilation.
The main differences between servlets and JSPs are:
The application logic of servlets is in Java files and is completely separated from HTML in the presentation layer.
In the case of JSP, Java and HTML can be combined into a single file with a .jsp extension.
JSP focuses on the view, Servlet is mainly used to control
the logic Servlet is more similar to a Controller, used for control.

1. What is the difference?

Servlet dynamically outputs HTML content in Java code through HttpServletResponse object JSP embeds Java code in
static HTML content, and Java code is dynamically executed to generate HTML content Generating dynamic HTML content by string splicing in source files will lead to difficult code maintenance and poor readability. Although JSP avoids the disadvantage of Servlet in generating HTML content, it is also not advisable to mix a large number of complex business logic into HTML. 3. Combining two swords through MVC: Since JSP and Servlet have their own applicable environments, can they make use of their strengths and circumvent weaknesses and let them play their respective advantages? The answer is yes - the MVC (Model-View-Controller) pattern is great for solving this problem.




The MVC pattern (Model-View-Controller) is a software architecture pattern in software engineering that divides the software system into three basic parts: Model, View and Controller:

Controller - responsible for forwarding and processing requests View -
responsible for interface display
Model - business function writing (such as algorithm implementation), database design and data access operation implementation
write picture description here

1. The web browser sends an HTTP request to the server, which is obtained and processed by the Controller (Servlet) (such as parameter parsing, request forwarding)
2. The Controller (Servlet) calls the core business logic - the Model part, and obtains the result
3. Controller( Servlet) passes the logic processing results to View (JSP), and dynamically outputs HTML content
4. The dynamically generated HTML content is returned to the browser for display

The benefits of the MVC pattern in web development are very obvious. It avoids the shortcomings of JSP and Servlet. Servlet is only responsible for business logic and does not dynamically generate HTML code through out.append(); JSP will not be flooded with a lot of business code. This greatly improves the readability and maintainability of the code.

Tomcat environment:
Tomcat environment variable Catalina_Home configuration

1. CATALINA_HOME is the alias of the TOMCAT installation path, the purpose is to facilitate the use of TOMCAT

2. Computer > Properties > Environment Variables, create a new environment variable. The variable name is CATALINA_HOME, and the variable value is the decompression directory of tomcat. The one on my computer is: "D:\apache-tomcat-9.0.5", please note that there is no need to add "\" or ";"

3. In the environment variable Path, add "%CATALINA_HOME%\bin\"

3. Start tomcat: enter the command line cmd, enter startup.bat

4. Open the browser and enter: http://localhost:8080 , (if the port number is modified, add the corresponding port) to see if tomcat starts normally

5. Close tomcat: enter the command line cmd, enter shutdown.bat

Tomcat directory structure:
write picture description here

bin: binary executable file. The most commonly used file in it is startup.bat, and if it is a Linux or Mac system, the startup file is startup.sh.
conf: Configuration directory. The core file inside is server.xml. You can change the port number in it. The default port number is 8080, which means that this port number cannot be used by other applications.
lib: library file. The directory where the jar package needed by tomcat is running
logs: log
temp: a temporarily generated file, that is, a cached
webapps: web application. The web application is placed in this directory and the browser can directly access
the work: compiled class file.

Http请求信息:
GET / HTTP/1.1
Host: localhost:9998
Connection: keep-alive
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.186 Safari/537.36
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,/;q=0.8
Accept-Encoding: gzip, deflate, br
Accept-Language: zh,zh-TW;q=0.9,en-US;q=0.8,en;q=0.7,zh-HK;q=0.6

How a web server uses JSP to create web pages:
write picture description here

Just like any other normal web page, your browser sends an HTTP request to the server.

The web server recognizes that this is a request for a JSP web page and passes the request to the JSP engine. This is done by using a URL or a .jsp file.

The JSP engine loads JSP files from disk and converts them into servlets. This conversion simply converts all template text to println() statements and converts all JSP elements to Java code.

The JSP engine compiles the servlet into an executable class and passes the original request to the servlet engine.

Some component of the Web server will call the servlet engine, which will then load and execute the servlet class. During execution, the servlet generates HTML-formatted output and delivers it to the Web server embedded in an HTTP response.

The web server returns the HTTP response to your browser as a static HTML page.

Ultimately, Web browsers treat dynamically generated HTML pages in HTTP responses as if they were static pages.

The variables and methods declared in <%! %> exist as attributes and methods of the class. Methods cannot be declared
in <% %>, and the variables declared in <% %> are used as internal attributes of the method _jspService.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326719675&siteId=291194637