background

Servlet and JSP are two basic technologies for developing Java Web applications. Spring MVC is a module of Spring framework for developing Web applications. Like me, you started with Servlets and JSPS and worked your way up to Spring MVC. This article will explore what Spring MVC does to simplify servlets and make Spring MVC the preferred framework for Web application development.

Introduction of the Servlet

What is the Servlet

Servlets are a Java-based language for creating Web applications. Before servlets, the CGI (Common Gateway Interface) scripting language was popular as a server-side programming language, but the technology had many disadvantages:

  • CGI (Common Gateway Interface) CGI Even though the server can call an external program and pass HTTP request information to the external program for processing, a new process is started for each request.

  • Disadvantages of CGI technology

  1. As the number of clients increases, the response time increases
  2. For each request, a new process needs to be started, consuming a lot of system resources
  3. Use platform-dependent languages such as C, C++, and Perl
  • Servlet

  • The advantages of the Servlet

  1. Better performance: Threads, not processes, are created per request
  2. Portability: Use the Java cross-platform language
  3. More powerful: Servlets are managed by the JVM and don’t need to worry about memory leaks, spills, etc

The evolution of the Servlet

  • The basic directory for servlets

    Create the above directory structure in the Tomcat /webapps directory. All HTML, static files are stored directly in the application directory, all Servlet classes are stored in the Web-INF /classe directory or subdirectory, and web.xml (deployment descriptor) files are stored in the Web-INF directory.

  • Servlet interface In the most basic Servlet classes, you implement the init(), servic(), destroy(), getServletConfig(), and geServletInfo() methods defined by the Servlet interface. The business logic is written in the Service, and the most common service method is the PrintWriter object.

  • Evolution 1: The GenericServlet abstract class must implement all methods when implementing the Servlet interface, even if there is no code in the method. With the help of the GenericServlet abstract class, you just need to override the Service method.

  • Evolution of 2: The abstract HttpServlet class overrides GenericServlet, Encapsulate ServletRequest and ServletResponse objects as HttpServletRequest and HttpServletResponse objects, respectively. Httpservlets also implement a service method. When a request comes in, the Web container first calls the Service method of httpServlets and then calls the doGet or doPost methods, depending on the type of request.

Evolution 3: The addition of JSP

Those who have written servlets should know that the biggest drawback of servlets is to write a lot of complicated HTML code in the Servlet class, making Java code and HTML code mixed together, so JSP came into being. A JSP is essentially a servlet, but it does not need to be compiled. A JSP page is a text file with a.jsp extension. A simple JSP page is translated into a (JSP name) _jsp servlet after the first request, and the translated servvelt can see that methods like _jspInit(), _jspDestory(), and _jspService() correspond to servlets.

Evolution 4: Spring comes out of nowhere

Spring provides powerful inversion of control (IOC) and dependency injection (DI) capabilities to decouple project components.

Evolution 5: Spring Web module – Spring MVC

Those of you who have learned servlets should know that when you want to use the complex functions that servlets do, you need to write multiple Servlet classes and register them in web.xml, which can be very complicated and expensive to write code for complex Web applications. So Spring provides a powerful Web development framework, Spring MVC. Spring MVC is part of the Spring product and enjoys all the benefits of Spring, such as loose coupling. Spring MVC is a model-view-controller Web framework built on a front-end controller servlet (DispatcherServlet), which is responsible for sending each request to the appropriate handler, using the view to return the response result. Spring MVC architecture:

The Spring Web MVC framework provides the MVC(Model-View-Controller) architecture and components for developing flexible and loosely coupled Web applications. The MVC pattern leads to separation of different aspects of the application (input logic, business logic, and UI logic) while providing loose coupling between these elements

  • Model: Encapsulates the application’s data, typically consisting of POJO classes
  • View: Is responsible for rendering model data and generally generates HTML output that the client browser can interpret
  • Controller: Handles user requests, builds the appropriate model, and passes it to the view for rendering

The Spring MVC framework is designed around DispatcherServlet, which handles all requests and responses. Spring MVC DispatcherServlet workflow:

The workflow of DispatcherServlet handling HTTP requests:

  1. Upon receiving the HTTP request, the DispatcherServlet
  2. HandlerMapping is queried to invoke the corresponding Controller (based on the requested URL)
  3. The Controller receives the request and invokes the corresponding service method based on the type of request Get/Post. The service method performs the corresponding business processing and sets the model data. Finally, the view name is returned to the DispatcherServlet
  4. The DispatcherServlet retrieves the corresponding view from the ViewResolver based on the returned view name
  5. The DispatcherServlet passes the model data to the final view and returns the view to the browser.

conclusion

At this point, the evolution of Servlet to Spring MVC has come to an end. We can only sigh that the speed of technology change and iteration is far faster than we expected. We have to keep learning to keep up with the trend of The Times.

References: Reference 1 Reference 2