hi , i just started learning jsp and servlets and i am going though the examples given in the tomcat example directory . Question is what is being used more Servlets or JSP ? i think servlets run faster then jsp page. I look how each works and servlets run faster on my pc here. THanks. ------------------ Val SCJP going for SCJD
JSPs get compiled into Servlets so theoretically they are the same. Differences in performance can be due to two things. Firstly (depending on app server configuration) the first time a jsp is accessed, it is converted into a servlet. This may never happen again but it significantly slow down the reponse time the first time the jsp is visitted. Secondly, the efficiency of the produced servet depends on how well the jsp conversion is written. For example, the servlet may implement inefficient code to accomplish a jsp command. Therefore the answer to you second question is that JSPs and Servlets should run the same. Personally I use JSPs much more since they are a lot easier to develop web pages (text/html) than servlets, but if you want to return other content types (eg image/gif etc) you need to do that in a servlet.
A JSP gets compiled into a Servlet so for the server they are both the same. Maybe the differences you are observing are subjective. In any case, I don't think that should be the factor deciding which one to use. You can use both, each one for what it does better (hint: Separation of presentation from control logic).
Tony Alicea Senior Java Web Application Developer, SCPJ2, SCWCD
Val Dra
Ranch Hand
Joined: Jan 26, 2001
Posts: 439
posted
0
thanks to both of you , but if jsp gets compiled into servlet isn't it slower becase servlets are already compiled?
Only the first time it is called. From then on the 'servlet' version of the jsp is called. Depending on the app server you're using it is possible to configure this so that (for instance) the jsp is always recompiled, never recompiled, or even recompiled every 10 seconds.
Peter den Haan
author
Ranch Hand
Joined: Apr 20, 2000
Posts: 3252
posted
0
A .jsp always hits an additional servlet which forwards the request to the actual page. The way jsps work is that any request for a file with the .jsp extension is mapped to a single jsp container servlet. This servlet is responsible for checking if you modified the .jsp file, recompiling it if necessary, and forwarding the request to the actual servlet that your .jsp compiles down to. The additional overhead is minor, but it's there. - Peter