The difference between JSP and Servlet in Java Web

      The operating principle of the servlet program is shown in the figure below. When the client submits a request, the Web server receives the request and hands the request to the servlet engine for processing. When the servlet engine finds the servlet specified by the request, it will call the service method in the servlet class, and pass the request information and response information into the service method (return the corresponding client code, such as HTML, JavaScript, etc.).

      Although Servlet can realize the functions of all dynamic web applications, when using Servlet to display the user interface, the client code must be sent to the client using the HttpServletResponse object. When the client page is very complex, it will lead to a considerable workload and is not easy to maintain. . Then came JSP , JSP and ASP are very similar, they all mix client-side code and server-side code in the same page. As shown in the figure below, when the JSP page is accessed for the first time, the JSP page will be translated into a servlet by the JSP engine, and then handed over to the servlet engine to run. If the page is not accessed for the first time, the web server will directly hand over the request to the servlet. Engine to run servlet programs that have been generated by JSP.


In conclusion:

①JSP pages are essentially Servlets.

JSP working principle: JSP page will be converted into Servelet (.java) by the server-side JSP engine during execution, and then the JSP engine will call the Java compiler to compile the Servelet (.java) into a Class file (.class), and Interpreted and executed by the Java Virtual Machine (JVM). If there is a JSP page My.jsp, when you visit http://localhost:8080/My.jsp through a browser, the execution result will appear. At the same time, there are two more files under %CATALINA_HOME%/work/Catalina/localhost: _My_jsp.java and _My_jsp.class, which are the Servelet and Class files mentioned earlier. Therefore, the JVM can only recognize java classes, not JSP codes. The web container will eventually compile the JSP codes into java classes that the JVM can recognize.

②The application logic of Servlet is in the Java file, which dynamically outputs HTML from the Java code, and is completely separated from the HTML in the presentation layer. And JSP is Java and HTML can be combined into a file with the extension .jsp. JSP focuses on the view, and Servlet is mainly used for control logic.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325898883&siteId=291194637