The relationship between Jsp and Servlet

1. Servlet working principle:

  The servlet program is called by the web server. After the web server receives the client's servlet access request:

  ① The Web server first checks whether an instance object of the servlet has been loaded and created. If yes, execute step 4 directly, otherwise, execute step 2.

  ② Load and create an instance object of the Servlet. 

  ③ Call the init() method of the Servlet instance object.

  ④ Create an HttpServletRequest object that encapsulates the HTTP request message and an HttpServletResponse object that represents the HTTP response message, and then call the servlet's service() method and pass the request and response objects as parameters.

  ⑤ Before the WEB application is stopped or restarted, the servlet engine will unload the servlet and call the destroy() method of the servlet before unloading. 

2. Jsp working principle:

  When JSP pages are executed, they will be converted into Servlet (.java) by the JSP engine on the server side, and then the JSP engine will call the Java compiler to compile the Servelet (.java) into a Class file (. (JVM) interpreted execution. Verify this below: 

When accessing the Jsp page for the first time, two files will appear in the tomcat directory: jsp.java and jsp.class, which are Servlet and Class respectively.

3. Both features:

  Servlets can organize business logic code well, but generating dynamic HTML content by string concatenation in Java source files will result in difficult code maintenance and poor readability.

  Although JSP avoids the disadvantage of Servlet in generating HTML content, it is also inadvisable to mix a large number of complex business logic in HTML.

  Therefore, in a standard MVC architecture, Servlet as Controller accepts user requests and forwards them to the corresponding Action processing ( business logic includes database access, Javabeans, algorithm logic ), JSP as View is mainly used to generate dynamic pages, and each takes advantage.

  

Guess you like

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