Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Servlets or Jsp

 
Val Dra
Ranch Hand
Posts: 439
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Tony Alicea
Desperado
Sheriff
Posts: 3226
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).
 
Val Dra
Ranch Hand
Posts: 439
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks to both of you , but if jsp gets compiled into servlet isn't it slower becase servlets are already compiled?
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic