简体   繁体   English

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

[英]What is the difference between JSF, Servlet and JSP?

I have some questions.我有一些问题。 These are :这些是 :

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

JSP (JavaServer Pages) JSP(JavaServer 页面)

JSP is a Java view technology running on the server machine which allows you to write template text in client side languages (like HTML, CSS, JavaScript, ect.). JSP 是一种运行在服务器机器上的Java 视图技术,它允许您用客户端语言(如 HTML、CSS、JavaScript 等)编写模板文本。 JSP supports taglibs , which are backed by pieces of Java code that let you control the page flow or output dynamically. JSP 支持taglibs ,它由 Java 代码片段支持,可让您动态控制页面流或输出。 A well-known taglib is JSTL .一个著名的标签库是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 还支持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 HttpServlet and use it during the web app's lifetime.当第一次请求 JSP 或 Web 应用程序启动时,servlet 容器会将其编译为扩展HttpServlet的类,并在 Web 应用程序的生命周期内使用它。 You can find the generated source code in the server's work directory.您可以在服务器的工作目录中找到生成的源代码。 In for example Tomcat , it's the /work directory.例如在Tomcat 中,它是/work目录。 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 请求,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. Servlet 是运行在服务器机器上的Java 应用程序编程接口 (API) ,它拦截客户端发出的请求并生成/发送响应。 A well-known example is the HttpServlet which provides methods to hook on HTTP requests using the popular HTTP methods such as GET and POST .一个众所周知的例子是HttpServlet ,它提供了使用流行的HTTP 方法(如GETPOST挂钩HTTP请求的方法 You can configure HttpServlet s to listen to a certain HTTP URL pattern, which is configurable in web.xml , or more recently with Java EE 6 , with @WebServlet annotation.您可以将HttpServlet配置为侦听某个 HTTP URL 模式,该模式可在web.xml配置,或者最近使用Java EE 6 进行配置,并带有@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.当首次请求 Servlet 或在 Web 应用程序启动期间,servlet 容器将创建它的一个实例,并在 Web 应用程序的生命周期内将其保存在内存中。 The same instance will be reused for every incoming request whose URL matches the servlet's URL pattern.对于 URL 与 servlet 的 URL 模式匹配的每个传入请求,将重用相同的实例。 You can access the request data by HttpServletRequest and handle the response by HttpServletResponse .您可以通过访问请求数据HttpServletRequest和处理由响应HttpServletResponse Both objects are available as method arguments inside any of the overridden methods of HttpServlet , such as doGet() and doPost() .这两个对象都可用作HttpServlet任何重写方法中的方法参数,例如doGet()doPost()

JSF (JavaServer Faces) JSF(JavaServer Faces)

JSF is a component based MVC framework which is built on top of the Servlet API and provides components via taglibs which can be used in JSP or any other Java based view technology such as Facelets . JSF 是一个基于组件的 MVC 框架,它构建在 Servlet API 之上,并通过 taglibs 提供组件,这些组件可用于 JSP 或任何其他基于 Java 的视图技术,例如Facelets Facelets is much more suited to JSF than JSP. Facelets 比 JSP 更适合 JSF。 It namely provides great templating capabilities such 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.它即提供了强大的模板功能,例如复合组件,而 JSP 基本上只提供<jsp:include>用于在 JSF 中进行模板化,因此您被迫使用原始 Java 代码创建自定义组件(这有点不透明而且很多乏味的工作)当您想用单个组件替换一组重复的组件时。 Since JSF 2.0, JSP has been deprecated as view technology in favor of Facelets.从 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 通过 Taglibs 具有强大的模板化能力,尤其是 ( Tag File ) 变体。 JSP templating in combination with JSF is what is lacking.缺少 JSP 模板与 JSF 的结合。

