Java JSF、Servlet 和 JSP 之间有什么区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2095397/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 03:28:59  来源:igfitidea点击:

What is the difference between JSF, Servlet and JSP?

javajspjsfservletsjakarta-ee

提问by Cheung

I have some questions. These are :

我有一些问题。这些是 :

  1. How are JSP and Servlet related to each other?
  2. Is JSP some kind of Servlet?
  3. How are JSP and JSF related to each other?
  4. Is JSF some kind of Pre-Build UI based JSPlike ASP.NET-MVC?
  1. JSP 和 Servlet 之间有什么关系?
  2. JSP 是某种 Servlet 吗?
  3. JSP 和 JSF 之间有什么关系?
  4. JSF 是某种基于预构建 UI 的 JSP,如 ASP.NET-MVC?

采纳答案by BalusC

JSP (JavaServer Pages)

JSP(JavaServer 页面)

JSP is a Java view technologyrunning on the server machine which allows you to write template text in client side languages (like HTML, CSS, JavaScript, ect.). JSP supports taglibs, which are backed by pieces of Java code that let you control the page flow or output dynamically. A well-known taglib is JSTL. JSP also supports Expression Language, which can be used to access backend data (via attributes available in the page, request, session and application scopes), mostly in combination with taglibs.

JSP 是一种运行在服务器机器上的Java 视图技术,它允许您用客户端语言(如 HTML、CSS、JavaScript 等)编写模板文本。JSP 支持taglibs,它由 Java 代码片段支持,可让您动态控制页面流或输出。一个著名的标签库是JSTL。JSP 还支持Expression Language,它可用于访问后端数据(通过页面、请求、会话和应用程序范围中可用的属性),主要与标签库结合使用。

When a JSP is requested for the first time or when the web app starts up, the servlet container will compile it into a class extending HttpServletand use it during the web app's lifetime. You can find the generated source code in the server's work directory. In for example Tomcat, it's the /workdirectory. On a JSP request, the servlet container will execute the compiled JSP class and send the generated output (usually just HTML/CSS/JS) through the web server over a network to the client side, which in turn displays it in the web browser.

当第一次请求 JSP 或 Web 应用程序启动时,servlet 容器会将其编译为一个扩展类,HttpServlet并在 Web 应用程序的生命周期内使用它。您可以在服务器的工作目录中找到生成的源代码。例如在Tomcat 中,它是/work目录。对于 JSP 请求,servlet 容器将执行编译后的 JSP 类,并将生成的输出(通常只是 HTML/CSS/JS)通过网络服务器通过网络发送到客户端,然后客户端将其显示在 Web 浏览器中。

Servlets

小服务程序

Servlet is a Java application programming interface (API)running on the server machine, which intercepts requests made by the client and generates/sends a response. A well-known example is the HttpServletwhich provides methods to hook on HTTPrequests using the popular HTTP methodssuch as GETand POST. You can configure HttpServlets to listen to a certain HTTP URL pattern, which is configurable in web.xml, or more recently with Java EE 6, with @WebServletannotation.

Servlet 是运行在服务器机器上的Java 应用程序编程接口 (API),它拦截客户端发出的请求并生成/发送响应。一个众所周知的例子是 ,HttpServlet它提供了使用流行的HTTP 方法(如和 )挂钩HTTP请求的方法。您可以将s配置为侦听某个 HTTP URL 模式,该模式可在 中配置,或者最近使用Java EE 6 进行配置,并带有注释。GETPOSTHttpServletweb.xml@WebServlet

When a Servlet is first requested or during web app startup, the servlet container will create an instance of it and keep it in memory during the web app's lifetime. The same instance will be reused for every incoming request whose URL matches the servlet's URL pattern. You can access the request data by HttpServletRequestand handle the response by HttpServletResponse. Both objects are available as method arguments inside any of the overridden methods of HttpServlet, such as doGet()and doPost().

当首次请求 Servlet 或在 Web 应用程序启动期间,servlet 容器将创建它的一个实例,并在 Web 应用程序的生命周期内将其保存在内存中。对于 URL 与 servlet 的 URL 模式匹配的每个传入请求,将重用相同的实例。您可以通过 访问请求数据HttpServletRequest并通过处理响应HttpServletResponse。这两个对象都可用作 的任何重写方法中的方法参数HttpServlet,例如doGet()doPost()

JSF (JavaServer Faces)

JSF(JavaServer Faces)

