C Books Guide and List
C++ Books Guide and List
Best Java Books

3,749 questions

4,049 answers


10,705 users

What is the difference between JSF, Servlet and JSP?

0 votes
asked Jan 21, 2014 by john Guru (27,140 points)

2 Answers

0 votes
JSP (JavaServer Pages)
JSP is a Java view technology running on the server machine which allows you to write template text in (the client side languages like HTML, CSS, JavaScript and so on). JSP supports the so-called taglibs which are backed by pieces of Java code with which you can control the page flow and/or output dynamically (programmatically). A well known taglib is JSTL. JSP also supports Expression Language which can be used to access backend data (actually, the attributes which are available in page, request, session and application scopes), mostly in combination with taglibs.
When a JSP is requested for the first time or when the webapp starts up, the servlet container will compile it into a class extending HttpServlet and use it during the webapp'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 servletcontainer will execute the compiled JSP class and send the generated output (usually just HTML/CSS/JS) through the webserver over network to the client side which in turn displays it in the webbrowser.
Servlet is an Java application programming interface (API) running on the server machine which can intercept on the requests made by the client and can generate/send a response accordingly. 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 on a certain HTTP URL pattern, which is configureable in web.xml, or more recently with Java EE 6, with @WebServlet annotation.
When a Servlet is requested for the first time or when the webapp starts up, the servlet container will create an instance of it and keep it in memory during webapp'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 in flavor of 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, so that you're forced to create custom components with raw Java code (which is a bit opaque and a lot of tedious work in JSF) when you want to replace a repeated group of components by a single component. If you can, I recommend to drop JSP and go for Facelets when you want to develop with JSF.
answered Jan 21, 2014 by sara Guru (24,570 points)
0 votes
From Browser/Client perspective:

JSP and JSF both looks same, As Per Application Requirements goes, JSP is more suited for request - response based applications.

JSF is targetted for richer event based Web applications. 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.

JSF page is converted to components tree(by specialized FacesServlet) and it follows component lifecycle defined by spec.
answered Jan 21, 2014 by keem Expert (13,240 points)

Related questions

0 votes
1 answer 16 views
16 views asked Jan 2, 2014 by john Guru (27,140 points)
0 votes
2 answers 200 views
200 views asked Jan 21, 2014 by john Guru (27,140 points)
0 votes
1 answer 113 views
0 votes
1 answer 53 views
53 views asked Jan 3, 2014 by smith Geek (15,340 points)