As being a MVC ( Model-View-Controller ) framework, JSF provides the FacesServlet as the sole request-response Controller .作为 MVC( 模型-视图-控制器)框架,JSF 提供FacesServlet作为唯一的请求-响应控制器 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.它从您手中完成所有标准和繁琐的 HTTP 请求/响应工作,例如收集用户输入、验证/转换它们、将它们放入模型对象、调用操作和呈现响应。 This way you end up with basically a JSP or Facelets (XHTML) page for View and a JavaBean class as Model .通过这种方式,您最终会得到一个用于View的 JSP 或 Facelets (XHTML) 页面和一个作为Model的 JavaBean 类。 The JSF components are used to bind the view with the model (such as your ASP.NET web control does) and the FacesServlet uses the JSF component tree to do all the work. JSF 组件用于将视图与模型绑定(例如您的 ASP.NET Web 控件所做的),而FacesServlet使用JSF 组件树来完成所有工作。

Related questions相关问题

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 技术是 Java 技术家族的一部分。 JSP pages are compiled into servlets and may call JavaBeans components (beans) or Enterprise JavaBeans components (enterprise beans) to perform processing on the server. JSP页面被编译成servlets,可以调用JavaBeans组件(beans)或企业JavaBeans组件(企业beans)在服务器上进行处理。 As such, JSP technology is a key component in a highly scalable architecture for web-based applications.因此,JSP 技术是基于 Web 的应用程序的高度可扩展架构中的关键组件。

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

A: JavaServer Faces technology is a framework for building user interfaces for web applications.答:JavaServer Faces 技术是一种为 Web 应用程序构建用户界面的框架。 JavaServer Faces technology includes: JavaServer Faces 技术包括:

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

A JavaServer Pages (JSP) custom tag library for expressing a JavaServer Faces interface within a JSP page. 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 一起使用的标记。

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. JSF 面向更丰富的基于事件的 Web 应用程序。 I see event as much more granular than request/response.我认为事件比请求/响应更精细。

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)并遵循规范定义的组件生命周期。

Servlets :小服务程序:

The Java Servlet API enables Java developers to write server-side code for delivering dynamic Web content. Java Servlet API 使 Java 开发人员能够编写服务器端代码以提供动态 Web 内容。 Like other proprietary Web server APIs, the Java Servlet API offered improved performance over CGI;与其他专有 Web 服务器 API 一样,Java Servlet API 提供了比 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.因为 servlet 是用 Java 编码的,所以它们提供了面向对象 (OO) 的设计方法,更重要的是,它们能够在任何平台上运行。 Thus, the same code was portable to any host that supported Java.因此,相同的代码可以移植到任何支持 Java 的主机上。 Servlets greatly contributed to the popularity of Java, as it became a widely used technology for server-side Web application development. Servlet 极大地促进了 Java 的普及,因为它成为服务器端 Web 应用程序开发中广泛使用的技术。

JSP : 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. JSP 构建在 servlet 之上,提供了一种更简单的、基于页面的解决方案来为 Web 用户界面生成大量动态 HTML 内容。 JavaServer Pages enables Web developers and designers to simply edit HTML pages with special tags for the dynamic, Java portions. JavaServer Pages 使 Web 开发人员和设计人员能够简单地编辑带有动态 Java 部分特殊标记的 HTML 页面。 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. JavaServer Pages 通过一个称为 JSP 容器的特殊 servlet 工作,该容器安装在 Web 服务器上并处理所有 JSP 页面查看请求。 The JSP container translates a requested JSP into servlet code that is then compiled and immediately executed. JSP 容器将请求的 JSP 转换为 servlet 代码,然后编译并立即执行。 Subsequent requests to the same page simply invoke the runtime servlet for the page.对同一页面的后续请求只需调用该页面的运行时 servlet。 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.如果对服务器上的 JSP 进行了更改,则查看它的请求会触发运行时 servlet 的另一个转换、编译和重新启动。

JSF : JSF :

JavaServer Faces is a standard Java framework for building user interfaces for Web applications. JavaServer Faces 是一个标准的 Java 框架,用于为 Web 应用程序构建用户界面。 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.最重要的是,它简化了用户界面的开发,这通常是 Web 应用程序开发中比较困难和乏味的部分之一。
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.尽管可以使用基础 Java Web 技术(如 Java servlet 和 JavaServer Pages)构建用户界面,而无需为企业 Web 应用程序开发设计的综合框架,但这些核心技术通常会导致各种开发和维护问题。 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.更重要的是,当开发人员获得生产质量的解决方案时,JSF 解决的同一组问题将以非标准方式解决。 JavaServer Faces is designed to simplify the development of user interfaces for Java Web applications in the following ways: JavaServer Faces 旨在通过以下方式简化 Java Web 应用程序用户界面的开发:
• It provides a component-centric, client-independent development approach to building Web user interfaces, thus improving developer productivity and ease of use. • 它提供了一种以组件为中心、独立于客户端的开发方法来构建 Web 用户界面,从而提高开发人员的生产力和易用性。
• It simplifies the access and management of application data from the Web user interface. • 它简化了从 Web 用户界面访问和管理应用程序数据的过程。
• 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. • 它描述了Web 应用程序的一组标准架构模式。

