Java portlet 和 servlet 有什么区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1480528/
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-12 13:16:34  来源:igfitidea点击:

what is the difference between a portlet and a servlet?

javaservletsjakarta-eeportlet

提问by Rajesh

I am asked to work on portlets and portals.

我被要求从事 portlet 和门户的工作。

I want to know the difference between a portlet and a servlet?

我想知道 portlet 和 servlet 之间的区别?

How / where does a portlet differ (may be functionally) from a servlet?

Portlet 与 servlet 有何不同(可能在功能上)?

采纳答案by topchef

Portlets are part of JSR-168 standard that regulates portal containers and components. This is different standard from standards for web containers (and servlets). Though there are definitely strong parallels between these two standards they differ in containers, APIs, life cycle, configuration, deployment, etc.

Portlet 是规范门户容器和组件的 JSR-168 标准的一部分。这与 Web 容器(和 servlet)的标准不同。尽管这两个标准之间肯定有很强的相似之处,但它们在容器、API、生命周期、配置、部署等方面有所不同。

The main difference between portlet vs. servlet could be that while servlet always responds to single type of action - request, portlet (due to nature of its life cycle and stronger container bindings) has to respond to two types of actions: renderand request. There are of course more to it but I found this as the core difference between the two when I studied portal development.

portlet 与 servlet 之间的主要区别可能在于,虽然 servlet 总是响应单一类型的 action - request,但 portlet(由于其生命周期的性质和更强的容器绑定)必须响应两种类型的 action:renderrequest。当然还有更多,但当我研究门户开发时,我发现这是两者之间的核心区别。

回答by whatnick

Servlets have a java definition (applications which handle HTTP GET/POST requests), while portlets have a user interface definition.A component performing a specific function similar to the windows vista widgets or a lot of components used in stackoverflow here. They need not necessarily be backed by servlets on the server side. But the Portlet standard was developed alongside java. O'Reilly has a nice tutorial.

Servlet 有一个 java 定义(处理 HTTP GET/POST 请求的应用程序),而 portlet 有一个用户界面定义。一个执行特定功能的组件,类似于 windows vista 小部件或这里 stackoverflow 中使用的许多组件。它们不一定需要由服务器端的 servlet 支持。但是 Portlet 标准是与 java 一起开发的。O'Reilly 有一个很好的教程

回答by djna

Both portlets and servlets receive an http request and return a response, which is usally some HTML that can be rendered by a browser. A portlet is used in the context of a "Portal", the idea being that a single page seen by the user has lots of parts, think tiles, each coming from a different portlet.

portlet 和 servlet 都接收一个 http 请求并返回一个响应,这通常是一些可以由浏览器呈现的 HTML。portlet 在“门户”的上下文中使用,其想法是用户看到的单个页面有很多部分,想想磁贴,每个部分都来自不同的 portlet。

Now, you can get that "tiled" effect from normal servets (See Struts + Tiles for an example of how) the extra bit from the portlets is that the portlets are in a richer environment provided by the Portal, extra APIs are provided so that what is displayed by any portlet can be configured by individual users to their preferences, and the porlets can communicate with each other - press a button in one, something happens in a another.

现在,您可以从普通服务中获得“平铺”效果(有关如何操作的示例,请参阅 Struts + Tiles) portlet 的额外部分是 portlet 位于门户提供的更丰富的环境中,提供了额外的 API,以便任何 portlet 显示的内容都可以由个人用户根据他们的喜好进行配置,并且这些 portlet 可以相互通信 - 在一个按钮中按下按钮,在另一个中发生某些事情。

回答by Liyakath

Essentially, Servlets provide content that normally takes up the whole page in a browser (unless you're using frames), and portlets provide content that is wrapped by a window. With portlets, you can have multiple portlets side by side with one another and each one can provide content and functionality that is different from the other. A portlet can provide the complete interaction for one type of application, while another portlet can provide content for another type of application. The portal can provide some house keeping functionality and secured single point of entry to all of the portlets on a page. As for the particulars (similarities/differences) between them, please continue reading. Here are some similarities: Servlets and portlets are web based components that utilize Java for their implementation Portlets are managed by a portlet container similar to a servlet container Both of these components generate content, which can be static or dynamic

本质上,Servlet 提供的内容通常占据浏览器中的整个页面(除非您使用框架),而 portlet 提供由窗口包装的内容。使用 portlet,您可以将多个 portlet 并排放置,并且每个 portlet 都可以提供彼此不同的内容和功能。一个 portlet 可以为一种类型的应用程序提供完整的交互,而另一个 portlet 可以为另一种类型的应用程序提供内容。门户可以提供一些内务管理功能和对页面上所有 portlet 的安全单点入口。至于它们之间的细节(相同点/不同点),请继续阅读。以下是一些相似之处:

回答by Ashok Goli

Enhanced from Source: Servlets Vs Portlets

从源头增强Servlets Vs Portlets

Similarities

  1. Servlets and Portlets are web based components which use Java for their implementation.

  2. Portlets are managed by a portlet container just like servlet is managed by servlet container.

  3. Both static and dynamic content can be generated by Portlets and Servlets.

  4. The life cycle of portlets and servlets is controlled by the container

  5. The client/server model is used for both servlets and portlets

  6. The packaging and deployment are essentially the same, WAR/EARs.

  7. Application Session exists in both Servlet and Portlet containers. It is one of the ways of of sharing data (crude Inter-Portlet Communication) from the render phase to the action phase (or any lower phases) in the portlet containers.

  8. Both Servlets and Portlets use similar server / VM environments that support it. Although, some additional configurations might needed in case of portlets to make it tick
  9. The build/DI tools are similar for both - Ant, Maven, Gradle, etc are all supported. Mostly :) - This has changed a bit with Liferay 7.

相似之处

  1. Servlet 和 Portlet 是基于 Web 的组件,它们使用 Java 来实现。

  2. Portlet 由 Portlet 容器管理,就像 servlet 由 servlet 容器管理一样。

  3. 静态和动态内容都可以由 Portlet 和 Servlet 生成。

  4. portlet 和 servlets 的生命周期由容器控制

  5. 客户机/服务器模型用于 servlet 和 portlet

  6. 打包和部署本质上是一样的,WAR/EARs。

  7. Application Session 存在于 Servlet 和 Portlet 容器中。它是在 portlet 容器中从呈现阶段到操作阶段(或任何较低阶段)共享数据(粗略的 Portlet 间通信)的方式之一。

  8. Servlet 和 Portlet 都使用类似的服务器/VM 环境来支持它。虽然,在 portlet 的情况下可能需要一些额外的配置来使其勾选
  9. 两者的构建/DI 工具相似——Ant、Maven、Gradle 等都受支持。主要是 :) - Liferay 7 的情况有所改变。


Dissimilarities

  1. Servlets can render complete web pages, whereas portlets renders html fragments. These fragments are aggregated by the portal into a complete web page.

  2. The content type of JSR 168 portlets can be only cHTML, XHTML, WML. It does not support other content types.

  3. Portlets are not allowed to generate HTML code that contains tags such as body, frame, frameset, head, html, or title.

  4. A Portlet unlike a servlet doesn't have URL attached to it so it cannot be accessed directly. Access is only through the portal page which holds the portlet.

  5. Portlets can be provided with controls to manipulate its window states or portlet modes.

  6. Multiple instances of a single portlet can be placed onto the same page.

  7. Portlets support persistent configuration and customization, profile information.

  8. Portlets can have two types of request viz. render request and action request.

  9. Portlets have two scopes within session; application scope for communication across portlets and portlet scope for intra portlet communication.

  10. Portlet cannot set the character set encoding of the response nor can it set the HTTP response headers.

  11. Portlets doesn't have access to request URL. So it cannot access the query parameters appended to the URL. Portlets cannot set cookies.

  12. Typical methods of Portlet API are doView(), doEdit(), doHelp()and processAction()while those of servlet are service(), doPost(), doGet().

  13. Servlet Specifications- JSR 369(Servlet 4.0), JSR 340(Servlet 3.1), JSR 315(Servlet 3.0), JSR 154(Servlet 2.5 & 2.4). Portlet Specifications- JSR 168(Portlet Spec v1.0), JSR 286(Portlet Spec v2.0), JSR 362(Portlet Spec v3.0)

  14. Deployment of Portlets involves different approachthan a Servlet application. Some Providers (Liferay/Alfresco/WebSphere) support hot-deploying of portlets without the need to restart the server which is not possible in case of servlets without modularizing the application using special libraries such as OSGi.

差异

  1. Servlet 可以呈现完整的网页,而 Portlet 可以呈现 html 片段。这些片段由门户聚合成一个完整的网页。

  2. JSR 168 portlet 的内容类型只能是 cHTML、XHTML、WML。它不支持其他内容类型。

  3. 不允许 Portlet 生成包含诸如正文、框架、框架集、头部、html 或标题等标签的 HTML 代码。

  4. 与 servlet 不同的 Portlet 没有附加 URL,因此不能直接访问它。只能通过包含 portlet 的门户页面进行访问。

  5. 可以为 Portlet 提供控件以操纵其窗口状态或 Portlet 模式。

  6. 可以将单个 portlet 的多个实例放置在同一页面上。

  7. Portlet 支持持久配置和定制、配置文件信息。

  8. Portlet 可以有两种类型的请求即。呈现请求和动作请求。

  9. Portlet 在会话中有两个作用域;Portlet 间通信的应用范围和 Portlet 内部通信的 Portlet 范围。

  10. Portlet 不能设置响应的字符集编码,也不能设置 HTTP 响应头。

  11. Portlet 无权访问请求 URL。因此它无法访问附加到 URL 的查询参数。Portlet 无法设置 cookie。

  12. Portlet API的典型的方法是doView()doEdit()doHelp()processAction()而那些的servlet的是service()doPost()doGet()

  13. Servlet 规范- JSR 369(Servlet 4.0)、JSR 340(Servlet 3.1)、JSR 315(Servlet 3.0)、JSR 154(Servlet 2.5 & 2.4)。 Portlet 规范- JSR 168(Portlet Spec v1.0)、JSR 286(Portlet Spec v2.0)、JSR 362(Portlet Spec v3.0)

  14. Portlet 的部署涉及Servlet 应用程序不同的方法。一些提供程序(Liferay/Alfresco/WebSphere)支持 portlet 的热部署,而无需重新启动服务器,这在 servlet 的情况下是不可能的,而无需使用特殊的库(如 OSGi)对应用程序进行模块化。



Edit(From comments)

编辑(来自评论)

A Portlet container is built on a Servlet container. So ultimately it can be said that the portlet runs on a Servlet Container. But while developing apps, we view a portlet container separately from the Servlet/Java EE container.

Portlet 容器构建在 Servlet 容器上。所以最终可以说 portlet 在 Servlet Container 上运行。但是在开发应用程序时,我们将 Portlet 容器与 Servlet/Java EE 容器分开查看。