JSPs are not and cannot be faster than servlets; they are servlets.
A JSP engine parses a JSP page into a .java file; when compiled, that file runs as a servlet.
The reason to use JSP pages is to separate the logic and presentation layers of your application, not to increase your performance. In fact, JSPs are slower when first called, because the compilation process has to take place.
Hi,
If you are running the JSP file first time that is slow becuase it needs to compile (it'll write the servlet in the background and execute that). After that it directly executed the precompiled servlet code thats why it's faster. If you do any changes to the JSP file or JAVA file (If you are calling as bean) it needs to recompile.
We had a long discussion on this a few weeks back. My feeling was that JSP could be faster than Servlets because when they are compiled, the compiler can optimise code in ways that most servlet programmers would not know how to. The whole thread is here: http://forums.java.sun.com/thread.jsp?forum=45&thread=136410
Hi simmy1,
The performance of JSP pages is very close to that of servlets. However, users may experience a perceptible delay when a JSP page is accessed for the very first time. This is because the JSP page undergoes a "translation phase" wherein it is converted into a servlet by the JSP engine. Once this servlet is dynamically compiled and loaded into memory, it follows the servlet life cycle for request processing. Here, the jspInit() method is automatically invoked by the JSP engine upon loading the servlet, followed by the _jspService() method, which is responsible for request processing and replying to the client. Do note that the lifetime of this servlet is non-deterministic - it may be removed from memory at any time by the JSP engine for resource-related reasons. When this happens, the JSP engine automatically invokes the jspDestroy() method allowing the servlet to free any previously allocated resources.
Subsequent client requests to the JSP page do not result in a repeat of the translation phase as long as the servlet is cached in memory, and are directly handled by the servlet's service() method in a concurrent fashion (i.e. the service() method handles each client request within a seperate thread concurrently.)
I hope this will help you out.
Regards,
TirumalaRao.
Developer TechnicalSupport,
Sun Microsystems,India.