JSF is a component based MVC frameworkwhich is built on top of the Servlet API and provides componentsvia taglibs which can be used in JSP or any other Java based view technology such as Facelets. Facelets is much more suited to JSF than JSP. It namely provides great templating capabilitiessuch as composite components, while JSP basically only offers the <jsp:include>for templating in JSF, so that you're forced to create custom components with raw Java code (which is a bit opaque and a lot of tedious work) when you want to replace a repeated group of components with a single component. Since JSF 2.0, JSP has been deprecated as view technology in favor of Facelets.

JSF 是一个基于组件的 MVC 框架,它构建在 Servlet API 之上,并通过 taglibs提供组件,这些组件可以在 JSP 或任何其他基于 Java 的视图技术(如Facelets)中使用。Facelets 比 JSP 更适合 JSF。它提供了强大的模板功能,例如复合组件,而 JSP 基本上只提供了<jsp:include>JSF 中的模板,因此当您使用原始 Java 代码(这有点不透明且工作繁琐)时,您被迫创建自定义组件。您想用单个组件替换一组重复的组件。从 JSF 2.0 开始,JSP 作为视图技术已被弃用,取而代之的是 Facelets。

Note: JSP itself is NOT deprecated, just the combination of JSF with JSP is deprecated.

注意:JSP 本身没有被弃用,只是 JSF 与 JSP 的组合被弃用。

Note: JSP has great templating abilities by means of Taglibs, especially the (Tag File) variant. JSP templating in combination with JSF is what is lacking.

注意:JSP 通过 Taglibs 具有强大的模板能力,尤其是 ( Tag File) 变体。缺少 JSP 模板与 JSF 的结合。

As being a MVC (Model-View-Controller) framework, JSF provides the FacesServletas the sole request-response Controller. It takes all the standard and tedious HTTP request/response work from your hands, such as gathering user input, validating/converting them, putting them in model objects, invoking actions and rendering the response. This way you end up with basically a JSP or Facelets (XHTML) page for Viewand a JavaBean class as Model. The JSF components are used to bind the view with the model (such as your ASP.NET web control does) and the FacesServletuses the JSF component treeto do all the work.

作为一个 MVC(模型-视图-控制器)框架,JSF 提供了FacesServlet作为唯一的请求-响应控制器。它从您手中完成所有标准和繁琐的 HTTP 请求/响应工作,例如收集用户输入、验证/转换它们、将它们放入模型对象、调用操作和呈现响应。通过这种方式,您最终会得到一个用于View 的基本 JSP 或 Facelets (XHTML) 页面和一个作为Model的 JavaBean 类。JSF 组件用于将视图与模型绑定(例如您的 ASP.NET Web 控件所做的),并且FacesServlet使用JSF 组件树来完成所有工作。

Related questions

相关问题

回答by S.Lott

See http://www.oracle.com/technetwork/java/faq-137059.html

参见http://www.oracle.com/technetwork/java/faq-137059.html

JSP technology is part of the Java technology family. JSP pages are compiled into servlets and may call JavaBeans components (beans) or Enterprise JavaBeans components (enterprise beans) to perform processing on the server. As such, JSP technology is a key component in a highly scalable architecture for web-based applications.

JSP 技术是 Java 技术家族的一部分。JSP页面被编译成servlets,可以调用JavaBeans组件(beans)或企业JavaBeans组件(企业beans)在服务器上进行处理。因此,JSP 技术是基于 Web 的应用程序的高度可扩展架构中的关键组件。

See https://jcp.org/en/introduction/faq

https://jcp.org/en/introduction/faq

A: JavaServer Faces technology is a framework for building user interfaces for web applications. JavaServer Faces technology includes:

A set of APIs for: representing UI components and managing their state, handling events and input validation, defining page navigation, and supporting internationalization and accessibility.

A JavaServer Pages (JSP) custom tag library for expressing a JavaServer Faces interface within a JSP page.

答:JavaServer Faces 技术是一种为 Web 应用程序构建用户界面的框架。JavaServer Faces 技术包括:

一组 API,用于:表示 UI 组件和管理它们的状态、处理事件和输入验证、定义页面导航以及支持国际化和可访问性。

JavaServer Pages (JSP) 自定义标记库,用于在 JSP 页面中表达 JavaServer Faces 接口。

JSP is a specialized kind of servlet.

JSP 是一种特殊的 servlet。

JSF is a set of tags you can use with JSP.

JSF 是一组可以与 JSP 一起使用的标记。

