Difference Between JSF, Servlet, and JSP – JSF、Servlet和JSP之间的区别

最后修改: 2021年 1月 6日

中文/混合/英文(键盘快捷键:t)

1. Introduction

1.绪论

When developing any application, the selection of the right technology plays a significant role. However, the decision isn’t always straightforward.

在开发任何应用程序时,选择正确的技术起着重要作用。然而,这个决定并不总是直截了当的。

In this article, we’ll provide a comparative view of three popular technologies of Java. Before jumping into comparison, we’ll start by exploring the purpose of each technology and its lifecycle. Then, we’ll see what their prominent features are and compare them on the basis of several features.

在这篇文章中,我们将对Java的三种流行技术进行比较。在进入比较之前,我们将首先探讨每种技术的目的和它的生命周期。然后,我们将看到它们的突出特点是什么,并在几个特点的基础上对它们进行比较。

2. JSF

2.JSF

Jakarta Server Faces, formerly known as JavaServer Faces, is a web framework to build component-based user interfaces for Java applications. Like many others, it also follows an MVC approach. The “View” of MVC simplifies the creation of user interfaces with the help of reusable UI components.

Jakarta Server Faces,以前被称为JavaServer Faces,是一个用于为Java应用程序构建基于组件的用户界面的Web框架。与其他许多框架一样,它也遵循MVC方法。MVC的 “视图 “在可重复使用的UI组件的帮助下,简化了用户界面的创建。

JSF has a wide range of standard UI components and also provides the flexibility to define a new one through an external API.

JSF拥有广泛的标准UI组件,同时也提供了通过外部API定义新组件的灵活性。

The lifecycle of any application refers to various stages from its initiation to conclusion. Similarly, the lifecycle of the JSF application starts when a client makes an HTTP request and ends when the server responds with a response. The JSF lifecycle is a request-response lifecycle and handles two kinds of requests: initial requests and postback.

任何应用程序的生命周期都是指从开始到结束的各个阶段。同样,JSF应用程序的生命周期从客户端发出HTTP请求时开始,到服务器响应时结束。JSF的生命周期是一个请求-响应的生命周期,处理两种请求:初始请求和回传。

The lifecycle of a JSF application consists of two major phases: execute and render.

JSF应用程序的生命周期由两个主要阶段组成。执行渲染

The execute phase is further divided into six phases:

执行阶段进一步划分为六个阶段。

  • Restore View: Starts when the JSF receives a request
  • Apply Request Values: Restoration of the component tree during a postback request
  • Process Validations: Process all the validators registered on the component tree
  • Update Model Values: Traverses the component tree and sets the corresponding server-side object properties
  • Invoke Application: Handles application-level events such as submitting a form
  • Render Response: Builds the view and renders pages

In the render phase, the system renders the requested resource as a response to the client browser.

render阶段,系统将所请求的资源作为响应渲染给客户端浏览器。

JSF 2.0 was a major release that included Facelets, composite components, AJAX, and resource libraries.

JSF 2.0是一个重要的版本,包括Facelets、复合组件、AJAX和资源库。

Before Facelets, JSP was the default templating engine for JSF applications. With older releases of JSF 2.x, many new features were introduced to make the framework more robust and efficient. These features include support for annotations, HTML5, Restful, and stateless JSF, among others.

在Facelets之前,JSP是JSF应用程序的默认模板引擎。随着JSF 2.x旧版本的发布,许多新的功能被引入,以使该框架更加健壮和高效。这些特性包括对注解、HTML5、Restful和无状态JSF的支持,等等。

3. Servlet

3.Servlet

Jakarta Servlets, formerly known as Java Servlets, extend the capability of a server. Usually, servlets interact with web clients using a request/response mechanism implemented by a container.

Jakarta Servlets,以前被称为Java Servlets,扩展了服务器的能力。通常情况下,Servlet使用由容器实现的请求/响应机制与Web客户端进行交互。

A servlet container is an important part of a web server. It manages servlets and creates dynamic content according to user requests. Whenever a web server receives a request, it directs the request to a registered servlet.

servlet容器是Web服务器的一个重要部分。它管理Servlet,并根据用户的请求创建动态内容。每当Web服务器收到一个请求时,它就会将该请求指向注册的小程序

The lifecycle consists of only three phases. First, the init() method is invoked to initiate the servlet. Then, the container sends incoming requests to the service() method that performs all the tasks. Lastly, the destroy() method cleans up a few things and tears down the servlet.

生命周期只包括三个阶段。首先,调用init()方法来启动servlet然后,容器将传入的请求发送到执行所有任务的service()方法。最后,destroy()方法清理了一些东西并关闭了Servlet。

Servlets have many important features, including native support for Java and its libraries, a standard API for web servers, and the powers of HTTP/2. Additionally, they allow asynchronous requests and create separate threads for each request.

Servlets有许多重要的功能,包括对Java及其库的本地支持,对Web服务器的标准API,以及HTTP/2的力量。此外,它们允许异步请求并为每个请求创建单独的线程。

4. JSP

4.JSP

Jakarta Server Pages, formerly known as JavaServer Pages, enable us to inject dynamic content into a static page. JSPs are the high-level abstraction of servlets because they are converted into servlets before execution begins.