[ Source : Complete reference:JSF ] [来源:完整参考:JSF ]

There are also situations where you can favor JSP over JSF.在某些情况下,您可以更喜欢 JSP 而不是 JSF。 The application nature should be the deciding factor to choose the technology.应用性质应该是选择技术的决定因素。

If you have a rich GUI interaction and lot of Java scripting needed then favor JSF.如果您有丰富的 GUI 交互并且需要大量的 Java 脚本,那么支持 JSF。 Basically if your GUI app architecture is like Component oriented & even driven like Swing then JSF is the best.基本上,如果您的 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 可以做得很好。

Servlet - it's java server side layer. Servlet - 它是 Java 服务器端层。

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

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

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 . Servlets 和 JSP 之间的基本区别在于,在 Servlets 中我们编写 java 代码并嵌入 HTML 代码,而 JSP 则正好相反。 In JSP we write HTML code and in that we embed java code using tags provided by JSP.在 JSP 中,我们编写 HTML 代码,并使用 JSP 提供的标签嵌入 Java 代码。

Java Server Pages (JSP) is java technology which enables Web developers and designers to rapidly develop and easily maintain, information-rich, dynamic Web pages that leverage existing business systems. Java Server Pages (JSP)是一种 Java 技术,它使 Web 开发人员和设计人员能够利用现有业务系统快速开发和轻松维护信息丰富的动态 Web 页面。 JSP technology separates the user interface from content generation, enabling designers to change the overall page layout without altering the underlying dynamic content. JSP 技术将用户界面与内容生成分离,使设计人员能够在不改变底层动态内容的情况下更改整体页面布局。

Facelets is the first non JSP page declaration language designed for JSF (Java Server Faces) which provided a simpler and more powerful programming model to JSF developers as compare to JSP. Facelets是第一种为JSF(Java Server Faces)设计的非 JSP 页面声明语言,与 JSP 相比,它为 JSF 开发人员提供了更简单、更强大的编程模型。 It resolves different issues occurs in JSP for web applications development.它解决了 Web 应用程序开发在 JSP 中出现的不同问题。

Here is a table that compares the features of scriplets and facelets:这是一个比较 scriplets 和 facelets 特征的表格:

在此处输入图片说明 Source 来源

JSP:means HTML+Java Code:

JSP have it's own life cycle jsp_init() jsp_service() jsp_destroy JSP 有它自己的生命周期 jsp_init() jsp_service() jsp_destroy

After first request JSP convert to .java file.在第一次请求 JSP 转换为 .java 文件后。 There is three type of tag we are using我们正在使用三种类型的标签
1.)Scriptless 1.) 无脚本

<%  %>

Here developer can declare all those things which developer want to take the data在这里开发者可以声明开发者想要获取数据的所有东西

2.)Expression tag 2.)表达式标签

<%=  %>

Here developer can use some print related data这里开发者可以使用一些打印相关的数据

3.)Declaration 3.) 声明

<!% %>

Here developer can declare some method related data.开发者可以在这里声明一些方法相关的数据。

Servlet:

Servlet have it's own life cycle. Servlet 有它自己的生命周期。

init()
service()
destroy()

After first request container will read the data from web.xml file then after out welcome fill will be display.第一个请求容器会从 web.xml 文件中读取数据,然后显示欢迎填充。
Now onward after performing action it will search the url and after this process it will search the particular servlet there it self.现在,在执行操作后,它将搜索 url,在此过程之后,它将自行搜索特定的 servlet。 service operation will perform.服务操作将执行。

JSF:

