Can anybody tell me the advantages of JSP over Servlets?
JSPs give a seperation between content and presentation. Browse to http://www.javaworld.com/javaworld/jw-12-1999/jw-12-ssj-jspmvc.html for more details
Advantages of using JSP
(source : http://developer.java.sun.com/developer/technicalArticles/Programming/jsp/)
1. JSP pages easily combine static templates, including HTML or XML fragments, with code that generates dynamic content.
2. JSP pages are compiled dynamically into servlets when requested, so page authors can easily make updates to presentation code. JSP pages can also be precompiled if desired.
3. JSP tags for invoking JavaBeans components manage these components completely, shielding the page author from the complexity of application logic.
4. Developers can offer customized JSP tag libraries that page authors access using an XML-like syntax.
5. Web authors can change and edit the fixed template portions of pages without affecting the application logic. Similarly, developers can make logic changes at the component level without editing the individual pages that use the logic.
Jason Hunter has given a comprehensive report on the topic JSP Vs Servlets. In that document he clearly mentions some disadvantages of using JSP. (though not always)
1. JSPs are converted into servlets by the JSP container. This makes the page load a bit late ( but only for the first time) from then on it loads the servlet from the cache.
Solution: We can use some tools like TomCat and Resin to precompile the JSP and include the servlet in the war file, thereby reducing the time lag even for the first time.
2. Java code in JSP : This actually proves to be a nightmare for HTML programmers as java code in JSP has got "lousy loopings" . The main idea of JSP was to clearly seperate the presentation from the business logic or the processing logic. But it does'nt seem to operate that way. The temptation to add java code in JSP is always there. Sometimes the HTML programmers may even "knock off" some java code in the JSP file leading to "chaos"
Solution: We can use the combination of custom TagLibs and Bean Tags to overcome this problem, but only to a certain extent. Moreover if we have our architecture right, wherein we use a combination of servlets and JSP we can overcome the above problem. In this architecture only the "View" is given to the JSP. The "Controller" will be a servlet that takes care of the processing logic.
3. Using Templates in JSP : Though in its initial stage, by using templates we can componentize the GUI also. The design pattern says " Anything that changes " should be encapsulated.
To conclude .... It feels "yakee" to write lots of out.println statements in the servlet. So just by using servlets we dont acheive anything, an efficient combination of Servlets and JSP can solve all the problems.
I hope this Helps !!!
Hi thanks for your reply but tell me jsp without servlets are not that effective. Can i use jsp alone for my applications?
Understand the fact that JSP is a servlet !!! By which What I mean is all the JSP pages are internally converted into a Servlet.
In simple terms, a servlet is a Java code with HTML code embedded (by using out.println()), a JSP is a HTML code with java code embedded by using tags.
So all that a servlet does can be done using effective combination of JSP and Java Beans.
Ahha !!! Why should there be a Bean at all ?
As I said before, the prime aim of JSP is to seperate the presentation from business logic.
Now, If u say u dont want to use Beans.... Go Ahead include all the Business Logic and the presentation Logic in the JSP. By doing that u are making the JSP file look more cumbersome, thereby making the code maintenance difficult.
Do u want to that ? I bet u dont !!!
So, What is the solution ?
Ans: The solution is use
JSP - for presentation.
Servlet - for Processing logic.
Bean - for Business Logic.