- Servlets are difficult to code which are overcome in JSP. Other way, we can say, JSP is almost a replacement of Servlets, (by large, the better word is extension of Servlets), where coding decreases more than half.
- In Servlets, both static code and dynamic code are put together. In JSP, they are separated. For example,In Servlets:
out.println(“Hello Mr.” + str + ” you are great man”);
where str is the name of the client which changes for each client and is known as dynamic content. The strings, “Hello Mr.” and “you are great man” are static content which is the same irrespective of client. In Servlets, in println(), both are put together.
Hello Mr. // static content as HTML code
<%= str %> // dynamic content as JSP code
you are great man // static content as HTML code
In JSP, the static content and dynamic content is separated. Static content is written in HTML and dynamic content in JSP. As much of the response comprises of static content (nearly 70%) only, the JSP file more looks as a HTML file.
Let us clarify more about this concept.
A Web site comes with both static content and dyanamic content. Static content involves displaying company logo, company building, company lietrature like company present and back history etc. which will never change. This is done by a (Graphic) Designer with the knowledge of just HTML and some Photoshop.
The dynamic content changes every day and every minute like displaying running share prices, clearing client doubts and accessing database to fulfil client requested data etc. This is done by by a JSP Programmer.
- Programmer inserts, here and there, chunks of JSP code in a running HTML developed by Designer. As much of the response delivered to cleint by server comprises of static content (nearly 70%), the JSP file more looks like a HTML file. Other way we can say, JSP is nothting but Java in HTML (servlets are HTML in Java); java code embedded in HTML.
- When the roles of Designer and Programmer are nicely separated, the product development becomes cleaner and fast. Cost of developing Web site becomes cheaper as Designers are much paid less than Programmers, especially should be thought in the present competitive world.
- Both presentation layer and business logic layer put together in Servlets. In JSP, they can be separated with the usage of JavaBeans.
- The objects of PrintWriter, ServletConfig, ServletContext, HttpSession and RequestDispatcher etc. are created by the Programmer in Servlets and used. But in JSP, they are builtin and are known as "implicit objects". That is, in JSP, Programmer never creates these objects and straightaway use them as they are implicitly created and given by JSP container. This decreases lot of coding.
- JSP can easily be integrated with JavaBeans.
- JSP is much used in frameworks like Sturts etc.
- With JSP, Programmer can build custom tags that can be called in JavaBeans directly. Servlets do not have this advantage. Reusability increases with tag libraries and JavaBean etc.
- Writing alias name in <url-pattern> tag of web.xml is optional in JSP but mandatory in Servlets.
- A Servlet is simply a Java class with extension .java written in normal Java code.
- A Servlet is a Java class. It is written like a normal Java. JSP is comes with some elements that are easy to write.
- JSP needs no compilation by the Programmer. Programmer deploys directly a JSP source code file in server where as incase of Servlets, the Programmer compiles manually a Servlet file and deploys a .class file in server.
- JSP is so easy even a Web Designer can put small interactive code (not knowing much of Java) in static Web pages.
- First time when JSP is called it is compiled to a Servlet. Subsequent calls to the same JSP will call the same compiled servlet (instead of converting the JSP to servlet), Ofcourse, the JSP code would have not modified. This increases performance.
- What are the prerequisites to learn JSP?
- What is JSP (Java Server Page)?
- 15 advantages of JSP over Servlets
- What is JSP engine or what is JSP container?
- How JSP works or What is JSP Architecture?