Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

The difference between a servlet and a jsp


May 15, 2021 Servlet



jsp as an extension of servlet technology, someone often confuses jsp with servlet. This article, will bring you the difference between servlet and jsp, I hope to help you.


The difference between servlet and jsp

1. Servlet can dynamically output HTML content through the HttpServletResponse object in Java code.

2, JSP is embedded in static HTML content Java code, and then Java code is dynamically executed to generate HTML content.


The respective characteristics of the servlet and jsp

1, Servlet although the business logic code can be well organized, but in the Java source file, because the string stitching way to generate dynamic HTML content, which can easily lead to code maintenance difficulties, poor readability.

2, JSP, although avoiding the disadvantages of servlet in generating HTML content, but in HTML mixed with a large number of complex business logic.


Combined with MVC double swords

JSPs and servlets have their own environments, so is there any way to make the most of their strengths? The answer is yes, and the MVC model solves this problem perfectly.

The MVC pattern, short for Model-View-Controller, is a software architecture pattern in software engineering that is divided into three basic parts: Model, View, and Controller:

Controller is responsible for forwarding and processing requests

View - Responsible for interface display

Model - Business function writing (e.g. algorithm implementation), database design, and data access operation implementation

In a software system developed by JSP/Servlet, the description of these three sections is as follows:


The difference between a servlet and a jsp


1, the web browser sends HTTP requests to the service side, and then gets by Controller (Servlet) and processes them (e.g. parameter resolution, request forwarding)

2. Controller (Servlet) calls the core business logic, the Model section, to get the results

3, Controller (Servlet) will logical processing results to View (JSP), dynamic output HTML content

4, dynamically generated HTML content returned to the browser display

The MVC pattern has a great advantage in Web development, it perfectly avoids the shortcomings of JSP and servlet, so that servlet is responsible for only the business logic part, not generating HTML code, and JSP will not be full of a lot of business code, which can greatly improve the readability and maintainability of the code.


Related reading:

Servlet tutorial

JSP tutorial