What is the difference between JSF, Servlet and JSP?
Asked Answered
L

16

884

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 JSP like ASP.NET-MVC?
Larder answered 19/1, 2010 at 17:15 Comment(1)
In JSF 2.0+, xml is not necessary.Skite
C
1288

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

Caller answered 19/1, 2010 at 22:39 Comment(11)
@Caller : I have seen this type of code used in jsp instead of java code - <c:forEach items="${items}"> <jsp:attribute name="var"> <mytag:doesSomething/> </jsp:attribute> What is this?Timon
@Shashank: Not entirely. Only the <c:forEach> tag is from JSTL. See also stackoverflow.com/tags/jstl/infoCaller
Yes. <mytag:doesSomething/> looks like a custom tag and <jsp:attribute>, a jsp tag.Legislator
@Caller I'm confused. What view technology are we supposed to be using? Java web apps seem to be all over the place, and it is the most confusing set of technologies to get start with I have ever seen.Himation
Since this is a hugely popular answer, I want to add a very important bit which is JSP tag files which allows for custom tag creation for page composition and layout without writing Java code. This feature is extremly useful and has been part of the standard for many years yet remains underutilized.Polygraph
@Himation Facelets has been the preferred view technology since Java EE 6 was released in 2009 (docs.oracle.com/javaee/6/tutorial/doc/giepx.html). The minimum set of technologies to develop Java web apps isn't higher than most other languages, but there are many more options and competitors, which is confusing to newcomers. Web development in Ruby? The first choice is obvious.Blucher
@Nimnio Thanks. I found so many conflicting ideas on Facelets and all the other "lets," that it was almost impossible to decide what to do and remain "standard." In some ways Facelets felt like I was back in time with MS WebForms with hew JavaScript libraries. I know that's simplistic.Himation
@Caller Can I have little advice from you, I have learned Java SE well and now I in a confusion, whether to learn Java EE as a web technology because I have heard that oracle has stopped supporting for Java EE( no Java EE 8 doc). will it be a waste of time of learning Java EE?, if it is so can you be kind enough to suggest me a language to studyEyewash
@Kasun: Oracle hasn't formally stopped supporting Java EE, it just temporarily used its resources for something else they didn't yet tell about. They will tell during JavaOne coming September. See also theregister.co.uk/2016/07/07/oracle_java_ee_8Caller
@Caller thankyou, Can you provide me any good links to tutorials that I can learn Java EE from scratch. I found some video tutorials, but the rest of the series is not free, we need to buy them :( since I am student I can afford that much moneyEyewash
They are all linked in "Related questions" and in links of answers thereof, particularly the "Java EE web development, what skills do I need?" one.Caller
G
89

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

See 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 Java Server Pages (JSP) custom tag library for expressing a JavaServer Faces interface within a JSP page.

JSP is a specialized kind of servlet.

JSF is a set of tags that you can use within a JSP.

Gunslinger answered 19/1, 2010 at 17:28 Comment(1)
"JSP is a specialized kind of servlet. JSF is a set of tags you can use with JSP." This statement best describes the relationship among the three!Rizzio
A
35

From Browser/Client perspective

JSP and JSF both look the same, as Per Application Requirements go, JSP is more suited for request and response based applications.

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

From Server Perspective

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

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

Aliped answered 6/4, 2012 at 16:24 Comment(0)
R
25

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.

[ Source : Complete reference:JSF ]

Rubiaceous answered 31/7, 2013 at 6:48 Comment(2)
It wasn't me, but I imagine it's because you just basically copy and pasted an answer from a book.Warmth
@Oberon: Thanks for the reply. It took a while for me to find the exact words so went for book. If its not appropriate, then you or any one can suggest me to take down the answer. I'll be glad to do it.Rubiaceous
B
18

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

If you have a rich GUI interaction and a 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.

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

Bernadette answered 20/6, 2013 at 7:59 Comment(0)
H
14

Servlet - it's Java server-side layer.

  • JSP is a Servlet with html
  • JSF is a component base on tag libs
  • JSP is a converted into servlet once when the server got a request.
Halbert answered 14/5, 2011 at 21:38 Comment(2)
JSP is converted into servlet once when server got request.Halbert
... when server got FIRST request. Mind caching.Bosh
P
10

JSP is indeed converted into a servlet at the time of execution, and JSF is a new thing to make the webpage more readable as JSF allows writing all the programming structures in the form of tags.

Pied answered 23/2, 2011 at 6:21 Comment(0)
P
6

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.

Parity answered 30/11, 2014 at 9:10 Comment(0)
S
5

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. JSP technology separates the user interface from content generation, enabling designers to change the overall page layout without altering the underlying dynamic content.

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. It resolves different issues occurs in JSP for web applications development.

Here is a table that compares the features of scriplets and facelets:

enter image description here Source

Skardol answered 24/12, 2015 at 11:17 Comment(0)
A
1
JSP:means HTML+Java Code:

JSP have it's own life cycle jsp_init() jsp_service() jsp_destroy

After first request JSP convert to .java file. There is three type of tag we are using
1.)Scriptless

<%  %>

Here developer can declare all those things which developer want to take the data

2.)Expression tag

<%=  %>

Here developer can use some print related data

3.)Declaration

<!% %>

Here developer can declare some method related data.

Servlet:

Servlet have it's own life cycle.

init()
service()
destroy()

After first request container will read the data from web.xml file then after out welcome fill will be display.
Now onward after performing action it will search the url and after this process it will search the particular servlet there it self. service operation will perform.

JSF:

JSF have it's own ui and it's life cycle can perform in six way,

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.

Rich Faces
Prime Faces.
Addington answered 4/7, 2016 at 11:55 Comment(0)
B
-1

JSPs are the View component of MVC (Model View Controller). The Controller takes the incoming request and passes it to the Model, which might be a bean that does some database access. The JSP then formats the output using HTML, CSS and JavaScript, and the output then gets sent back to the requester.

Bimolecular answered 23/5, 2016 at 12:48 Comment(0)
V
-1

JSF is an advanced framework wherein its very easy to implement Model-View-Controller (MVC) based architecture for projects. 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.

The front end of the JSF application i.e. xhtml files are the ones which are shown to the user via browser. These xhtml files internally invoke managed beans e.g. controllers wherein actual application logic is written.

The controllers internally invoke various services which communicate with database (using Hibernate or 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 + Hibernate/JPA is a good technology to learn for sure !

Volkan answered 11/5, 2017 at 8:37 Comment(0)
N
-1

JSP stands for JavaServer Pages while JSF stands for JavaServer Faces. JSP is a technology that helps developers develop dynamic web pages using technologies like HTML, XML and similar other languages. JSF is a framework that helps developers develop user interfaces for server-side applications. Both these technologies are based on Java and they are primarily used for web-based applications. JSP is more like ASP or PHP except the fact that it is based on Java which means it uses Java programming language. Both the technologies are developed by Sun Microsystems. It is interesting to note that JSP 1.x versions used JSP as the default system for templating but JSP 2.x uses Facelets instead of JSP.

Nasal answered 7/2, 2022 at 13:22 Comment(0)
F
-2

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.

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

Jsf is a view component of MVC Framework

Fala answered 12/2, 2015 at 13:1 Comment(0)
S
-2

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 .

Springhead answered 14/3, 2015 at 9:54 Comment(1)
JSPs are indeed converted into Servlet components. "JSPs are translated into servlets at runtime". Source: en.wikipedia.org/wiki/JavaServer_PagesBosh
F
-2

Servlets are the server side java programs which execute inside the web container. The main goal of the servlet is to process the requests received from the client.

Java Server Pages is used to create dynamic web pages. Jsp's were introduced to write java plus html code in a single file which was not easy to do in servlets program. And a jsp file is converted to a java servlet when it is translated.

Java Server Faces is a MVC web framework which simplifies the development of UI.

Flynt answered 10/1, 2019 at 16:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.