JSF have it's own ui and it's life cycle can perform in six way, JSF 有它自己的 ui 并且它的生命周期可以以六种方式执行,

A)Restore view phase
B)Apply request values phase
C)Process validations phase
D)Update model values phase
E)Invoke application phase
F)Render response phase

For ui here for table here we are using panel grid and there is different faces for this that is.对于此处的 ui 此处的表格,我们使用面板网格,因此有不同的面。

Rich Faces
Prime Faces.

JSPs are the View component of MVC (Model View Controller). JSP 是 MVC(模型视图控制器)的视图组件。 The Controller takes the incoming request and passes it to the Model, which might be a bean that does some database access. Controller 接受传入的请求并将其传递给 Model,它可能是一个执行某些数据库访问的 bean。 The JSP then formats the output using HTML, CSS and JavaScript, and the output then gets sent back to the requester.然后 JSP 使用 HTML、CSS 和 JavaScript 格式化输出,然后将输出发送回请求者。

JSF is an advanced framework wherein its very easy to implement Model-View-Controller (MVC) based architecture for projects. JSF 是一个高级框架,其中非常容易为项目实现基于模型-视图-控制器 (MVC) 的架构。 Main advantage of JSF over JSP is the easy dynamic rendering of the components on the browser based upon conditions and easy integration of ajax events. JSF 相对于 JSP 的主要优点是基于条件的浏览器上组件的轻松动态呈现和 ajax 事件的轻松集成。

The front end of the JSF application ie xhtml files are the ones which are shown to the user via browser. JSF 应用程序的前端,即 xhtml 文件是通过浏览器显示给用户的文件。 These xhtml files internally invoke managed beans eg controllers wherein actual application logic is written.这些 xhtml 文件在内部调用托管 bean,例如控制器,其中编写了实际的应用程序逻辑。

The controllers internally invoke various services which communicate with database (using Hibernate or JPA API).控制器在内部调用与数据库通信的各种服务(使用 Hibernate 或 JPA API)。 This is how the flow happens in short.简而言之,这就是流程的发生方式。

JSF is also used in combination with RichFaces which is a framework for giving rich look and feel to your web application. JSF 还与 RichFaces 结合使用,RichFaces 是一个框架,可为您的 Web 应用程序提供丰富的外观和感觉。

JSF + RichFaces + Hibernate/JPA is a good technology to learn for sure ! JSF + RichFaces + Hibernate/JPA 是一个值得学习的好技术!

Jsp is also having in built servlet code which don't need any external compilation it can be run directly run. Jsp 也有内置的 servlet 代码,不需要任何外部编译,它可以直接运行。 Changes will take effect in jsp directly in a browser.更改将直接在浏览器中的jsp 中生效。

Servlet need to be compiled (ie it will have specific class creation) Servlet 需要编译(即会创建特定的类)

Jsf is a view component of MVC Framework Jsf 是 MVC 框架的一个视图组件

JSP stands for JAVA SERVER PAGE........ jsp is not a servlet. JSP 代表 JAVA SERVER PAGE.........jsp 不是 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. Jsp 本身使用代码和 HTML 标签,您不需要单独制作 HTML 和 servlet。Jsp 在 Web 应用程序中扮演着重要的角色。 Servlet is a java class plays an role to make your HTML page from static to dynamic . Servlet 是一个 java 类,它的作用是使您的 HTML 页面从静态变为动态。

Servlets are the server side java programs which execute inside the web container. Servlet是在 Web 容器内执行的服务器端 Java 程序。 The main goal of the servlet is to process the requests received from the client. servlet 的主要目标是处理从客户端收到的请求。

Java Server Pages is used to create dynamic web pages. Java Server Pages用于创建动态网页。 Jsp's were introduced to write java plus html code in a single file which was not easy to do in servlets program.引入 Jsp 是为了在单个文件中编写 java 和 html 代码,这在 servlet 程序中是不容易做到的。 And a jsp file is converted to a java servlet when it is translated.并且一个jsp文件在翻译的时候会转换成java servlet。

Java Server Faces is a MVC web framework which simplifies the development of UI. Java Server Faces是一个 MVC Web 框架,它简化了 UI 的开发。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM