本文介绍了JSF、Servlet 和 JSP 之间有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些问题.这些是:

I have some questions. These are :

  1. JSP 和 Servlet 之间有什么关系?
  2. JSP 是某种 Servlet 吗?
  3. JSP 和 JSF 之间有什么关系?
  4. JSF 是不是类似于 ASP.NET-MVC 的某种基于预构建 UI 的 JSP?

推荐答案

JSP (JavaServer Pages)

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

JSP (JavaServer Pages)

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

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. You can find the generated source code in the server's work directory. In for example Tomcat, it's the /work directory. 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.

Servlet 是运行在服务器机器上的 Java 应用程序编程接口 (API),它拦截客户端发出的请求并生成/发送响应.一个著名的例子是 HttpServlet,它提供了钩住 的方法HTTP 请求使用流行的HTTP 方法,例如 GETPOST.您可以配置 HttpServlets 以侦听特定的 HTTP URL 模式,该模式可在 web.xml 中配置,或者最近使用 Java EE 6,带有 @WebServlet 注释.

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 HttpServlet which provides methods to hook on HTTP requests using the popular HTTP methods such as GET and 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 @WebServlet annotation.

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

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 HttpServletRequest and 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().

JSF 是一个基于组件的 MVC 框架,它建立在 Servlet API 之上并提供 components 通过 taglibs 可用于 JSP 或任何其他基于 Java 的视图技术,例如 Facelets.Facelets 比 JSP 更适合 JSF.它即提供了出色的模板功能,例如复合组件,而JSP基本上只提供 用于 JSF 中的模板,以便当您想用单个组件替换重复的一组组件时,您不得不使用原始 Java 代码创建自定义组件(这有点不透明且工作量很大).从 JSF 2.0 开始,JSP 作为视图技术已被弃用,取而代之的是 Facelets.

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. Facelets is much more suited to JSF than JSP. 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. Since JSF 2.0, JSP has been deprecated as view technology in favor of Facelets.

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

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

注意:JSP 借助 Taglib 具有强大的模板化能力,尤其是 (标签文件) 变体.缺少 JSP 模板与 JSF 的结合.

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.

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

As being a MVC (Model-View-Controller) framework, JSF provides the FacesServlet as 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 View and 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 FacesServlet uses the JSF component tree to do all the work.

这篇关于JSF、Servlet 和 JSP 之间有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 01:09