回答by mahesh

that is true that JSP is converted into servlet at the time of execution, and JSF is totally new thing in order to make the webpage more readable as JSF allows to write all the programming structures in the form of tag.

的确,JSP在执行的时候就被转换成servlet,而JSF是一个全新的东西,为了让网页更具可读性,JSF允许以标签的形式编写所有的编程结构。

回答by cackle

Servlet - it's java server side layer.

Servlet - 它是 Java 服务器端层。

  • JSP - it's Servlet with html
  • JSF - it's components base on tag libs
  • JSP - it's converted into servlet once when server got request.
  • JSP - 它是带有 html 的 Servlet
  • JSF - 它是基于标签库的组件
  • JSP - 当服务器收到请求时,它会转换为一次 servlet。

回答by Vh24

From Browser/Client perspective

从浏览器/客户端角度

JSP and JSF both looks same, As Per Application Requirements goes, JSP is more suited for request - response based applications.

JSP 和 JSF 看起来都一样,根据应用程序需求,JSP 更适合基于请求 - 响应的应用程序。

JSF is targetted for richer event based Web applications. I see event as much more granular than request/response.

JSF 面向更丰富的基于事件的 Web 应用程序。我认为事件比请求/响应更精细。

From Server Perspective

从服务器角度

JSP page is converted to servlet, and it has only minimal behaviour.

JSP 页面被转换为 servlet,它只有最少的行为。

JSF page is converted to components tree(by specialized FacesServlet) and it follows component lifecycle defined by spec.

JSF 页面被转换为组件树(通过专门的 FacesServlet)并遵循规范定义的组件生命周期。

回答by Karthi

There are also situations where you can favor JSP over JSF. The application nature should be the deciding factor to choose the technology.

在某些情况下,您可以更喜欢 JSP 而不是 JSF。应用性质应该是选择技术的决定因素。

If you have a rich GUI interaction and lot of Java scripting needed then favor JSF. Basically if your GUI app architecture is like Component oriented & even driven like Swing then JSF is the best.

如果您有丰富的 GUI 交互并且需要大量的 Java 脚本,那么请选择 JSF。基本上,如果您的 GUI 应用程序架构像面向组件甚至像 Swing 一样驱动,那么 JSF 是最好的。

If the application is just a plain form submitting, not much of GUI interaction needed, then JSP could do well if learning a new tech is an overhead and also complex framework is unnecessary.

如果应用程序只是一个简单的表单提交,不需要太多的 GUI 交互,那么如果学习新技术是一种开销并且不需要复杂的框架,那么 JSP 可以做得很好。

回答by Kishor Prakash

Servlets :

The Java Servlet API enables Java developers to write server-side code for delivering dynamic Web content. Like other proprietary Web server APIs, the Java Servlet API offered improved performance over CGI; however, it has some key additional advantages. Because servlets were coded in Java, they provides an object-oriented (OO) design approach and, more important, are able to run on any platform. Thus, the same code was portable to any host that supported Java. Servlets greatly contributed to the popularity of Java, as it became a widely used technology for server-side Web application development.

JSP :

JSP is built on top of servlets and provides a simpler, page-based solution to generating large amounts of dynamic HTML content for Web user interfaces. JavaServer Pages enables Web developers and designers to simply edit HTML pages with special tags for the dynamic, Java portions. JavaServer Pages works by having a special servlet known as a JSP container, which is installed on a Web server and handles all JSP page view requests. The JSP container translates a requested JSP into servlet code that is then compiled and immediately executed. Subsequent requests to the same page simply invoke the runtime servlet for the page. If a change is made to the JSP on the server, a request to view it triggers another translation, compilation, and restart of the runtime servlet.

JSF :

JavaServer Faces is a standard Java framework for building user interfaces for Web applications. Most important, it simplifies the development of the user interface, which is often one of the more difficult and tedious parts of Web application development.
Although it is possible to build user interfaces by using foundational Java Web technologies(such as Java servlets and JavaServer Pages) without a comprehensive framework designedfor enterprise Web application development, these core technologies can often lead to avariety of development and maintenance problems. More important, by the time the developers achieve a production-quality solution, the same set of problems solved by JSF will have been solved in a nonstandard manner. JavaServer Faces is designed to simplify the development of user interfaces for Java Web applications in the following ways:
? It provides a component-centric, client-independent development approach to building Web user interfaces, thus improving developer productivity and ease of use.
? It simplifies the access and management of application data from the Web user interface.
? It automatically manages the user interface state between multiple requests and multiple clients in a simple and unobtrusive manner.
? It supplies a development framework that is friendly to a diverse developer audience with different skill sets.
? It describes a standard set of architectural patterns for a web application.

