Difference Between Jsf, Servlet and Jsp

What is the difference between JSF, Servlet and 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.

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.

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 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.

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 (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. 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.

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

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.

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.

Related questions

  • What is the main-stream Java alternative to ASP.NET / PHP?
  • Java EE web development, what skills do I need?
  • How do servlets work? Instantiation, session variables and multithreading
  • What is a Javabean and where are they used?
  • How to avoid Java code in JSP files?
  • What components are MVC in JSF MVC framework?
  • What is the need of JSF, when UI can be achieved with JavaScript libraries such as jQuery and AngularJS

Java EE vs JSP vs JSF

Is JSP "dead" in favor of JSF?

JSF has countless benefits over JSP. For instance:

  • It defines a MVC approach
  • It set up componentization standards
  • It has apply values feature
  • Built-in AJAX
  • A defined view context control
  • Allows for rich interfaces extensions like Primefaces

And we can go on and on.

You can still use JSP for other scenarios where you need some specific flexibility or performance, and the same thing for servlets, but JSF pretty much replaced JSP for "robust" applications.

Now, I am a huge fan of JSF, but it has a long way to go. JSF 2.2 kinda looks like a mature framework now that it has a defined navigation standard (FacesFlow), and we just had a built-in file uploader in 2.1 and it is not even AJAX, and there's "HTML5", etc. So yeah, there is a lot more work to be done that I won't detail in here.

In my experience, JSP is in fact "dead" if compared to JSF and other frameworks like Spring MVC and others. Java EE 7 tutorial barely says anything about JSP. But it is not dead dead, since it is already supported in Web Containers and you can still use it.

Is Java EE the platform JSF runs on top of or something different
altogether?

JSF is part of Java EE but you do not need full Java EE profile in order to use JSF. Examples:

  • Tomcat is just a Java EE Web Profile implementation, and you can use
    JSF in Tomcat.
  • You can use JSF in JBoss but you do not need JMS enabled in order to JSF to work.

Java EE components are modular, and you only need a Web Profile server/container in order to use JSF.

Is JSF merely an MVC framework for developing web applications?

Yes (but I wouldn't say merely). Each one has it own pros and cons. But the principle is the same.

One could argue about integration with EJB, but so is Spring MVC with its own container.

Is JSF a framework developed by Oracle and part of Java, or is it a
separate framework altogether (Much like the Zend Framwork is from
PHP)?

Oracle now delegates to teams to define specification. In theory, you can implement your own JSF if you want. I do not know about PHP's Zend Framework.

Most common JSF implementations are Mojarra and MyFaces. (Luiggi beat me on this right now, you can check his links).

[Bonus] Would you recommend learning jsp or jsf?

I would recommend both. JSP first and JSF after it.

But I would 100% recommend you to use JSF for you projects. But make sure you understand componentization and all the stuff that makes JSF a powerful tool.

Also check out JSF 2.2 new features, this page is awesome for an intro on the latest features added to the framework.

Java EE 6: JSF vs Servlet + JSP. Should I bother learning JSF?

JSF basically enables you to develop a web application with only model objects (JavaBeans) and views (JSP/XHTML pages). With "plain vanilla" JSP/Servlet you'll have to bring in a lot of code to control, preprocess, postprocess, gather data, validate, convert, listen, etc the HTTP request and response. And then I'm not talking about refactoring it to a high (abstract) degree so that you can also end up the same way as JSF does (just a JavaBean class and a JSP/XHTML page per use case).

I've posted a more detailed answer on the subject before here: What is the difference between JSF, Servlet and JSP?

What is the difference between creating JSF pages with .jsp or .xhtml or .jsf extension

JSP is an old view technology and widely used in combination with JSF 1.x. Facelets (by some people overgeneralized as XHTML) is the successor of JSP and introduced as default view technology of JSF 2.x at end of 2009. When you were seeing JSPs, you were perhaps reading outdated books, tutorials or resources targeted on JSF 1.x. You should generally ignore them when developing with JSF 2.x and head to resources targeted on JSF 2.x, otherwise you may end up in confusion because many things are done differently in JSF 2.x on Facelets.

The *.jsf is just one of widely used URL patterns of the FacesServlet mapping in web.xml. Other ones are *.faces and /faces/*, but those are from back in the JSF 1.0/1.1 ages. They all do not represent the concrete file extension/path, but just a virtual file extension/path and is to be specified in URLs only like so http://example.com/contextname/page.jsf. If you are familiar with basic Servlets, then you should know that the servletcontainer will invoke the servlet when the request URL matches the servlet's URL pattern. So when the request URL matches *.jsf, then the FacesServlet will be invoked this way. When using JSPs, it would actually execute page.jsp. When using Facelets, this would actually compile page.xhtml.

Since JSF 2.x you can also use *.xhtml as URL pattern. This way you don't need to get confused when specifying URLs. Using *.xhtml as URL pattern was not possible in JSF 1.x with Facelets 1.x, because the FacesServlet would then run in an infinite loop calling itself everytime. An additional advantage of using *.xhtml is that the enduser won't be able to see raw JSF source code whenever the enduser purposefully changes the URL extension in browser address bar from for example .jsf to .xhtml. It is not possible to use *.jsp as URL pattern, because this way the container's builtin JspServlet, which is already using that URL pattern, would be overridden and then the FacesServlet wouldn't be able to feed on JSPs anymore.

See also:

  • What is the difference between JSF, Servlet and JSP?
  • Why Facelets is preferred over JSP as the view definition language from JSF2.0 onwards?
  • JSF Facelets: Sometimes I see the URL is .jsf and sometimes .xhtml. Why?

JSF vs Facelets vs JSP

  • JSF is a standardized Java framework for web UIs based on an MVC pattern
  • JSPs are a (much older) standard for generating web pages from templates - these can be used as the View in a JSF application, but also separately from JSF.
  • Facelets are an alternative view technology based on pure XML templates (no scriptlets) which was introduced with Version 2 of the JSF standard. They can only be used in a JSF application.

In the light of that, let's take a look at your conflicting statements:

That JSF is a replacement for JSP; and

Not quite true, since JSF can use JSPs for its view (and had to, prior to JSF 2). However, JSF apps using Facelets can be seen as a replacement for JSP-based technologies.

JSF and JSP form different parts of the View in Java's web-tier MVC paradigm

Completely wrong - JSF covers the entire MVC pattern (though it can overlap with EJBs, since both are based on annotations that can be mixed in the same class).



Related Topics



Leave a reply



Submit