The simplified path of the handwritten servlet to Spring MVC

Source: Internet
Author: User
Tags java web

Background

Servlets and JSPs are two basic techniques for developing Java Web applications, and spring MVC is a module for developing Web applications in the Spring framework. I believe everyone is just like me, starting with writing Servelt and JSPs, and then stepping closer to spring MVC. So what does spring MVC do to simplify the servlet and make spring MVC the preferred framework for Web application development? This article will discuss this.

About Servlet

What is a servlet

A servlet is a Java-based language that is used to create Web applications. Before the servlet, the CGI (Universal Gateway Interface) scripting language was popular as a server-side programming language, but the technology had many drawbacks:

CGI (Common Gateway Interface) CGI, even though it allows the server to invoke external programs and pass HTTP request information to external programs, a new process is started for each request.
Disadvantages of CGI technology

More response time when the number of clients increases
Each of its requests needs to start a new process that consumes a lot of system resources
Use platform-dependent languages such as: C, C + +, Perl
Servlet
Advantages of Servlets

Better performance: Each request creates a thread, not a process
Portability: Using the Java cross-platform language
More powerful: Servlet has JVM management, no need to worry about memory leaks, overflow, etc.
Friends who want to learn these techniques can add a group: 582505643. The group will share these technical knowledge points for everyone to learn free download
The evolution of the servlet

Basic directory for Servlets
Servlet interface in the most basic servlet class, you need to implement the Init (), Servic (), Destroy (), Getservletconfig (), and Geservletinfo () methods defined by the Servlet interface. Where the business logic is written in the service, the most common in the service method is the output of the content through the PrintWriter object.

The evolution of the 1:genericservlet abstract class implements the Servlet interface when all methods must be implemented, even if there is no code in the method. With the help of the Genericservlet abstract class, you only need to rewrite the service method.
Create the above directory structure under the Tomcat/webapps directory, all HTML, static files are stored directly in the application directory, all servlet classes are saved in the Web-inf/classe directory or subdirectory, The Web. XML (Deployment descriptor) file is saved in the Web-inf directory.
Evolution 2:httpservlet Abstract class HttpServlet covers the Genericservlet class, Encapsulates the ServletRequest and Servletresponse objects as HttpServletRequest and HttpServletResponse objects, respectively. HttpServlet implements the service method at the same time, when the request comes in, the Web container first calls the service method of HttpServlet, and calls the Doget or Dopost method according to the requested type, we only need to overwrite doget () and Gopost () method.
The accession of evolutionary 3:jsp

A friend who has written a servlet should know that the biggest disadvantage of a servlet is to write a lot of complicated HTML code in the Servlet class, so that the Java code and HTML code melted together, so JSP came into being.

JSP nature is also servelt, but it does not need to compile, JSP page is a. jsp extension of the text file. A simple JSP page is translated to (JSP name) _jsp servlet after the first request, Servelt can be seen after translation: _jspinit (), _jspdestory (), _jspservice () This approach is in fact consistent with the servlet.

Evolution 4:spring was born

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

Evolution 5:spring Web Module-Spring MVC

Friends who want to learn these techniques can add a group: 582505643. The group will share these technical knowledge points for everyone to learn free download

A friend of the servlet should know that when it comes to using the complex functionality of a servlet, you need to write multiple servlet classes and register with Web. XML, which can be very complex and costly to complete for complex applications. So spring provides a powerful Web development framework for spring MVC. Spring MVC is part of the spring product and offers all the benefits of spring loose coupling.

Spring MVC is a model-view-controller web framework built on the front-end controller servlet (Dispatcherservlet), which is responsible for sending each request to the appropriate handler and using the view to return the response result.

The schema of Spring MVC:

The Spring Web MVC Framework provides an MVC (model-View-Controller) architecture and components for developing flexible and loosely coupled web applications. The MVC pattern separates 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, usually consisting of the Pojo class
View: Responsible for rendering the model data, in general it generates a client browser to interpret the HTML output
Controller: Responsible for processing user requests and building the appropriate model and passing it to the view for rendering
The Dispatcherservlet Component Class Spring MVC framework is designed around Dispatcherservlet and handles all requests and responses. The workflow of Spring MVC Dispatcherservlet:

Dispatcherservlet Workflow for handling HTTP requests:

After accepting the HTTP request, Dispatcherservlet
The handlermapping is queried to invoke the appropriate controller (based on the requested URL)
The Controller accepts the request and invokes the appropriate service method based on the type of request, get/post the service method, sets the model data, and returns the view name to Dispatcherservlet
Dispatcherservlet get the corresponding view from Viewresolver based on the returned view name
Dispatcherservlet passes the model data to the final view and returns the view to the browser.
Summarize

At this point the servlet to Spring MVC evolution of the road to the curtain, can only sigh, technology change and iteration speed far beyond our expectations, we have to continue to learn, to keep up with the trend of the Times

The simplified path of the handwritten servlet to Spring MVC

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.