小服务程序:

Java Servlet API 使 Java 开发人员能够编写服务器端代码以提供动态 Web 内容。与其他专有 Web 服务器 API 一样,Java Servlet API 提供了比 CGI 更高的性能;然而,它还有一些关键的额外优势。因为 servlet 是用 Java 编码的,所以它们提供了一种面向对象 (OO) 的设计方法,更重要的是,它们能够在任何平台上运行。因此,相同的代码可以移植到任何支持 Java 的主机上。Servlet 极大地促进了 Java 的普及,因为它成为服务器端 Web 应用程序开发中广泛使用的技术。

JSP :

JSP 构建在 servlet 之上,并提供了一个更简单的、基于页面的解决方案来为 Web 用户界面生成大量动态 HTML 内容。JavaServer Pages 使 Web 开发人员和设计人员能够简单地编辑带有动态 Java 部分的特殊标记的 HTML 页面。JavaServer Pages 通过一个称为 JSP 容器的特殊 servlet 工作,该容器安装在 Web 服务器上并处理所有 JSP 页面查看请求。JSP 容器将请求的 JSP 转换为 servlet 代码,然后编译并立即执行。对同一页面的后续请求只需调用该页面的运行时 servlet。如果对服务器上的 JSP 进行了更改,则查看它的请求会触发运行时 servlet 的另一个转换、编译和重新启动。

JSF :

JavaServer Faces 是一个标准的 Java 框架,用于为 Web 应用程序构建用户界面。最重要的是,它简化了用户界面的开发,这通常是 Web 应用程序开发中比较困难和乏味的部分之一。
尽管可以使用基础 Java Web 技术(如 Java servlet 和 JavaServer Pages)构建用户界面,而无需为企业 Web 应用程序开发设计的综合框架,但这些核心技术通常会导致各种开发和维护问题。更重要的是,当开发人员获得生产质量的解决方案时,JSF 解决的同一组问题将以非标准方式解决。JavaServer Faces 旨在通过以下方式简化 Java Web 应用程序用户界面的开发:
? 它提供了一种以组件为中心、独立于客户端的开发方法来构建 Web 用户界面,从而提高开发人员的生产力和易用性。
? 它简化了从 Web 用户界面访问和管理应用程序数据的过程。
? 它以一种简单而不显眼的方式自动管理多个请求和多个客户端之间的用户界面状态。
? 它提供了一个开发框架,该框架对具有不同技能集的不同开发人员受众友好。
? 它描述了 Web 应用程序的一组标准架构模式。

[ Source : Complete reference:JSF]

[来源:完整参考:JSF]

回答by user3608245

The basic difference between Servlets and JSP is that in Servlets we write java code and in that we embed HTML code and there is just reverse case with JSP . In JSP we write HTML code and in that we embed java code using tags provided by JSP.

Servlets 和 JSP 之间的基本区别在于,在 Servlets 中我们编写 java 代码并嵌入 HTML 代码,而 JSP 则正好相反。在 JSP 中,我们编写 HTML 代码,并使用 JSP 提供的标签嵌入 Java 代码。

回答by Mahender Reddy Yasa

Jsp is also having in built servlet code which don't need any external compilation it can be run directly run. Changes will take effect in jsp directly in a browser.

Jsp 也有内置的 servlet 代码,不需要任何外部编译,它可以直接运行。更改将直接在浏览器中的jsp 中生效。

Servlet need to be compiled (i.e it will have specific class creation)

Servlet 需要编译(即会创建特定的类)

Jsf is a view component of MVC Framework

Jsf 是 MVC 框架的一个视图组件

回答by Roopam

JSP stands for JAVA SERVER PAGE........ jsp is not a servlet. Jsp uses code and HTML tag both in itself you dont need to make a HTML and a servlet seprately.Jsp are playing magnificent role in web application. Servlet is a java class plays an role to make your HTML page from static to dynamic .

JSP 代表 JAVA SERVER PAGE.........jsp 不是 servlet。Jsp 本身使用代码和 HTML 标签,您不需要单独制作 HTML 和 servlet。Jsp 在 Web 应用程序中扮演着重要的角色。Servlet 是一个 java 类,它的作用是使您的 HTML 页面从静态变为动态。