雅加达服务器页面,以前被称为JavaServer Pages,使我们能够将动态内容注入静态页面中。JSP是servlets的高级抽象,因为它们在开始执行之前就被转换为servlets。

The common tasks such as variable declaration and printing values, looping, conditional formatting, and exception handling are performed through the JSTL library.

变量声明和打印值、循环、条件格式化和异常处理等常见任务是通过JSTL库进行的。

The lifecycle of a JSP is similar to the servlet with one additional step — the compilation step. When a browser asks for a page, the JSP engine first checks whether it needs to compile the page or not. The compilation step consists of three phases.

JSP的生命周期与servlet相似,但多了一个步骤–编译步骤。当浏览器请求一个页面时,JSP引擎首先检查它是否需要编译该页面。编译步骤包括三个阶段。

Initially, the engine parses the page. Then, it converts the page into a servlet. Lastly, the generated servlet compiles into a Java class.

最初,该引擎解析页面。然后,它将页面转换为一个servlet。最后,生成的servlet被编译成一个Java类。

JSPs have many notable features such as tracking the session, good form controls and sending/receiving data to/from the server. Because the JSP is built on top of the servlet, it has access to all important Java APIs such as JDBC, JNDI, and EJB.

JSP有许多值得注意的功能,如跟踪会话、良好的表单控制以及向/从服务器发送/接收数据。由于JSP是建立在servlet之上的,它可以访问所有重要的Java API,如JDBC、JNDI和EJB。

5. Key Differences

5.关键差异

Servlet technology is the foundation of web application development in J2EE. However, it doesn’t come with a view technology, and the developer has to mix markup tags in with Java code. Additionally, it lacks the utilities for common tasks like building the markup, validating the requests, and enabling the security features.

Servlet技术是J2EE中Web应用开发的基础。然而,它并不带有视图技术,开发者必须将标记标签与Java代码混合在一起。此外,它还缺少一些常见任务的工具,如建立标记、验证请求和启用安全功能。

JSPs fill the markup gap for the servlet. With the help of JSTL and EL, we can define any custom HTML tag to build a good UI. Unfortunately, JSPs are slow to compile, hard to debug, leave basic form validation and type conversion to the developer, and lack support for security.

JSP填补了servlet的标记空白。在JSTL和EL的帮助下,我们可以定义任何自定义的HTML标签来建立一个良好的用户界面。不幸的是,JSP的编译速度很慢,难以调试,将基本的表单验证和类型转换留给了开发人员,而且缺乏对安全的支持。

JSF is a proper framework that connects a data source with a reuseable UI component, provides support for multiple libraries, and decreases the effort to build and manage applications. Being component-based, JSF always has a good security advantage over JSP. Despite all of its benefits, JSF is complex and has a steep learning curve.

JSF是一个合适的框架,它将数据源与可重用的UI组件连接起来,提供对多个库的支持,并减少了构建和管理应用程序的工作量。作为基于组件的,JSF总是比JSP有很好的安全优势。尽管有这么多好处,JSF还是很复杂,有一个陡峭的学习曲线。

In light of the MVC design pattern, the servlet acts as a controller and JSP as a view, whereas JSF is a complete MVC.

从MVC设计模式来看,servlet作为控制器,JSP作为视图,而JSF是一个完整的MVC。

As we already know, the servlet will need manual HTML tags in Java code. For the same purpose, JSP uses HTML and JSF uses Facelets. Additionally, both provide support for custom tags, too.

我们已经知道,servlet将需要在Java代码中手动设置HTML标签。出于同样的目的,JSP使用HTML,JSF使用Facelets。此外,两者也都提供对自定义标签的支持。

There’s no default support for error handling in servlet and JSP. In contrast, JSF provides a bunch of predefined validators.

在servlet和JSP中没有对错误处理的默认支持。相比之下,JSF提供了一堆预定义的验证器。

Security has always been a concern in applications that transmit data over the web. JSPs that support only role-based and form-based authentication are lacking in this aspect.

在通过网络传输数据的应用程序中,安全问题一直是一个关注点。只支持基于角色和基于表单的认证的JSP在这方面是缺乏的。

Speaking about the protocols, JSP only accepts HTTP, whereas servlet and JSF support several protocols, including HTTP/HTTPS, SMTP, and SIP. All of these technologies advocate multithreading and necessitate a web container to run.

说到协议,JSP只接受HTTP,而servlet和JSF支持多种协议,包括HTTP/HTTPS、SMTP和SIP。所有这些技术都提倡多线程,并需要一个Web容器来运行。

6. Conclusion

6.结语

In this tutorial, we compared three popular technologies in the Java world: JSF, Servlet, and JSP. First, we saw what each technology represents and how its lifecycle progress. Then, we discussed the main features and limitations of each technology. Finally, we compared them on the basis of several features.

在本教程中,我们比较了Java世界中三种流行的技术。JSF、Servlet和JSP。首先,我们看到了每种技术代表什么,以及它的生命周期是如何进展的。然后,我们讨论了每种技术的主要特点和限制。最后,我们在几个特点的基础上对它们进行了比较。

What technology should be chosen over the other totally depends on the context. The nature of the application should be the deciding factor.

应该选择哪种技术而不是其他技术,完全取决于环境。应用的性质应该是决定性的因素。